我用API中的ReadFile函数读取串口收到的字符,但怎样才能获得已收到字符的个数呢?这个设大了读取出错,设小了数据读取又不完整,怎么办啊?记得好像是有这么个函数,但对API不是很熟悉!还请大家帮帮忙!

解决方案 »

  1.   

    var
      nErrMask   : DWORD;
      CommStat: ComStat;
    begin
      if ClearCommError( hCommFile, nErrMask, @CommStat ) then
      begin
        if CommStat.cbInQue <= 0 then;  { cbInQue 就是你当前缓存区的数据字节数量 }
      end;
      

  2.   

    function readdata(pub_handle: cardinal; var read_str: pchar): cardinal;
    var
      DUFF: array[0..50] of CHAR; //存放读出的数据放在缓冲区该缓冲区中
      xx: cardinal; //实际读取的字节数
      BCC: BOOLEAN;
      ss_xx, ss_yy: string;
      LAPPED: pOVERLAPPED;
    begin
      bcc := readfile(pub_handle, duff, 50, XX, nil);
      if not bcc then
      begin
        result := 0;
        read_str := '';
      end else //not bcc
      begin
        if xx > 0 then
        begin
          ss_yy := duff;
          ss_xx := copy(ss_yy, 1, xx);
          read_str := pchar(ss_xx);
          result := xx;
          duff := '';
        end else //xx>0  表示有数据读出
        begin
          read_str := '';
          result := 0;
          duff := '';
        end;
      end; //not bcc
    end;
      

  3.   

    我用cbInQue,值始终是0,怎么回事啊?
      

  4.   

    自己搞定,引用了cport里的一段代码