使用SPCOMM控件,网上搜索自己下载, Comm1.CommName:=Trim(Comprot.Text); //串口号 Comm1.BaudRate:=strtoint(Trim(Comrate.text));//波特率 Comm1.Parity:=None;// 发送端:var buff:array[1..8] of byte; begin Comm1.StartComm; sleep(100); buff[1]:=$00; buff[2]:=$00; buff[3]:=$00; buff[4]:=$00; buff[5]:=$00; buff[6]:=$00; CRC16(@buff,6); buff[7]:=crc1; buff[8]:=crc2; if not comm1.writecommdata(@buff,8) then begin ... end ... end 接收端:procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); var i:integer; viewstring:string; buff: array[0..19] of Byte;
begin Sleep(100); viewstring:=''; CopyMemory(@buff,buffer,BufferLength); memo1.lines.add('温度:'+floattostr(((Buff[3] * 256)+(Buff[4] mod 256))*0.1)+'湿度:'+floattostr(((Buff[5] * 256)+(Buff[6] mod 256)) * 0.1)+'状态:'+inttohex(Buff[7],2)); for i:=8 to 19 do begin viewstring:=viewstring+inttohex(Buff[i],2)+' '; end; ; Comm1.StopComm; end;
Comm1.CommName:=Trim(Comprot.Text); //串口号
Comm1.BaudRate:=strtoint(Trim(Comrate.text));//波特率
Comm1.Parity:=None;//
发送端:var
buff:array[1..8] of byte;
begin
Comm1.StartComm;
sleep(100);
buff[1]:=$00;
buff[2]:=$00;
buff[3]:=$00;
buff[4]:=$00;
buff[5]:=$00;
buff[6]:=$00;
CRC16(@buff,6);
buff[7]:=crc1;
buff[8]:=crc2; if not comm1.writecommdata(@buff,8) then
begin
...
end
...
end
接收端:procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
viewstring:string;
buff: array[0..19] of Byte;
begin
Sleep(100);
viewstring:='';
CopyMemory(@buff,buffer,BufferLength);
memo1.lines.add('温度:'+floattostr(((Buff[3] * 256)+(Buff[4] mod 256))*0.1)+'湿度:'+floattostr(((Buff[5] * 256)+(Buff[6] mod 256)) * 0.1)+'状态:'+inttohex(Buff[7],2)); for i:=8 to 19 do
begin
viewstring:=viewstring+inttohex(Buff[i],2)+' ';
end;
;
Comm1.StopComm;
end;