这样的算法怎样的实现,急?
我从串口接收数据在数据包‘28H’到‘54H’之间提取所要之内容的4个字节,但是明显算法不对,应当要实现这样的算法,才算是正确的,
首先是,
//开始提取数据
procedure tform1.wmcommnotify(var message:tmessage);
var
titem:tlistitem;
commstate:comstat;
dwnumberofbytesread:dword;
errorflag:dword;
inputbuffer:array[0..1024] of char; //静态数组
recstr:string;
i,n,m:integer;
begin
......
for n:=0 to m(串口要从文件或通信资源读取的字节数) do
begin
if inttohex(ord(inputbuffer[n]),2)= '28 ' then //inputbuffer为接
收到的字符
begin
i:=0;
recstr:= ' ';
while i <4 do
begin
i:=i+1;
recstr:=recstr+inttohex(ord(inputbuffer[n+i]),2);
end;
titem:=listview1.Items.Add;
titem.caption:=datetimetostr(date()+time());
titem.SubItems.Add(recstr);
end;
end;
这里当取到M个字节内的最后一个为‘54H’的字节时即中止FOR再向下面循环了,同时删除串口缓存内前面已提取的数据,这时程序又回到for循环的位置,进行下一次的循环,但是这里不知道怎样的写,有高手能指点一二吗,想了许久都不知,急啊!!!
end;
end;
setevent(post_event);
end;
[解决办法]
你可以先把所有收到的数据组合成一个字符串(每个字节间最好用空格隔开),然后就能判断28H’和‘54H’的位置,之后再用循环来找,不是很容易吗?