之前用MSCOMM控件写的串口通信程序,串口设备是用的水木行(IRXON)的蓝牙转RS232,但每次用input读取数据时就显示comm 设备读错误 8020,实在是解决不了这个错误,后来就用API函数写,可是在用readfile函数进行读的时候还是不成功,readfile函数的返回值不为零,表示操作成功,但是readfile函数的第四个参数lpNumberOfBytesRead总是为零。真的是让我很无奈。不管使用MSCOMM控件还是API函数我直接用RS232线子都能正常通信。这是我第一次用API函数写串口通信,我用的是同步操作,以下是与串口相关的代码,大家帮忙看下哪里不对
'初始化端口
Public Function Init_Com(ComNumber As String) As Boolean
On Error GoTo handelinitcom
    Dim ComSetup As DCB, stat As COMSTAT, RetBytes As Long
    Dim retval As Long
    Dim CtimeOut As COMMTIMEOUTS, BarDCB As DCB
 
    ComNum = CreateFile(ComNumber, GENERIC_WRITE Or GENERIC_READ, 0, 0&, OPEN_EXISTING, 0, 0)
       
    If ComNum = -1 Then
        frmerror.Label1.Caption = "Please Choose COM Port!"
        frmerror.Visible = True
        Init_Com = False
        Exit Function
    End If
 
   ' 超时
    CtimeOut.ReadIntervalTimeout = 1
    CtimeOut.ReadTotalTimeoutConstant = 10
    CtimeOut.ReadTotalTimeoutMultiplier = 0
    CtimeOut.WriteTotalTimeoutConstant = 1
    CtimeOut.WriteTotalTimeoutMultiplier = 1
    retval = SetCommTimeouts(ComNum, CtimeOut)
    If retval = 0 Then
        retval = GetLastError()
        frmerror.Label1.Caption = "COM Port Error!"
        frmerror.Visible = True
        retval = CloseHandle(ComNum)
        Init_Com = False
        Exit Function
    End If
    
    retval = BuildCommDCB(Comsettings, BarDCB)
    If retval = 0 Then
        retval = GetLastError()
        frmerror.Label1.Caption = "COM Port Error!"
        frmerror.Visible = True
        retval = CloseHandle(ComNum)
        Init_Com = False
        Exit Function
    End If
    retval = SetCommState(ComNum, BarDCB)
    If retval = 0 Then
        retval = GetLastError()
        frmerror.Label1.Caption = "COM Port Error!"
        frmerror.Visible = True
        retval = CloseHandle(ComNum)
        Init_Com = False
        Exit Function
    End If
    Init_Com = True
handelinitcom:
    Exit Function
End Function'发送数据
Private Sub Timer_send_Timer()
On Error GoTo handelwritelpt
Dim RetBytes As Long
Dim retval As Long
Dim comerror As Long
Dim stat As COMSTAT
Dim sendMask As Long
  
   retval = PurgeComm(ComNum, PURGE_TXCLEAR)
   retval = WriteFile(ComNum, outdata(num_func, num_byte), 1, RetBytes, 0)'单字节发送
    
   If RetBytes Then
   
      num_byte = num_byte + 1
      If num_byte > 3 Then
         num_byte = 0
         num_func = num_func + 1
         If num_func > max_func_number Then
            num_func = 0
         End If
      End If
  End If
   
handelwritelpt:
    Exit Sub
End Sub'接收数据
Private Sub Timer_read_Timer()
On Error GoTo handelwritelpt
Dim i As LongDim RetBytes As Long
Dim retval As Long
Dim comerror As Long
Dim stat As COMSTAT
retval = ReadFile(ComNum, inbyte(0), 512, RetBytes, 0)
 If retval Then
    For i = 0 To (RetBytes - 1)
        data_oncomm(rev_ptr) = inbyte(i)
        rev_ptr = rev_ptr + 1
        If rev_ptr > oncomm_buffer Then
           rev_ptr = 0
        End If
    Next i
 Else
    Call ClearCommError(ComNum, comerror, stat)
 End IfIf data_ptr = rev_ptr Then
   communicate.FillColor = &HFF&
End IfDo While data_ptr <> rev_ptr
   Call pro_revdata
Loop
flash_num = flash_num + 1
If flash_num = 2 Then
   Call flash1
End If
If flash_num = 4 Then
   flash_num = 0
   Call flash2
   Call timenow
End If
handelwritelpt:
  Exit Sub
End Sub

解决方案 »

  1.   

    函数原型 
      BOOL ReadFile(   HANDLE hFile, //文件的句柄   LPVOID lpBuffer, //用于保存读入数据的一个缓冲区   DWORD nNumberOfBytesToRead, //要读入的字符数   LPDWORD lpNumberOfBytesRead, //指向实际读取字节数的指针   LPOVERLAPPED lpOverlapped //如文件打开时指定了FILE_FLAG_OVERLAPPED,那么必须,用这个参数引用一个特殊的结构。该结构定义了一次异步读取操作。否则,应将这个参数设为NULL   );
      

  2.   

    参考这里:
    http://www.photoshopsky.net/it/view-55280-1.html
      

  3.   

    我使用的vb啊,vb里是没有指针的,vb中的函数原型为
    Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long
    这里因为用同步操作,所以lpOverlapped参数设为long型。第三个参数lpNumberOfBytesRead也是long型,不是指针,返回实际读取的字符数。我遇到的问题就是readfile操作成功,但是lpNumberOfBytesRead的返回值为却为0
      

  4.   

    我在用readfile函数的时候也不是一直读不到,偶尔那么一下会读的到数据
      

  5.   

    之前看过http://www.photoshopsky.net/it/view-55280-1.html里的资料
    他在处理readfile是也是先判断readfile函数的返回值,看是否成功,然后判断第三个参数lpNumberOfBytesRead,看是否读出了数据,读出了就写到磁盘,没读出就不写。我现在是readfile的返回值不为零,即成功。getlasterror的返回值为零,表明操作确实成功。但是lpNumberOfBytesRead就是为零。
    查看蓝牙串口状态,串口是接受到了数据的。我就郁闷了,串口接收到了数据,为什么就读不出来了
      

  6.   

    http://download.csdn.net/source/1339593
      

  7.   

    应该是声明的参数问题。
    针对你的调用
    retval = ReadFile(ComNum, inbyte(0), 512, RetBytes, 0)
    Declare Function ReadFile Lib "kernel32" ( ... 
    ByRef lpOverlapped As Long ...
    '这会将常量 0 的地址作为参数压栈
    Declare Function ReadFile Lib "kernel32" ( ... 
    ByVal lpOverlapped As Long ...
    '这样才会将常量 0 的值作为参数压栈,等于 NULL 指针压栈API 接口属于“弱类型”,必须注意调用和声明的正确结合。
    仔细检查其他的声明。