wangxz5200 于 2015.06.15 17:28 提问

Private Sub MSComm1_OnComm()
Static cnt As Integer
Dim i As Integer
Dim Pr As Byte

Dim Buffer As Variant
Dim Arr() As Byte
Dim s As String

Select Case MSComm1.CommEvent

' 往暂存区存二进位资料
Buffer = MSComm1.Input
' 指定给位元组阵列以便处理
Arr = Buffer

s = ""
For i = 0 To receive_cnt - 1

If Arr(i) > 15 Then
s = s + Hex(Arr(i)) + " "

Else
s = s + "0" + Hex(Arr(i)) + " "

End If

cnt = cnt + 1

Next i

Text1.Text = Text1.Text & s

Erase Arr

If cnt >= 300 Then
Text1.Text = ""
cnt = 0
Exit Sub
End If

## Pr = Arr(11) + Arr(12) + Arr(13) + Arr(14) + Arr(15) + Arr(16) + Arr(17)

If Pr <> Arr(18) Then '校验正确
If Arr(17) = &H0 Then
Shape2.FillColor = RGB(0, 255, 0)
Shape3.FillColor = RGB(0, 255, 0)
Shape4.FillColor = RGB(0, 255, 0)
Shape5.FillColor = RGB(0, 255, 0)
Shape6.FillColor = RGB(0, 255, 0) '测试OK
Else
If Arr(17) And &H1 = &H1 Then '电压校准
Shape2.FillColor = RGB(0, 255, 0)
Else
Shape2.FillColor = RGB(255, 0, 0)
End If

`````` If Arr(17) And &H2 = &H2 Then    '电压测试
Shape3.FillColor = RGB(0, 255, 0)
Else
Shape3.FillColor = RGB(255, 0, 0)
End If

If Arr(17) And &H4 = &H4 Then   '写内部EEPROM
Shape4.FillColor = RGB(0, 255, 0)
Else
Shape4.FillColor = RGB(255, 0, 0)
End If

If Arr(17) And &H8 = &H8 Then   ' 脉冲检测
Shape5.FillColor = RGB(0, 255, 0)
Else
Shape5.FillColor = RGB(255, 0, 0)
End If

If Arr(17) And &H10 = &H10 Then   ' 电机开关
Shape6.FillColor = RGB(0, 255, 0)
Else
Shape6.FillColor = RGB(255, 0, 0)
End If
End If
``````

Else
MsgBox "校验失败，请重新测试"

End If

Case comEvSend
End Select
End Sub

4个回答

gamefinity   2015.06.15 20:29

frank_20080215   2015.06.16 07:34

Dim Arr() As Byte只是使用Redim之前的声明，表示该数组大小尚未定义，以后碰到Redim时，来确定数组的长度。

bdmh      2015.06.15 17:38

Arr本来有10个元素，你非要访问第11个元素，所以越界，断点调试，看看当时Arr有多少元素，你访问的对不对

danielinbiti      2015.06.15 17:38
`````` For i = 0 To receive_cnt - 1
If Arr(i) > 15 Then

For i = 0 To UBOUND(Arr)
If Arr(i) > 15 Then
``````
wangxz5200 什么意思
2 年多之前 回复