Do DoEvents Loop Until MSComm1.OutBufferCount = 0 '等待,直到数据发送完毕———————————————————— 你sleep 500幹嗎?
以下是我在项目中的一个函数,应可以解决你的问题: Private Sub MoveBox(ByVal sSection As String, ByVal sAddress As String, ByVal sWay As String, ByVal sMode As String, ByVal COM As MSComm) On Error Resume Next Dim sBurlLayer As String Dim sDisplayBLAddress As String Dim i As Long Dim dStart As Date Dim dEnd As Date
Dim sCurMode As String '当前模式 Dim sCurWay As String '当前状态 Dim sCurAddress As String '当前地址
bIsStop = False sReturn = COM.Input sReturn = ""
If Trim(FilePosition.sBurl) = "" Then FilePosition.sBurl = "1" End If If Trim(FilePosition.sLayer) = "" Then FilePosition.sLayer = "1" End If If Trim(FilePosition.sAddress) = "" Then sDisplayBLAddress = GetAddress(sAddress) Else sDisplayBLAddress = FilePosition.sAddress End If sBurlLayer = Format(Right(FilePosition.sBurl, 1), "0") & Format(Right(FilePosition.sLayer, 1), "0")
' Sleep (100) ' Call SendData(sSection, sAddress, "n", ":s", COM) '允许 Sleep (100) Call SendData(sSection, sAddress, sWay, sMode, COM) dStart = Now Do Until bExit = True DoEvents: dEnd = Now i = (dEnd - dStart) * (10 ^ 5) If i > 140 Then ' Call SendData(sSection, sAddress, "y", ":s", COM) '允许 ' Sleep (100) ' Call SendData(sSection, sAddress, sWay, ":c", COM) '禁止 Unload frmMessage Screen.MousePointer = 0 bExit = True Else If COM.InBufferCount > 23 Then sReturn = COM.Input
好像不行,收不到数据
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.Output = data_out '发送数据,这里加上 sleep 500,可以收到数据
Do
DoEvents
Loop Until MSComm1.OutBufferCount = 0 '等待,直到数据发送完毕
好像不行,收不到数据
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.Output = data_out '发送数据,这里加上 sleep 500,可以收到数据
Do
DoEvents
Loop Until MSComm1.OutBufferCount = 0 '等待,直到数据发送完毕————————————————————
你sleep 500幹嗎?
Private Sub MoveBox(ByVal sSection As String, ByVal sAddress As String, ByVal sWay As String, ByVal sMode As String, ByVal COM As MSComm)
On Error Resume Next
Dim sBurlLayer As String
Dim sDisplayBLAddress As String
Dim i As Long
Dim dStart As Date
Dim dEnd As Date
Dim sCurMode As String '当前模式
Dim sCurWay As String '当前状态
Dim sCurAddress As String '当前地址
bIsStop = False
sReturn = COM.Input
sReturn = ""
If Trim(FilePosition.sBurl) = "" Then
FilePosition.sBurl = "1"
End If
If Trim(FilePosition.sLayer) = "" Then
FilePosition.sLayer = "1"
End If
If Trim(FilePosition.sAddress) = "" Then
sDisplayBLAddress = GetAddress(sAddress)
Else
sDisplayBLAddress = FilePosition.sAddress
End If
sBurlLayer = Format(Right(FilePosition.sBurl, 1), "0") & Format(Right(FilePosition.sLayer, 1), "0")
' Sleep (100)
' Call SendData(sSection, sAddress, "n", ":s", COM) '允许
Sleep (100)
Call SendData(sSection, sAddress, sWay, sMode, COM)
dStart = Now
Do Until bExit = True
DoEvents:
dEnd = Now
i = (dEnd - dStart) * (10 ^ 5)
If i > 140 Then
' Call SendData(sSection, sAddress, "y", ":s", COM) '允许
' Sleep (100)
' Call SendData(sSection, sAddress, sWay, ":c", COM) '禁止
Unload frmMessage
Screen.MousePointer = 0
bExit = True
Else
If COM.InBufferCount > 23 Then
sReturn = COM.Input
sCurMode = Mid(sReturn, 2, 1)
sCurWay = Mid(sReturn, 19, 1)
sCurAddress = Mid(sReturn, 5, 4)
If sCurMode = "c" Then
Screen.MousePointer = 0
bIsStop = True
bExit = True
End If
If sCurMode = "g" Then
Select Case sCurWay
Case "m" '门禁-有人进入
bExit = True
MsgBox "列 " & sCurAddress & " 门禁-有人进入"
bExit = True
Case "t" '时间到
bExit = True
MsgBox "列 " & sCurAddress & " 时间到"
Case "u" '超速
bExit = True
MsgBox "列 " & sCurAddress & " 超速"
Case "d" '电眼坏
bExit = True
MsgBox "列 " & sCurAddress & " 电眼坏"
Case "x"
bExit = True
Sleep (100)
Call SendData(sSection, sAddress, sWay, ":c", COM) '停止
MsgBox "列 " & sCurAddress & " 走廊有人"
Case "r" '复位
dStart = Now
j = 140 * 5
Sleep (100)
Call SendData(sSection, sAddress, sWay, sMode, COM)
Case Else
End Select
End If
End If
End If
Loop
Select Case Mid(sReturn, 2, 1)
Case "s"
MsgBox "已禁止"
Case "c"
If bDisplayBurlLayer = True Then
Sleep (100)
Call SendData(sBurlLayer, sDisplayBLAddress, sWay, ":j", COM) '显示打开的节与层
End If
MsgBox "已停止或打开到位"
Case Else
Call SendData(sSection, sAddress, "y", ":s", COM) '允许
Sleep (100)
Call SendData(sSection, sAddress, sWay, ":c", COM) '停止
' MsgBox "打开失败"
End Select
If bExit = True Then
Unload Me
End If
End Sub
N = MSComm1.InBufferCount
If N > 0 Then
MSComm1.InputLen = N
ReDim data_in(N) As Byte
data_in = MSComm1.Input
Do While True '数据接收
DoEvents
Sleep 100
If MSComm1.InBufferCount >= N Then Exit Do //感觉这里不对
Loop
End If