当前位置:编程学习 > VB >>

串口通信,字节数组下标越界的问题

小弟刚学串口通信,想做一个控制注射泵的程序。在读取注射泵参数时,发现当收到的数据小于8个字符时正常;当大于8个字符时,显示readbyte()下标越界。
就是代码中"灌注"和"抽取"模式下,执行CLng(readbyte(i + 2)), CLng(readbyte(i + 3))时正常,但是执行到CLng(readbyte(i + 4))时就显示下标越界。
例如,文本框中收到233 1 9 82 84 1  25 34 7 36 78 14 121,  i=4时readbyte(i)=84,后面的1 25 34 都能读到,可是那个7就读不到,显示下标越界。 想不通是为什么。。。各位高手帮帮忙啊,到底是哪里错了?

Private Sub MsComm_OnComm()
Dim buffer As Variant
Dim readByte() As Byte
Dim i As Integer

Select Case MsComm.CommEvent
  Case comEvReceive
    buffer = MsComm.Input
    readByte = buffer
End Select

For i = 0 To UBound(readByte)
  Text1.Text = Text1.Text & " " & readByte(i)
Next

For i = 0 To UBound(readByte)
    If readByte(i) = 84 Then 
          Select Case readByte(i + 1)
            Case 1
              rdMode = "灌注"
              rdInjVol = rdVolConvert(CLng(readbyte(i + 2)), CLng(readbyte(i + 3)), CLng(readbyte(i + 4)))
              
            Case 2
              rdMode = "抽取"
              rdDrawVol = Str(rdVolConvert(CLng(readbyte(i + 2)), CLng(readbyte(i + 3)), CLng(readbyte(i + 4))))
             
          End Select

    End If
Next
End Sub 

请参考:
http://download.csdn.net/source/1262066

Option Explicit

Private Sub Form_Load()
    With MSComm1
        .CommPort = 1
        .Settings = "9600,n,8,1"
        .InputMode = comInputModeBinary
        .RThreshold = 1
        .InputLen = 1
    End With
End Sub

Private Sub MSComm1_OnComm()
    Dim buffer As Variant
    Dim readByte(100) As Byte
    Dim i As Integer
    Select Case MSComm.CommEvent
        Case comEvReceive
            MSComm1.RThreshold = 0
            For i = 1 To MSComm1.InBufferCount
                buffer = Null
                buffer = MSComm1.Input
                readByte(i) = buffer(0)
            End If
            MSComm1.RThreshold = 1
    End Select
    
    For i = 0 To UBound(readByte)
        If readByte(i) <> "" Then Text1.Text = Text1.Text & " " & readByte(i)
    Next
    
    For i = 0 To UBound(readByte)
        If readByte(i) = 84 Then
            Select Case readByte(i + 1)
                Case 1
                    rdMode = "灌注"
                    rdInjVol = rdVolConvert(CLng(readByte(i + 2)), CLng(readByte(i + 3)), CLng(readByte(i + 4)))
                Case 2
                    rdMode = "抽取"
                    rdDrawVol = Str(rdVolConvert(CLng(readByte(i + 2)), CLng(readByte(i + 3)), CLng(readByte(i + 4))))
            End Select
      End If
    Next

End Sub

引用 1 楼 veron_04 的回复:
请参考:
http://download.csdn.net/source/1262066

VB code

Option Explicit

Private Sub Form_Load()
    With MSComm1
        .CommPort = 1
        .Settings = "9600,n,8,1"
        .InputMode = ……


这位大哥能解释下我那个哪里出问题吗,我有点看不明白。。。
For i = 0 To UBound(readByte)'i最大可能是readByte的上届,那么i+1就访问越界了,你还用了i+4
If readByte(i) = 84 Then
if i + 4 > UBound(readByte) then Exit For'避免访问越界,因为i可能指向上届,则i+1不存在了,例如一共读了9个字节,当前i=8,则0~8已经是所有数据了,你访问i+1个位置当然访问越界了,同理下面的i+2,i+3,i+4
Select Case readByte(i + 1)
Case 1
rdMode = "灌注"
rdInjVol = rdVolConvert(CLng(readbyte(i + 2)), CLng(readbyte(i + 3)), CLng(readbyte(i + 4)))
Case 2
rdMode = "抽取"
rdDrawVol = Str(rdVolConvert(CLng(readbyte(i + 2)), CLng(readbyte(i + 3)), CLng(readbyte(i + 4))))
End Select
End If
Next
问题出在OnComm事件中,你仔细对比一下代码.还有那个Rth...属性要设置为1最好.
巴萨2:0皇马!!!!!哈哈哈 问题出在OnComm事件中,你仔细对比一下代码.还有那个Rth...属性要设置为1最好.
巴萨2:0皇马!!!!!哈哈哈 For i = 0 To UBound(readByte) - 4

这样就不会越界了。
谢谢楼上各位!
问题已解决了,是OnComm事件执行比串口接收数据快的原因,接收这一串数据产生了两次OnComm事件,囧。。。
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,