单步调试没有问题,四路都可以显示
但自动运行,只能显示第一路和第四路。郁闷!程序代码如下:
Private Sub Timer1_Timer()
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
For i = 1 To 4
x(0) = 128 + i
x(1) = 128 + i
x(2) = 82
x(3) = 0
x(4) = 0
x(5) = 0
x(6) = 82 + i
x(7) = 0
MSComm1.Output = x
If MSComm1.InBufferCount >= 10 Then
s = MSComm1.Input
Label1(i - 1).Caption = Str$(s(0)) 'Label1.Caption + Str$(s(i))
MSComm1.InBufferCount = 0
End If
Next
End Sub
但自动运行,只能显示第一路和第四路。郁闷!程序代码如下:
Private Sub Timer1_Timer()
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
For i = 1 To 4
x(0) = 128 + i
x(1) = 128 + i
x(2) = 82
x(3) = 0
x(4) = 0
x(5) = 0
x(6) = 82 + i
x(7) = 0
MSComm1.Output = x
If MSComm1.InBufferCount >= 10 Then
s = MSComm1.Input
Label1(i - 1).Caption = Str$(s(0)) 'Label1.Caption + Str$(s(i))
MSComm1.InBufferCount = 0
End If
Next
End Sub
Public Declare Function timeGetTime Lib "winmm.dll" () As Long ……
MSComm1.Output = OutByte
t = timeGetTime()
Do
DoEvents
Loop Until timeGetTime - t > 100 Or MSComm1.InBufferCount >= 10
t = timeGetTime()
Do
DoEvents
Loop Until timeGetTime - t > 20
……
……
If MSComm1.InBufferCount > 0 Then MSComm1.InBufferCount = 0 ' 加上这句
MSComm1.Output = OutByte
……3)另一种常用思路是,设置oncomm事件触发接收,这样就不需软件延时考虑了
……
If MSComm1.InBufferCount > 0 Then MSComm1.InBufferCount = 0
MSComm1.RThreshold = 10
MSComm1.Output = OutByte
…… Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
' 以下处理接收 case else
end select
end sub4)建议对mscomm控件再深入了解一下,还有很多应用。