864
社区成员




Function mWait(chrCount As Long, Optional TimerOut As Long = 0) As Long
'等待mscomm1的接收缓冲区至少有chrCount个字符
Dim Count As Long
Dim t As Double, EnterT As Double, mT As Double
mT = IIf(TimerOut = 0, 2, TimerOut)
'debug.Print TimerOut
If TimerOut <> 0 Then Stop
EnterT = Timer
Count = MSComm1.InBufferCount
Do While Not (Count >= chrCount Or t > mT)
DoEvents
Count = MSComm1.InBufferCount
t = Timer - EnterT
Loop
mWait = IIf(t > mT, 1, 0)
End Function
是用OnComm的代码:
Dim WithEvents Com1 As MSComm
Dim Com1SendStr() As Byte'需要发送的字节数组
Dim Com1SendP As Integer'发送的字节在数组的位置
Dim Com1SendCount As Integer'发送的数组的长度
Dim Com1RevN As Integer'命令返回的自己数量
Dim Com1InBuf() As Byte'输入缓冲区数组,接收用
Dim Com1OutBuf() As Byte'输出缓冲区
With Com1
Select Case Com1.CommEvent
' 事件
Case comEvReceive ' 收到 RThreshold # of chars.已经设置为1
If .RThreshold = 1 Then
If Com1SendP <> Com1SendCount Then'返回的是单字节,命令发送未完成
Com1SendP = Com1SendP + 1
If Com1SendP = Com1SendCount Then
.RThreshold = Com1RevN
End If
Com1OutBuf(0) = Com1SendStr(Com1SendP)
.Output = Com1OutBuf
Else
End If
Else
.InputLen = 0
Com1InBuf = .Input
.RThreshold = 1
'GetAline函数,返回下一个准备发送的命令
'Com1SendString过程,准备发送GetALine返回的字符串所需要的一些准备,3表示命令发送成功后会额外返回3个字节
Com1SendString GetALine, 3
End If
End Select
End With
使用OnComm后,程序逻辑不严谨,偶尔会出现发送中断的问题。
但是,从速度上讲,还是很慢。并没有比mWait快什么。