首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

VB 串口接收变长数组有关问题

2013-09-05 
VB 串口接收变长数组问题串口要接收的数据时不定长的,有可能是9个字节,也有可能是12个字节等等,我的思路是

VB 串口接收变长数组问题
串口要接收的数据时不定长的,有可能是9个字节,也有可能是12个字节等等,我的思路是采取一个字节然后判断处理一次,但不对,程序如下,请高手指点

'初始化
    intBuad = 19200
    RecvNum = 1

    With comISP
        '.CommPort = 1
        
        Select Case intBuad
        Case 9600
            .Settings = "9600,E,8,1"    '设置通信端口参数 9600赫兹、偶校验、8个数据位、1个停止位
        Case 19200
            .Settings = "19200,E,8,1"   '设置通信端口参数 19200赫兹、偶校验、8个数据位、1个停止位
        Case Else
            .Settings = "9600,E,8,1"    '设置通信端口参数 9600赫兹、偶校验、8个数据位、1个停止位.9600
        End Select
        
        .InBufferSize = 20             '设置缓冲区接收数据为40字节
        .InputLen = RecvNum             '设置Input一次从接收缓冲读取字节数为1
        '.PortOpen = True
        .InBufferCount = 0              '清除接收缓冲区
        .OutBufferCount = 0             '清除发送缓冲区
        .RThreshold = RecvNum           '设置接收1个字节就产生OnComm事件
        .InputMode = comInputModeBinary '接收字节为2进制模式
    End With

    RecvCount = 0
    Recving = False


'接收处理
Private Sub comISP_OnComm()
    Dim i As Integer
    Dim Sum As Integer
    Dim str As String
    
    str = "rx:"
    
    With comISP
        Select Case .CommEvent                  '判断通信事件


            Case comEvReceive:                  '收到Rthreshold个字节产生的接收事件
            
                Buffer = 0
                Buffer = comISP.Input

                Recv_Buf(RecvCount) = Buffer(0)
                
                If Recving = False Then
                    If Recv_Buf(RecvCount) = PAD_ADD Then
                        RecvCount = 0
                        Recv_Buf(RecvCount) = PAD_ADD
                        Recving = True
                    End If
                Else
                    RecvCount = RecvCount + 1

                    If (Recv_Buf(CMD_NUM) = &H4 And RecvCount = 8) _
                    Or (Recv_Buf(CMD_NUM) = &H6 And RecvCount = 8) _
                    Or (Recv_Buf(CMD_NUM) = &H10 And RecvCount = 11) Then
                        
                        Recving = False


                        For i = 0 To RecvCount - 1 Step 1
                            str = str & Right("00" & Hex(Recv_Buf(i)), 2) & " "
                        Next
                        txtRecv.Text = txtRecv.Text & str & vbCrLf
                        RecvCount = 0
                        If Recv_Buf(ADD_NUM) = PAD_ADD Then   '数据正确的话处理
                            comISP.InBufferCount = 0           '清除接收缓冲区
                                       
                            Select Case Recv_Buf(CMD_NUM)
                            Case &H4                            '读
                                If (Recv_Buf(REGH_NUM) = &H13) And (Recv_Buf(REGL_NUM) = &H2D) Then       '地址
                                    Add_Reply
                                ElseIf (Recv_Buf(REGH_NUM) = &H7) And (Recv_Buf(REGL_NUM) = &HE0) Then     '温度


                                    Temper_Reply
                                End If
                                
                            Case &H6                            '清总累
                                If (Recv_Buf(REGH_NUM) = &HA) And (Recv_Buf(REGL_NUM) = &H2F) Then        '清总累1
                                    Flow_Total = 0
                                    Clear_Total1_Reply
                                End If
                                
                            Case &H10                           '写
                                If (Recv_Buf(REGH_NUM) = &H13) And (Recv_Buf(REGL_NUM) = &H2F) Then        '写波特率
                                     Write_Buad_Reply


                                End If
                                
                            End Select
                        Else                                    '数据出错处理
                            RecvCount = 0
                            comISP.InBufferCount = 0           '清除接收缓冲区
                        End If
                    End If
                End If

        End Select
    End With
End Sub


[解决办法]
数组长度不用你管,你定义一个Variant变量接收数据就是了

dim vIN as varinat

vin=mscomm1.input
din l as long
l=ubound(vIN)+1'这就是接收的长度
[解决办法]
http://www.vbgood.com/thread-83700-1-1.html

热点排行