数组的下标越界是怎么回事?

hengkongshi 2008-10-16 05:02:05

If Read(LBound(Read)) = &HAA And Read(UBound(Read)) = &H55 Then
For i = 1 To 9
CRC = CRC + Read(i + 1)
Next i
CRC_B = CRC Mod 256
If Not Read(11) = CRC_B Then
Exit Sub
End If
Select Case Read(1)
Case &H51
If Read(3) > 128 Then
weitht_value = 0
Else
weight_value = Format((Read(3) * 256 + Read(4)) / 1000, "0.00")
End If
Text1.Text = weight_value
sngTemp(0, X) = weight_value
scale_value = (Read(5) * 256 + Read(6)) / 10
Text2.Text = scale_value
sngTemp(1, X) = scale_value
height_value = (Read(7) * 256 + Read(8)) / 10
Text3.Text = height_value
sngTemp(2, X) = height_value
rotate_value = Read(9) * 256 + Read(10)
'If (rotate_value Mod 360) >= 0 Then '需要考虑一下这里的情况说明了什么
sngTemp(3, X) = rotate_value 'Mod 360
'Else
'sngTemp(3, x) = (rotate_value Mod 360) + 360
'End If
Text4.Text = rotate_value
torque_value = (weight_value * 1000 + car_weight) * scale_value / 1000
Text5.Text = Format(torque_value, "0.00")
sngTemp(4, X) = Format(torque_value, "0.00")
If color_flag = True Then
num = num + 1
If num >= 5 Then
Shape1.BackColor = &HFF&
End If
Else
num = 0
Shape1.BackColor = &HFF00&
End If
...全文
190 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hengkongshi 2008-10-17
  • 打赏
  • 举报
回复
谢谢上面的朋友了。我试试……
fvflove 2008-10-16
  • 打赏
  • 举报
回复

Dim Read() As Byte
Dim CRC As Integer
Dim CRC_B As Byte
Dim w1, w2, w3, w4, w5 As Double
Dim fault_times As Integer
Dim temp As String
Sleep 30

'建议在此处增加语句
'ReDim Read(0) As Byte

With MSComm1
Select Case .CommEvent
Case comEvReceive
ReDim Read(.InBufferCount) As Byte '如果条件不满足,不会执行这句.
Read = .Input
.InBufferCount = 0
Case comEvSend
End Select
End With

'这儿有问题:
'如果上面我注释的那句没有执行,则 Read 数组是空.以下就有问题了.

If Read(LBound(Read)) = &HAA And Read(UBound(Read)) = &H55 Then
For i = 1 To 9
CRC = CRC + Read(i + 1)
Next i
CRC_B = CRC Mod 256
If Not Read(11) = CRC_B Then
Exit Sub
End If
Select Case Read(1)
Case &H51
If Read(3) > 128 Then
weitht_value = 0
Else
weight_value = Format((Read(3) * 256 + Read(4)) / 1000, "0.00")
End If
Text1.Text = weight_value
sngTemp(0, X) = weight_value
scale_value = (Read(5) * 256 + Read(6)) / 10
Text2.Text = scale_value
sngTemp(1, X) = scale_value
height_value = (Read(7) * 256 + Read(8)) / 10
Text3.Text = height_value
sngTemp(2, X) = height_value
rotate_value = Read(9) * 256 + Read(10)
'If (rotate_value Mod 360) >= 0 Then '需要考虑一下这里的情况说明了什么
sngTemp(3, X) = rotate_value 'Mod 360
'Else
'sngTemp(3, x) = (rotate_value Mod 360) + 360
'End If
Text4.Text = rotate_value
torque_value = (weight_value * 1000 + car_weight) * scale_value / 1000
Text5.Text = Format(torque_value, "0.00")
sngTemp(4, X) = Format(torque_value, "0.00")
If color_flag = True Then
num = num + 1
If num >= 5 Then
Shape1.BackColor = &HFF&
End If
Else
num = 0
Shape1.BackColor = &HFF00&
End If
color_flag = True

Picture1.Cls
X = X + 1

Open App.Path & "\备份\temp.txt" For Append As #1
If pre_time = str(Now) Then
fdsk = fdsk + 1
Else
fdsk = 1

End If
pre_time = Now
temp = str(Now) & "#" & Trim(str(fdsk))

Write #1, temp, weight_value, scale_value, height_value, rotate_value, torque_value
Close #1
' Timer3.Enabled = True
'Timer3.Interval = 3000
savecurr_data
Case &H40
MSComm1.Output = Read
Case Else
Exit Sub
End Select
If Check1.Value = 1 Then
Call Plot(spa_weight, 0)
End If
If Check2.Value = 1 Then
Call Plot(spa_scale, 1)
End If
If Check3.Value = 1 Then
Call Plot(spa_height, 2)
End If
If Check4.Value = 1 Then
Call Plot(spa_rotate, 3)
End If
If Check5.Value = 1 Then
Call Plot(spa_torque, 4)
End If
Picture2.Cls
w1 = Picture2.Width * weight_value / spa_weight

Picture2.Line (0, 0)-(w1, Picture2.Height), vbBlack, BF
Picture2.ToolTipText = "比例值为" & Format(Val(w1 / Picture2.Width), "0.00")
Picture3.Cls
w2 = Picture3.Width * scale_value / spa_scale
Picture3.ToolTipText = "比例值为" & Format(Val(w2 / Picture3.Width), "0.00")
Picture3.Line (0, 0)-(w2, Picture3.Height), vbRed, BF
Picture4.Cls
w3 = Picture4.Width * height_value / spa_height
Picture4.ToolTipText = "比例值为" & Format(Val(w3 / Picture4.Width), "0.00")
Picture4.Line (0, 0)-(w3, Picture4.Height), vbGreen, BF
Picture5.Cls
w4 = Picture5.Width * rotate_value / spa_rotate
Picture5.ToolTipText = "比例值为" & Format(Val(w4 / Picture5.Width), "0.00")
Picture5.Line (0, 0)-(w4, Picture5.Height), vbYellow, BF
Picture6.Cls
w5 = Picture6.Width * torque_value / spa_torque
Picture6.ToolTipText = "比例值为" & Format(Val(w5 / Picture6.Width), "0.00")
Picture6.Line (0, 0)-(w5, Picture6.Height), vbBlue, BF
End If

If data_flag = 4 Then
If fault_flag = False Then

data_flag = 0
End If
End If
data_flag = data_flag + 1
color_flag = Not color_flag

End Sub

hengkongshi 2008-10-16
  • 打赏
  • 举报
回复
重新沾一下代码:

Dim Read() As Byte
Dim CRC As Integer
Dim CRC_B As Byte
Dim w1, w2, w3, w4, w5 As Double
Dim fault_times As Integer
Dim temp As String
Sleep 30
With MSComm1
Select Case .CommEvent
Case comEvReceive
ReDim Read(.InBufferCount) As Byte
Read = .Input
.InBufferCount = 0
Case comEvSend
End Select
End With

If Read(LBound(Read)) = &HAA And Read(UBound(Read)) = &H55 Then
For i = 1 To 9
CRC = CRC + Read(i + 1)
Next i
CRC_B = CRC Mod 256
If Not Read(11) = CRC_B Then
Exit Sub
End If
Select Case Read(1)
Case &H51
If Read(3) > 128 Then
weitht_value = 0
Else
weight_value = Format((Read(3) * 256 + Read(4)) / 1000, "0.00")
End If
Text1.Text = weight_value
sngTemp(0, X) = weight_value
scale_value = (Read(5) * 256 + Read(6)) / 10
Text2.Text = scale_value
sngTemp(1, X) = scale_value
height_value = (Read(7) * 256 + Read(8)) / 10
Text3.Text = height_value
sngTemp(2, X) = height_value
rotate_value = Read(9) * 256 + Read(10)
'If (rotate_value Mod 360) >= 0 Then '需要考虑一下这里的情况说明了什么
sngTemp(3, X) = rotate_value 'Mod 360
'Else
'sngTemp(3, x) = (rotate_value Mod 360) + 360
'End If
Text4.Text = rotate_value
torque_value = (weight_value * 1000 + car_weight) * scale_value / 1000
Text5.Text = Format(torque_value, "0.00")
sngTemp(4, X) = Format(torque_value, "0.00")
If color_flag = True Then
num = num + 1
If num >= 5 Then
Shape1.BackColor = &HFF&
End If
Else
num = 0
Shape1.BackColor = &HFF00&
End If
color_flag = True

Picture1.Cls
X = X + 1

Open App.Path & "\备份\temp.txt" For Append As #1
If pre_time = str(Now) Then
fdsk = fdsk + 1
Else
fdsk = 1

End If
pre_time = Now
temp = str(Now) & "#" & Trim(str(fdsk))

Write #1, temp, weight_value, scale_value, height_value, rotate_value, torque_value
Close #1
' Timer3.Enabled = True
'Timer3.Interval = 3000
savecurr_data
Case &H40
MSComm1.Output = Read
Case Else
Exit Sub
End Select
If Check1.Value = 1 Then
Call Plot(spa_weight, 0)
End If
If Check2.Value = 1 Then
Call Plot(spa_scale, 1)
End If
If Check3.Value = 1 Then
Call Plot(spa_height, 2)
End If
If Check4.Value = 1 Then
Call Plot(spa_rotate, 3)
End If
If Check5.Value = 1 Then
Call Plot(spa_torque, 4)
End If
Picture2.Cls
w1 = Picture2.Width * weight_value / spa_weight

Picture2.Line (0, 0)-(w1, Picture2.Height), vbBlack, BF
Picture2.ToolTipText = "比例值为" & Format(Val(w1 / Picture2.Width), "0.00")
Picture3.Cls
w2 = Picture3.Width * scale_value / spa_scale
Picture3.ToolTipText = "比例值为" & Format(Val(w2 / Picture3.Width), "0.00")
Picture3.Line (0, 0)-(w2, Picture3.Height), vbRed, BF
Picture4.Cls
w3 = Picture4.Width * height_value / spa_height
Picture4.ToolTipText = "比例值为" & Format(Val(w3 / Picture4.Width), "0.00")
Picture4.Line (0, 0)-(w3, Picture4.Height), vbGreen, BF
Picture5.Cls
w4 = Picture5.Width * rotate_value / spa_rotate
Picture5.ToolTipText = "比例值为" & Format(Val(w4 / Picture5.Width), "0.00")
Picture5.Line (0, 0)-(w4, Picture5.Height), vbYellow, BF
Picture6.Cls
w5 = Picture6.Width * torque_value / spa_torque
Picture6.ToolTipText = "比例值为" & Format(Val(w5 / Picture6.Width), "0.00")
Picture6.Line (0, 0)-(w5, Picture6.Height), vbBlue, BF
End If

If data_flag = 4 Then
If fault_flag = False Then

data_flag = 0
End If
End If
data_flag = data_flag + 1
color_flag = Not color_flag

End Sub
dlmeijianyu 2008-10-16
  • 打赏
  • 举报
回复
lz的代码好像不全,if与endif不对称。还有,没有select case结束的地方。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧