现在有个项目是与下位机进行串口通信的,当VB发送命令过去后返回一串数据,返回的数据长度是不同的,当 我把MSComm1.RThreshold设为一定长度时,
要么就是达不到长度MSComm1_OnComm不触发,要么就是数据太长接收不完整。 在网上下了个串口调试软件,它就可以做到不定长度时都可以接收完全,不知
是怎么实现的,各位能否给个原理或代码看看,最好给个代码看看,谢谢!

解决方案 »

  1.   

    设置MSComm1.RThreshold = 1
    不同字节长返回数据需通过数据处理代码按通信协议编写。
      

  2.   

    zdingyun,你好!你所说的不同字节长返回数据需通过数据处理代码按通信协议编写,问题就是设置MSComm1.RThreshold = 1 后MSComm1_OnComm事件在缓冲中长度达到7位时就已触发了,后面的数据没办法获取了呀?
      

  3.   


    Option Explicit
        Dim strData As StringPrivate Sub Form_Load()
        Dim port As Integer
        port = 1
        MSComm1.CommPort = port                   'COM端口
        MSComm1.Settings = "9600,n,8,1"
        MSComm1.InputMode = comInputModeBinary      '采用二进制传输
        MSComm1.InBufferCount = 0   '清空接受缓冲区
        MSComm1.OutBufferCount = 0  '清空传输缓冲区
        MSComm1.RThreshold = 1      '产生MSComm事件
        MSComm1.InBufferSize = 1024
        MSComm1.PortOpen = True
    End SubPrivate Sub MSComm1_OnComm() '接收数据
        Dim BytReceived() As Byte
        Dim strBuff As String
        Select Case MSComm1.CommEvent
            Case 2
                MSComm1.InputLen = 0
                strBuff = MSComm1.Input
                BytReceived() = strBuff
                Dim i As Integer
                For i = 0 To UBound(BytReceived) '接收数据处理为16进制
                    If Len(Hex(BytReceived(i))) = 1 Then
                        strData = strData & "0" & Hex(BytReceived(i))
                    Else
                        strData = strData & Hex(BytReceived(i))
                    End If
                Next
                Text1 = strData
                '数据处理代码
        End Select
    End Sub
      

  4.   

    郁闷,昨天怎么搞也不行,刚我看了zdingyun的代码,然后再看看自己的代码也没发现什么错误,现调试既然OK了.
    Private Sub Form_Load()On Error GoTo Err1
        If MSComm1.PortOpen = False Then
        
            MSComm1.CommPort = 1   'COM端口
            MSComm1.Settings = "19200,n,8,1"
            MSComm1.InputMode = comInputModeBinary      '采用二进制接收
            MSComm1.InBufferCount = 0   '清空接受缓冲区
            MSComm1.OutBufferCount = 0  '清空传输缓冲区
            MSComm1.InBufferSize = 1024 '接收缓冲区大小
            MSComm1.OutBufferSize = 1024 '发送缓冲区大小
            MSComm1.PortOpen = True '打开通信口
            MSComm1.RThreshold = 1      '设置引发OnComm事件的字节长度
            MSComm1.InputLen = 0 '设置Input从接收缓冲读取全部数据
           
        End If
        
    Exit Sub
    Err1:
       Select Case Err.Number   '串口被其它程序打开的出错提示
         Case 8005
          MsgBox "串口打开错误,可能串口被其它程序打开,请关闭其它程序再打开.", vbInformation
         Case Else
          MsgBox "程序出错:" & "出错代码. " & Err.Number & ": 出错提示信息: " & Err.Description, vbInformation
       End Select
    End SubPrivate Sub MSComm1_OnComm()
            Dim card     As String
            Dim indata     As Variant    '保存串口数据
            Dim BytesReceived()     As Byte
            Dim ReceiveLen     As Byte        Select Case MSComm1.CommEvent
                    Case comEvReceive
                    MSComm1.InputMode = comInputModeBinary
                    MSComm1.InputLen = 0                indata = MSComm1.Input
                    
                    
                    ReDim BytesReceived(UBound(indata)) As Byte                
                    BytesReceived() = indata                                     '将数据转入BYTE中                card = StrConv(BytesReceived, vbUnicode)
                    TxtSend.Text = TxtSend.Text & card
                    MSComm1.InBufferCount = 0                   '清除接收缓冲区
                    
            End Select
    End Sub