vb程序总是提示下标越界,高手帮帮忙啊!

PrivateDeclareSubSleepLib"kernel32"(ByValdwmillisecondsAsLong)PrivateSubCommand1_Clic... Private Declare Sub Sleep Lib "kernel32" (ByVal dwmilliseconds As Long)
Private Sub Command1_Click()
MSComm1.CommPort = Val(Text1.Text)
MSComm1.PortOpen = True
If MSComm1.PortOpen = True Then
Picture1.BackColor = vbRed
End If
End Sub

Private Sub Command2_Click()
MSComm1.PortOpen = False
If MSComm1.PortOpen = False Then
Picture1.BackColor = vbBlack
End If
End Sub

Private Sub Command3_Click()
Dim a(1) As Byte
Dim b() As Byte
Dim s As String

a(0) = 0
a(1) = 15
MSComm1.Output = a
Sleep (30)
b = MSComm1.Input
s = CStr(b(0))
Text2.Text = s
End Sub

Private Sub Command4_Click()
On Error Resume Next
Dim a(1) As Byte
Dim b() As Byte
Dim s As String
Dim l(18) As Long
Dim u(18) As Single
Dim i As Integer
Dim VREF As Single
VREF = 2.495

MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.InputLen = 38

a(0) = CByte(Val(Text2.Text))
a(1) = 15

MSComm1.Output = a
Sleep (50)
b = MSComm1.Input
i = 1
For i = 1 To 18

l(i - 1) = CLng(b(i * 2)) * 255 + CLng(b(i * 2 + 1))
Next i

i = 1
For i = 1 To 18
u(i - 1) = (CSng(l(i - 1)) - CSng(l(16))) / (CSng(l(17)) - CSng(l(16))) * VREF * 25.875622
Text3(i - 1).Text = CStr(u(i - 1))
Next i
End Sub

Private Sub Command5_Click()
End
End Sub

Private Sub Command6_Click()
Dim a(2) As Byte
Dim i As Integer
If i = 1 Then
a(0) = 0
a(1) = &HF0
a(2) = CByte(Val(Text4.Text))
MSComm1.Output = a

End If
End Sub
s = CStr(b(0))提示下标越界
展开
 我来答
heizhenpeng
2010-06-23 · TA获得超过899个赞
知道小有建树答主
回答量:955
采纳率:0%
帮助的人:1075万
展开全部
Private Sub Command3_Click()
Dim a(1) As Byte
Dim b() As Byte
Dim s As String

a(0) = 0
a(1) = 15
MSComm1.Output = a
Sleep (30)
b = MSComm1.Input
s = CStr(b(0))
Text2.Text = s
End Sub

Dim b() As Byte b元素个数为0
因此CStr(b(0))报错,看看是不是你原来要写cstr(a(0))?
a53068662
2010-06-23
知道答主
回答量:19
采纳率:0%
帮助的人:8.8万
展开全部
b()声明错误
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
gwhxyz
2010-06-23 · TA获得超过558个赞
知道小有建树答主
回答量:314
采纳率:40%
帮助的人:156万
展开全部
b = MSComm1.Input 如果没有收到任何数据则B为null所以下标越界加上对为空的判断吧
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式