串口通信 单片机上传96组数给text1(i)显示
我的目的是这样的:单片机上传96组双字节的数,大小介于0-4400之间,于是我定义arr(196) Byte型数组来接收(二进制模式接收),再通过数据转换付给cellvol(96) integer型数组,但是实现不了,在监视框里能看到arr()数组里的数据,可是该处cellvol(j) = arr(2 * j) * 256 + arr(2 * j + 1)显示下标越界,不知为什么?
不知到这样将BYTE型移位相加后付值给 INTEGER型 行不行?
请高手指教:
源代码如下:
Private Sub Form_Load()
MSComm1.CommPort = 1
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
Else
MsgBox ("先打开串行口!")
End If
MSComm1.RThreshold = 192
MSComm1.SThreshold = 0
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary
MSComm1.InPutLen = 192
End Sub
Private Sub SendCommand2_Click()
Dim outputdata As Variant
outputdata = Text2.Text
MSComm1.Output = outputdata
End Sub
Private Sub MSComm1_OnComm()
Dim InPutLen As Integer
Dim i As Integer
Dim j As Integer
Dim arr() As Byte
Dim cellvol() As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
i = MSComm1.InBufferCount
ReDim arr(i)
arr = MSComm1.Input
For j = 0 To (95)
cellvol(j) = arr(2 * j) * 256 + arr(2 * j + 1) 该处现实下标越界
Text1(j).Text = Hex(cellvol(j))
Next j
End Select
End Sub --------------------编程问答-------------------- 改成:
cellvol(j) = arr(2 * j)
cellvol(j) = cellvol(j)* 256 + arr(2 * j + 1)
试一下。
在调试窗口中输入:
?ubound(arr)
然后回车看一下,下标的最大值,看是否真的越界。 --------------------编程问答--------------------
Option Explicit
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.RThreshold = 192
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary
MSComm1.InPutLen = 0
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
Else
MsgBox ("先打开串行口!")
End If
End Sub
Private Sub SendCommand2_Click()
Dim outputdata As Variant
outputdata = Text2.Text
MSComm1.Output = outputdata
End Sub
Private Sub MSComm1_OnComm()
Dim j As Integer
Dim arr() As Byte
Dim cellvol() As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
ReDim cellvol(192 / 2 - 1)
arr = MSComm1.Input
For j = 0 To (95)
cellvol(j) = arr(2 * j) * 256 + arr(2 * j + 1)
Print cellvol(j)
Text1(j).Text = Hex(cellvol(j))
Next j
End Select
End Sub
补充:VB , 基础类