单步调试没有问题,四路都可以显示
但自动运行,只能显示第一路和第四路。郁闷!程序代码如下:
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

解决方案 »

  1.   

    1)如果按照你的操作方式,串口发送后需要延时,如
                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
                    ……
      

  2.   

    2)建议在发送前清接收缓冲,防止在实际应用中出现串扰
        ……
        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控件再深入了解一下,还有很多应用。