这是我的RS232编程部分源码,你参考一下:Private Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (ByVal lpEventAttributes As Long, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
...... Dim l As Long
l = CreateEvent(0, True, False, 0)
If l Then
olpRead.hEvent = l
l = CreateEvent(0, True, False, 0)
If l Then
olpWrite.hEvent = l
l = CreateEvent(0, True, False, 0)
If l Then
olpEvents.hEvent = l
Else
CloseHandle olpWrite.hEvent
GoTo LError1
End If
Else
LError1:
CloseHandle olpRead.hEvent
GoTo LError
End If
Else
LError:
Err.Raise vbObjectError + ERR_CREATEEVENT, MYNAME, "不能创建事件。"
End If
......
......Private Sub ReadLoop(ByVal WaitTime As Long)
Dim l As Long
If blnRead Then
If WaitForSingleObject(olpRead.hEvent, WaitTime) <> WAIT_TIMEOUT Then
blnRead = False
l = olpRead.InternalHigh
GoTo ReadComplated
End If
Else
Static b(32766) As Byte
If ReadFile(m_CommID, b(0), m_InBufferSize, l, olpRead) Then 'ReadComplated:
If l > 0 Then
strIn = strIn & LeftB$(b(), l)
m_CommEvent = comEvReceive
RaiseEvent OnComm
End If
Else
If Err.LastDllError = ERROR_IO_PENDING Then
blnRead = True
olpRead.InternalHigh = 0
Else
m_CommEvent = comEventDCB
RaiseEvent OnComm
End If
End If
End If
End Sub......
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
...... Dim l As Long
l = CreateEvent(0, True, False, 0)
If l Then
olpRead.hEvent = l
l = CreateEvent(0, True, False, 0)
If l Then
olpWrite.hEvent = l
l = CreateEvent(0, True, False, 0)
If l Then
olpEvents.hEvent = l
Else
CloseHandle olpWrite.hEvent
GoTo LError1
End If
Else
LError1:
CloseHandle olpRead.hEvent
GoTo LError
End If
Else
LError:
Err.Raise vbObjectError + ERR_CREATEEVENT, MYNAME, "不能创建事件。"
End If
......
......Private Sub ReadLoop(ByVal WaitTime As Long)
Dim l As Long
If blnRead Then
If WaitForSingleObject(olpRead.hEvent, WaitTime) <> WAIT_TIMEOUT Then
blnRead = False
l = olpRead.InternalHigh
GoTo ReadComplated
End If
Else
Static b(32766) As Byte
If ReadFile(m_CommID, b(0), m_InBufferSize, l, olpRead) Then 'ReadComplated:
If l > 0 Then
strIn = strIn & LeftB$(b(), l)
m_CommEvent = comEvReceive
RaiseEvent OnComm
End If
Else
If Err.LastDllError = ERROR_IO_PENDING Then
blnRead = True
olpRead.InternalHigh = 0
Else
m_CommEvent = comEventDCB
RaiseEvent OnComm
End If
End If
End If
End Sub......
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货