现想从串中读取数据并从里面取出来,现已作到能从串口读出数据(使用的是API函数的方式),但是不知怎样的取其中的数据,数据协议如下:
             
 客户系统     RS232   无线PDA系统客户系统->系统(发信请求通信协议)
Baud rate             Parity                 Word length             Stop bits
波特率                奇偶校验位            数据长度                数据停止位
1200                  none                     8                      128H PDA号(6byte右对齐)(用空格补足6byte) 20H 任务内容(不超过100 byte) 54H无线PDA系统->客户系统(收到发信请求反馈信息, 注:当寻呼接口收到后)05H (ACK)注: 28H , 54H , 20H(空格) , 05H 为 ASCII 码 ,其他为字符。数据信息数据读出代码如下:(采用了一个listview作表显示相应的提取数据)//数据接收消息处理函数
procedure tform1.wmcommnotify(var message:tmessage);
var
  titem:tlistitem;
  commstate:comstat;
  dwnumberofbytesread:dword;
  errorflag:dword;
  inputbuffer:array[0..1024] of char;    //静态数组
  recvstring:string;
  recstr,recno,recinfo,recmes:string;
  begin
    if not clearcommerror(hrecv,errorflag,@commstate) then
    begin
    messagebox(0,'clearcommerror !','notice',mb_ok);
    purgecomm(hrecv,purge_rxabort or purge_rxclear);
    exit;
    end;
    if (commstate.cbInQue>0) then
    begin
    fillchar(inputbuffer,commstate.cbInQue,#0);
    if (not readfile(hrecv,inputbuffer,commstate.cbInQue,dwnumberofbytesread,@read_os)) then
      begin
      errorflag:=getlasterror();
      if (errorflag<>0)and (errorflag<>error_io_pending) then
      begin
      receive:=false;
      raise exception.Create('读串口数据出错!!');
      end
      else
      begin
      waitforsingleobject(hrecv,infinite);
      getoverlappedresult(hrecv,read_os,dwnumberofbytesread,false);
      end;
      end;
      if dwnumberofbytesread>0 then
      begin
        read_os.Offset:=read_os.Offset+dwnumberofbytesread;
        inputbuffer[dwnumberofbytesread]:=#0;//其中inputbuffer即是接收的包,就在这里提取内容        //开始提取数据
        recstr:=copy(inputbuffer,1,pos(05H,inputbuffer));
        recinfo:=copy(recstr,pos(#28H,recstr),pos(#54H,recstr));
        recno:=copy(recinfo,pos(#28H,recinfo)+1,pos(#03H,recinfo)-1);
        recmes:=copy(recinfo,pos(#03H,recinfo)+1,pos(#54H,recinfo)-7);
        titem:=listview1.Items.Add;
        titem.caption:=strpas(recno);
        titem.SubItems.Add(strpas(recmes));  
        end;
        end;
        setevent(post_event);
        end;以上的数据的提取是否写的正确,里面有没有什么要注意的,
  1、比如是不是可能出现复的提取着一条数据,因为数据在提取之后在内存是否已删除,
  

解决方案 »

  1.   

    我没有用过aip,不过我觉得spcomm或者apro还是比api使用起来方便。
    procedure Tfrm_icread.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);var tmpArray:array[0..256] of Byte;
     ArraySize: DWORD;
     Count:DWORD;
     tmpStr:string;
     i:integer;
     pStr:PChar;
    begin  //-------------接受返回串口返回数据-----------
       pStr:=Buffer;
       tmpStr:=string(pStr);
       Dec(PStr);
       tempb:='';
       for i:=0 to bufferlength-1 do
       begin
         inc(PStr);
         tmpArray[i]:=Byte(PSTR^);
         tempb:=tempb+IntToHEX(Ord(tmpArray[i]),2);   end;
    end;
      

  2.   

    bufferlength这里怎么编译不过,我同时还要取内容,现在出现不同步,即第一个数据可以正确的取出,但是第二个开始,由于串口触发了,条件又不能满足,所以中间又漏掉许多信息,一下随机碰上了又取了一些出来,很困惑于此,不知怎样的逐一能将数据取出来我的代码如下://数据接收消息处理函数
    procedure tform1.wmcommnotify(var message:tmessage);
    var
      titem:tlistitem;
      commstate:comstat;
      dwnumberofbytesread:dword;
      errorflag:dword;
      inputbuffer:array[0..1024] of char;    //静态数组
      recvstring:string;
      recstr,recno,recinfo,recmes:string;
      i,n,m:integer;  begin//-------------接受返回串口返回数据----------- //开始提取数据
           recinfo:=copy(trim(inputbuffer),pos(#$28,trim(inputbuffer)),pos(#$54,trim(inputbuffer)));
           recno:=copy(trim(recinfo),2,pos(#$54,trim(recinfo))-3);      i:=0;
           recstr:='';
          while i<length(recno) do
           begin
             i:=i+1;
             recmes:=inttostr(ord(recno[i]));
               recstr:=recstr+recmes;
              end;
              titem:=listview1.Items.Add;
              titem.caption:=datetimetostr(date()+time());
              titem.SubItems.Add(recstr);