[原创]下标越界?
Private Sub MSC_OnComm()Select Case
Case comEvReceive
Do
buf = MSC.Input
T = T + 1
If T > 10000 Then Exit Do
Loop Until (Left(buf, 1) = "=" And Len(buf) = 9)
‘保证buf是一个以 “=“开始的 9位数据,用do…loop进行循环等待。
ReDim ary(Len(buf))
For i = 1 To Len(buf)
ary(i - 1) = Mid(buf, i, 1)
Next ‘将buf重新定义为数组型
If ary(Len(buf) - 1) = "-" Then ‘判断buf的最高位是否为“-“
For i = 0 To Len(buf) - 1
buf1 = buf1 + ary(i)
Next
txtReceive1.Text = buf1 ‘如果为“-”,将赋值给txtReceive1.Text
buf1 = ""
Else
For i = 0 To Len(buf) - 1
buf1 = buf1 + ary(i)
Next
txtReceive2.Text = buf1 ‘否则将赋值给txtReceive2.Text
buf1 = ""
End If
Case comEvSend
End Select
End Sub
为什么在运行时,有的时候正常,有的时候在If ary(Len(buf) - 1) = "-" Then 这一句会出现错误提示:下标越界,
真的不知哪儿错了,想请高手帮帮忙!