VB6.0 串口通讯 如何让电脑自动搜索仪表地址赋给label1? 10
PrivateSubForm_Load()i=1CallInitialPort'设置串口参数Timer1.Enabled=Truecaiji=TrueEndSubPriv...
Private Sub Form_Load()
i = 1
Call InitialPort '设置串口参数
Timer1.Enabled = True
caiji = True
End Sub
Private Sub MSComm1_OnComm()
Dim cmdin() As Byte '接收数据字节变量
Dim Index As Integer
MSComm1.InputLen = 0
Select Case MSComm1.CommEvent
Case comEvReceive '接收事件
If MSComm1.InBufferCount = 10 Then
cmdin = MSComm1.Input
If i = 1 Then
Label1(0).Caption = TwoBytes(cmdin(0), cmdin(1))
Label1(1).Caption = TwoBytes(cmdin(2), cmdin(3))
ElseIf i = 3 Then
Label1(2).Caption = TwoBytes(cmdin(0), cmdin(1))
ElseIf i = 4 Then
Label1(3).Caption = TwoBytes(cmdin(0), cmdin(1))
ElseIf i = 5 Then
MSComm1.Output = cmdout
Label1(4).Caption = TwoBytes(cmdin(0), cmdin(1))
Label2(0).Caption = TwoBytes(cmdin(2), cmdin(3))
ElseIf i = 6 Then
MSComm1.Output = cmdout
Label1(5).Caption = TwoBytes(cmdin(0), cmdin(1))
Label2(1).Caption = TwoBytes(cmdin(2), cmdin(3))
End If
End If
End Select
End Sub
现在我在form里设置i=3 就只能测仪表地址为3的值 设置i=1就只能测试仪表地址为1的值,怎么才能让它同时测不同仪表的数值? 展开
i = 1
Call InitialPort '设置串口参数
Timer1.Enabled = True
caiji = True
End Sub
Private Sub MSComm1_OnComm()
Dim cmdin() As Byte '接收数据字节变量
Dim Index As Integer
MSComm1.InputLen = 0
Select Case MSComm1.CommEvent
Case comEvReceive '接收事件
If MSComm1.InBufferCount = 10 Then
cmdin = MSComm1.Input
If i = 1 Then
Label1(0).Caption = TwoBytes(cmdin(0), cmdin(1))
Label1(1).Caption = TwoBytes(cmdin(2), cmdin(3))
ElseIf i = 3 Then
Label1(2).Caption = TwoBytes(cmdin(0), cmdin(1))
ElseIf i = 4 Then
Label1(3).Caption = TwoBytes(cmdin(0), cmdin(1))
ElseIf i = 5 Then
MSComm1.Output = cmdout
Label1(4).Caption = TwoBytes(cmdin(0), cmdin(1))
Label2(0).Caption = TwoBytes(cmdin(2), cmdin(3))
ElseIf i = 6 Then
MSComm1.Output = cmdout
Label1(5).Caption = TwoBytes(cmdin(0), cmdin(1))
Label2(1).Caption = TwoBytes(cmdin(2), cmdin(3))
End If
End If
End Select
End Sub
现在我在form里设置i=3 就只能测仪表地址为3的值 设置i=1就只能测试仪表地址为1的值,怎么才能让它同时测不同仪表的数值? 展开
1个回答
展开全部
1.您在开始的赋值语句中设置了i=N,就设置为N的地址,说明编制的没有问题;
2.把您的整个过程放到一个循环中就可以了啊。
do while 退出条件(防止锁死,并在关闭窗口时关闭COM口,以免下次不能打开)
for i=1 to 7
您的编程本体 Timer1.Enabled = True caiji = TrueEnd SubPrivate Sub MSComm1_OnComm() Dim cmdin() As Byte '接收数据字节变量 Dim Index As Integer MSComm1.InputLen = 0 Select Case MSComm1.CommEventCase comEvReceive '接收事件 If MSComm1.InBufferCount = 10 Thencmdin = MSComm1.Input If i = 1 Then Label1(0).Caption = TwoBytes(cmdin(0), cmdin(1)) Label1(1).Caption = TwoBytes(cmdin(2), cmdin(3)) ElseIf i = 3 Then Label1(2).Caption = TwoBytes(cmdin(0), cmdin(1)) ElseIf i = 4 Then Label1(3).Caption = TwoBytes(cmdin(0), cmdin(1)) ElseIf i = 5 Then MSComm1.Output = cmdout Label1(4).Caption = TwoBytes(cmdin(0), cmdin(1)) Label2(0).Caption = TwoBytes(cmdin(2), cmdin(3)) ElseIf i = 6 Then MSComm1.Output = cmdout Label1(5).Caption = TwoBytes(cmdin(0), cmdin(1)) Label2(1).Caption = TwoBytes(cmdin(2), cmdin(3)) End If End If End Select
next i
loop
2.把您的整个过程放到一个循环中就可以了啊。
do while 退出条件(防止锁死,并在关闭窗口时关闭COM口,以免下次不能打开)
for i=1 to 7
您的编程本体 Timer1.Enabled = True caiji = TrueEnd SubPrivate Sub MSComm1_OnComm() Dim cmdin() As Byte '接收数据字节变量 Dim Index As Integer MSComm1.InputLen = 0 Select Case MSComm1.CommEventCase comEvReceive '接收事件 If MSComm1.InBufferCount = 10 Thencmdin = MSComm1.Input If i = 1 Then Label1(0).Caption = TwoBytes(cmdin(0), cmdin(1)) Label1(1).Caption = TwoBytes(cmdin(2), cmdin(3)) ElseIf i = 3 Then Label1(2).Caption = TwoBytes(cmdin(0), cmdin(1)) ElseIf i = 4 Then Label1(3).Caption = TwoBytes(cmdin(0), cmdin(1)) ElseIf i = 5 Then MSComm1.Output = cmdout Label1(4).Caption = TwoBytes(cmdin(0), cmdin(1)) Label2(0).Caption = TwoBytes(cmdin(2), cmdin(3)) ElseIf i = 6 Then MSComm1.Output = cmdout Label1(5).Caption = TwoBytes(cmdin(0), cmdin(1)) Label2(1).Caption = TwoBytes(cmdin(2), cmdin(3)) End If End If End Select
next i
loop
更多追问追答
追问
追答
老,你里面用了I变量,外面我做的就改成其他就行!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询