串口通信与读写文件基本类似:varFCOMM: TFCOMM;Viewstring:string;i:integer;rbuf,sbuf:array[1..6] of byte;打开串口 procedure TFCOMM.FormShow(Sender: TObject);begincomm1.StartComm;end;关闭串口procedure TFCOMM.FormClose(Sender: TObject; var Action: TCloseAction);begincomm1.StopComm;end;发送数据自定义的发送过程procedure senddata;vari:integer;commflg:boolean;beginviewstring:="";commflg:=true;for i:=1 to 6 dobeginif not fcomm.comm1.writecommdata(@sbuf,1) then begincommflg:=false;break;end;sleep(2); {发送时字节间的延时}viewstring:=viewstring+inttohex(sbuf,2)+" ";end;viewstring:="发送"+viewstring;fcomm.memo1.lines.add(viewstring);fcomm.memo1.lines.add("");if not commflg then messagedlg("发送失败!",mterror,[mbyes],0);end;procedure TFCOMM.Btn_sendClick(Sender: TObject);{发送按钮的点击事件}beginsbuf[1]:=byte($f0); {帧头}sbuf[2]:=byte($01); {命令号}sbuf[3]:=byte($ff);sbuf[4]:=byte($ff);sbuf[5]:=byte($01);sbuf[6]:=byte($0f); {帧尾}senddata;{调用发送函数}end;接收过程procedure TFCOMM.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);var i:integer;beginviewstring:="";move(buffer^,pchar(@rbuf^),bufferlength);for i:=1 to bufferlength doviewstring:=viewstring+inttohex(rbuf,2)+" ";viewstring:="接受"+viewstring;memo1.lines.add(viewstring);memo1.lines.add("");end; 如果memo1上显示发送F0 01 FF FF 0F 和 接受F0 01 FF FF F0这表示串口已正确的发送出数据并正确的接受到数据,串口通讯成功。