也许是这样的;
var
s: array[1..5] of byte;
begin
s[1]:=byte($7e);
s[2]:=byte($cc);
s[3]:=byte($01);
s[4]:=byte($cd);
s[5]:=byte($e7);
comm1.WriteCommData(@s[1],5);
可我接受的还是错误信息,当然错误信息是正确的接受了,
我的通信协议是这样的:
如果单片机认为我发送的错误就发给我:126 204 9 197 231,16进制为:7e cc 01 cd e7
空格是我自己加的。当然这个错误有可能是控件属性、通信协议、发错数据等等,
如果单片机认为我正确就会发给我:7e cc 10 dc e7 7e ee ac 00 00 00 dd dd 42 e7
发送应以16进制发送。
你很忙是吧!不好意思了!
procedure TreadThread.Execute;
begin
read_commdata(fcomhandle,fport);
end;
procedure read_commdata(comhandle:thandle;port:integer);
var
eventmask:dword;
Clear:Boolean;
Coms:Tcomstat;
cbNum,ReadNumber:cardinal;
lpErrors:cardinal;
Read_Buffer:array[1..200] of byte;
i:integer;
begin
eventmask:=0;
while true do
if (waitcommevent(comhandle,eventmask,overlapped)) then
if (eventmask=ev_rxchar) then
begin
Clear:=Clearcommerror(comhandle,lpErrors,@Coms);
if Clear Then
Begin
cbNum:=Coms.cbInQue;
if ReadFile(comhandle,Read_Buffer,cbNum,ReadNumber,overlapped) then
for i:=1 to cbnum do
begin
add_onereceivedata(port,read_buffer)
end;
end;
end;
end;
procedure send_data(comhandle:thandle;datacount:integer;dataarray:array of byte);
var
i:integer;
tempstring:string;
begin
tempstring:='';
for i:=0 to datacount-1 do
tempstring:=tempstring+inttohex(dataarray,0)+' ';
sendstring.Add(tempstring);
comwrite(comhandle,datacount,dataarray);
end;
//通讯口初始化
function init_comport(port:string;var comhandle:thandle;sbaud,sstop,sbyte,sparity:integer):boolean;
var
lpdcb:Tdcb;
begin
fillchar(overlapped,sizeof(toverlapped),0);
Comhandle:=CreateFile(PChar(Port),generic_read or generic_write,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if comhandle = INVALID_HANDLE_VALUE then
result:=false
else
begin
if not setupcomm(comhandle,100,100) then
begin
result:=false;
exit;
end;
getcommstate(comhandle,lpdcb);
lpdcb.baudrate:=sbaud;
lpdcb.StopBits:=sstop;
lpdcb.ByteSize:=sbyte;
lpdcb.Parity:=sparity;
if not Setcommstate(comhandle,lpdcb) then
begin
result:=false;
exit;
end;
if not setcommMask(comhandle,ev_rxchar) then
begin
result:=false;
exit;
end;
result:=true;
end;
end;
//通讯口发送数据
procedure comwrite(comhandle:integer;num:longint;buf:array of byte);
var
i:integer;
number:cardinal;
begin
number:=0;
for i:=0 to num-1 do
begin
writefile(comhandle,buf,1,number,overlapped);
end;
end;