以下是一个跟字符叠加器的通讯,使用ComPort控件,你参考一下。
以下代码是指电脑串口有数据到时的处理过程:
procedure TFormqt_sale.ComPort1RxChar(Sender: TObject; Count: Integer);
var
Str: String;
Send_byte : array[0..2] of byte;
begin
ComPort1.ReadStr(Str, Count);
if Str = chr($55) then //等待命令
begin
label_zfdj.Caption := '等待命令';
end
else
if Str = chr($77) then //等待接收
begin
if iFlag <= 27 then //1..27的字符
begin
label_zfdj.Caption := '发送' + inttostr(iFlag);
Send_byte[0] := iFlag;
Send_byte[1] := sendstring[iFlag];
Send_byte[2] := sendstring[iFlag];
AddNum := AddNum + sendstring[iflag] + iFlag;
iFlag := iFlag + 1;
Comport1.Write(Send_byte,3);
end
else
begin //传送校验位
AddNum := AddNum and 255;
label_zfdj.Caption := '校验';
Send_byte[0] := $A7;
Send_byte[1] := AddNum;
Send_byte[2] := AddNum;
Comport1.Write(Send_byte,3);
end;
end
else
if str = chr($A7) then //校验错误,重发
begin
label_zfdj.Caption := '校验错误';
iFlag := 1;
end;
end;