从串口发送过来的数据中取数据,数据的传递格式是这样的,以“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(recst);//   
        可就是得不到正确的数据       //接下来开始处理数据,取要的数据,..................
          ........................
          end;

解决方案 »

  1.   

    私有变量
      data:integer;  //为实际收到的数据量
      i:integer;
      recmes:string;
     begin
      recstr:='';    ***********是要处理的不完整数据包,为什么一开始就初始化,如果这样,在第二次的不完整数据包来时,上一次的数据包不是被丢失了。应是recmes吧?
      if date>0 then
         for i:=0 to data-1 do
    ....................................
      

  2.   

    只是有一个开始,一个结束的标志是做不到对中间分次发送的数据和中间还有开始或者结束符号的数据进行分拆的目的的吧。
    你看tcp/ip协议,就有N多标志位的啊