Public Function COM_Read_Byte(hWnd As Long) As Byte()
Dim recComState As COMSTAT
Dim ErrorFlag As Long, t As Long
Dim lResult As Long, dwRes As Long
Dim Get_Data() As Byte
Dim NumToRead As Long
Dim NumhaveRead As Long
If hWnd <= 0 Then Exit Function
t = GetTickCount '- 这里要怎么判断数据已经全部进来? -' Do Call ClearCommError(hWnd, ErrorFlag, recComState) ' If recComState.cbInQue > 0 Then Exit Do '这是超时时间' If GetTickCount - t > 10 Then Exit Do Loop Until recComState.cbInQue > 0
If recComState.cbInQue > 0 Then '取得接收缓存区内没有被读取的字节数 ReDim Get_Data(recComState.cbInQue - 1) NumToRead = recComState.cbInQue lResult = ReadFile(hWnd, Get_Data(0), NumToRead, NumhaveRead, ByVal 0&) End If
COM_Read_Byte = Get_Data '清空' If recComState.cbInQue > 0 Then PurgeComm hWnd, PURGE_RXABORT Or PURGE_RXCLEAR