从串口发送过来的数据中取数据,数据的传递格式是这样的,以“A5”开头“5A”结尾,中间数据量不定,我也看了传递的规律,没有太多的规律,有时可一整条过来(里面即有“A5”也有“5A”),但有时只传一二个过来(里面只有“A5”,或什么都没有,或后面只有“5A”),也就是讲一条完整的信息有时是分几次传过来的,所以关键的处理就在这里。我的算法如下,但是有问题不知怎样的改二个全局变量:inputbuffer:array[0..1024] of char; //接收到的数据
recstr:string; //不完整数据累加数据私有变量
data:integer; //为实际收到的数据量
i:integer;
recmes:string;
begin
recstr:='';
if date>0 then
for i:=0 to data-1 do
begin
if inttohex(ord(inputbuffer[i]),2)<>'5A' then;
begin
recmes:=recmes+inttohex(ord(inputbuffer[n]),2)
end;
end; //开始对不完整数据进行判断,如不完整,就累加,不过我下面的算法明显有问题
但不知怎样改
if (pos('5A',recmes)>0) and (pos('A5',recmes)>0) then
begin
recstr:=recmes
end else
begin
recstr:=recstr+recmes;
end;
// showmessage(recstr);//
可就是得不到正确的数据 //接下来开始处理数据,取要的数据,..................
........................
end;
recstr:string; //不完整数据累加数据私有变量
data:integer; //为实际收到的数据量
i:integer;
recmes:string;
begin
recstr:='';
if date>0 then
for i:=0 to data-1 do
begin
if inttohex(ord(inputbuffer[i]),2)<>'5A' then;
begin
recmes:=recmes+inttohex(ord(inputbuffer[n]),2)
end;
end; //开始对不完整数据进行判断,如不完整,就累加,不过我下面的算法明显有问题
但不知怎样改
if (pos('5A',recmes)>0) and (pos('A5',recmes)>0) then
begin
recstr:=recmes
end else
begin
recstr:=recstr+recmes;
end;
// showmessage(recstr);//
可就是得不到正确的数据 //接下来开始处理数据,取要的数据,..................
........................
end;
如果没有,你的算法就是无用的。
一般地指令协议为:
STX CMD_NAME CMD_LENGTH CMD_DATA CMD_CRC ETX
STX:指令起始字节;指令头;
ETX:指令终止字节;指令尾;
if date>0 then
应为:
if data>0 then因为date返回的是当前日期,永远大写0