To cuteant(我这张旧船票还能否登上你的破船|涛声是否依旧) 我用的是spcomm,但是不知道应该怎么做。你能否放个例子上来阿?非常感谢!
贴全部代码不方便,不适合你的,协议是自己定的,我说一下大概过程吧,我是这样做的:在声明部分定义这样一个结构体: TRecData=record comm:byte; len:integer; data:array[0..255]of byte; end; 然后定义两个全局变量 recData:TRecData; //收到的数据 sendData:TRecData;//待发送的数据比如说发送握手命令的过程commSendStatus是这样写的: procedure tform1.commSendStatus; begin sendData.comm:=$60; sendData.len:=0; self.commSend; end;然后就是commSend过程了,这样写的: procedure tform1.commSend; var CRC:byte; i:integer; s:string; begin comm1.SendByte($ff); comm1.SendByte($ff); comm1.SendByte($ff); CRC:=$81; comm1.SendByte($81); CRC:=CRC+sendData.comm; comm1.SendByte(sendData.comm); CRC:=CRC+sendData.len; comm1.SendByte(sendData.len); comm1.SendData(@sendData.data[0],sendData.len); for i:=0 to sendData.len-1 do CRC:=CRC+sendData.data[i]; comm1.SendByte(-CRC); RichEdit1.SelAttributes.Color:=clBlue; if sendData.Len=0 then RichEdit1.Lines.Add(trans(msgIndex)+': Send: Command='+intToHexFormat(sendData.comm)+' Len='+intToStr(sendData.Len)+'('+FormatDateTime('tt', now)+')') else begin s:=''; for i:=0 to sendData.len-1 do if (sendData.comm=2) or (sendData.comm=4) or (sendData.comm=10) then s:=s+intToHex(sendData.data[i],2) else s:=s+intToStr(sendData.data[i]); RichEdit1.Lines.Add(trans(msgIndex)+': Send: Command='+intToHexFormat(sendData.comm)+' Data='+s+' Len='+intToStr(sendData.Len)+'('+FormatDateTime('tt', now)+')'); end; end;仅供参考吧,commSend过程是自定义的协议,所以比较乱,发送一个字节是用comm1.SendByte
我用的是spcomm,但是不知道应该怎么做。你能否放个例子上来阿?非常感谢!
TRecData=record
comm:byte;
len:integer;
data:array[0..255]of byte;
end;
然后定义两个全局变量
recData:TRecData; //收到的数据
sendData:TRecData;//待发送的数据比如说发送握手命令的过程commSendStatus是这样写的:
procedure tform1.commSendStatus;
begin
sendData.comm:=$60;
sendData.len:=0;
self.commSend;
end;然后就是commSend过程了,这样写的:
procedure tform1.commSend;
var
CRC:byte;
i:integer;
s:string;
begin
comm1.SendByte($ff);
comm1.SendByte($ff);
comm1.SendByte($ff);
CRC:=$81;
comm1.SendByte($81);
CRC:=CRC+sendData.comm;
comm1.SendByte(sendData.comm);
CRC:=CRC+sendData.len;
comm1.SendByte(sendData.len);
comm1.SendData(@sendData.data[0],sendData.len);
for i:=0 to sendData.len-1 do
CRC:=CRC+sendData.data[i];
comm1.SendByte(-CRC);
RichEdit1.SelAttributes.Color:=clBlue;
if sendData.Len=0 then
RichEdit1.Lines.Add(trans(msgIndex)+': Send: Command='+intToHexFormat(sendData.comm)+' Len='+intToStr(sendData.Len)+'('+FormatDateTime('tt', now)+')')
else
begin
s:='';
for i:=0 to sendData.len-1 do
if (sendData.comm=2) or (sendData.comm=4) or (sendData.comm=10) then
s:=s+intToHex(sendData.data[i],2)
else
s:=s+intToStr(sendData.data[i]);
RichEdit1.Lines.Add(trans(msgIndex)+': Send: Command='+intToHexFormat(sendData.comm)+' Data='+s+' Len='+intToStr(sendData.Len)+'('+FormatDateTime('tt', now)+')');
end;
end;仅供参考吧,commSend过程是自定义的协议,所以比较乱,发送一个字节是用comm1.SendByte