展开全部
建议按Byte字节数组组织指令发送.
LZ的发送指令应该按9个指令轮询(没看到LZ的通信协议),接收可以放在OnComm事件中实现.
修改代码如下:
Option Explicit
Dim temp As Integer '仪表号循环变量
Dim data1T As Single, data2T As Single, data3T As Single
Dim data1F As Single, data2F As Single, data3F As Single
Dim data1Q As Single, data2Q As Single, data3Q As Single
Dim bytSend(1 To 4) As Byte
'串口初始化
Private Sub Form_Load()
MSComm1.CommPort = 1 '设置通信端口号为COM1
MSComm1.InputMode = 1 '以二进制格式读取数据
MSComm1.RThreshold = 1 '设置并返回的要接收的字符数
MSComm1.SThreshold = 1 '设置并返回传输缓冲区中允许的最小字符数
MSComm1.Settings = "4800,n,8,2" '设置串口参数
MSComm1.PortOpen = True '打开串口
Timer1.Interval = 200
End Sub
'改变仪表序号
Private Sub Timer1_Timer()
temp = temp + 1
Call order_num
If temp > 9 Then temp = 0
End Sub
'周期发出请求指令,自动连续采集
'循环向仪表发送读数据命令串
'不同的仪表号发送不同的读指令
Sub order_num()
If temp = 1 Then
bytSend(1) = &H81
bytSend(2) = &H81
bytSend(3) = &H52
bytSend(4) = &H1
MSComm1.Output = bytSend 'Chr(&H8181) + Chr(&H52) + Chr(&H1)
ElseIf temp = 2 Then
bytSend(4) = &H2
MSComm1.Output = bytSend 'Chr(&H8181) + Chr(&H52) + Chr(&H2)
ElseIf temp = 3 Then
bytSend(4) = &H3
MSComm1.Output = bytSend 'Chr(&H8181) + Chr(&H52) + Chr(&H3)
ElseIf temp = 4 Then
bytSend(2) = &H82
bytSend(4) = &H1
MSComm1.Output = bytSend 'Chr(&H8182) + Chr(&H52) + Chr(&H1)
ElseIf temp = 5 Then
bytSend(4) = &H2
MSComm1.Output = bytSend 'Chr(&H8182) + Chr(&H52) + Chr(&H2)
ElseIf temp = 6 Then
bytSend(4) = &H3
MSComm1.Output = bytSend 'Chr(&H8182) + Chr(&H52) + Chr(&H3)
ElseIf temp = 7 Then
bytSend(2) = &H83
bytSend(4) = &H1
MSComm1.Output = bytSend 'Chr(&H8183) + Chr(&H52) + Chr(&H1)
ElseIf temp = 8 Then
bytSend(4) = &H2
MSComm1.Output = bytSend 'Chr(&H8183) + Chr(&H52) + Chr(&H2)
ElseIf temp = 9 Then
bytSend(4) = &H3
MSComm1.Output = bytSend 'Chr(&H8183) + Chr(&H52) + Chr(&H3)
Else
Exit Sub
End If
End Sub
希望对你能有所帮助。
LZ的发送指令应该按9个指令轮询(没看到LZ的通信协议),接收可以放在OnComm事件中实现.
修改代码如下:
Option Explicit
Dim temp As Integer '仪表号循环变量
Dim data1T As Single, data2T As Single, data3T As Single
Dim data1F As Single, data2F As Single, data3F As Single
Dim data1Q As Single, data2Q As Single, data3Q As Single
Dim bytSend(1 To 4) As Byte
'串口初始化
Private Sub Form_Load()
MSComm1.CommPort = 1 '设置通信端口号为COM1
MSComm1.InputMode = 1 '以二进制格式读取数据
MSComm1.RThreshold = 1 '设置并返回的要接收的字符数
MSComm1.SThreshold = 1 '设置并返回传输缓冲区中允许的最小字符数
MSComm1.Settings = "4800,n,8,2" '设置串口参数
MSComm1.PortOpen = True '打开串口
Timer1.Interval = 200
End Sub
'改变仪表序号
Private Sub Timer1_Timer()
temp = temp + 1
Call order_num
If temp > 9 Then temp = 0
End Sub
'周期发出请求指令,自动连续采集
'循环向仪表发送读数据命令串
'不同的仪表号发送不同的读指令
Sub order_num()
If temp = 1 Then
bytSend(1) = &H81
bytSend(2) = &H81
bytSend(3) = &H52
bytSend(4) = &H1
MSComm1.Output = bytSend 'Chr(&H8181) + Chr(&H52) + Chr(&H1)
ElseIf temp = 2 Then
bytSend(4) = &H2
MSComm1.Output = bytSend 'Chr(&H8181) + Chr(&H52) + Chr(&H2)
ElseIf temp = 3 Then
bytSend(4) = &H3
MSComm1.Output = bytSend 'Chr(&H8181) + Chr(&H52) + Chr(&H3)
ElseIf temp = 4 Then
bytSend(2) = &H82
bytSend(4) = &H1
MSComm1.Output = bytSend 'Chr(&H8182) + Chr(&H52) + Chr(&H1)
ElseIf temp = 5 Then
bytSend(4) = &H2
MSComm1.Output = bytSend 'Chr(&H8182) + Chr(&H52) + Chr(&H2)
ElseIf temp = 6 Then
bytSend(4) = &H3
MSComm1.Output = bytSend 'Chr(&H8182) + Chr(&H52) + Chr(&H3)
ElseIf temp = 7 Then
bytSend(2) = &H83
bytSend(4) = &H1
MSComm1.Output = bytSend 'Chr(&H8183) + Chr(&H52) + Chr(&H1)
ElseIf temp = 8 Then
bytSend(4) = &H2
MSComm1.Output = bytSend 'Chr(&H8183) + Chr(&H52) + Chr(&H2)
ElseIf temp = 9 Then
bytSend(4) = &H3
MSComm1.Output = bytSend 'Chr(&H8183) + Chr(&H52) + Chr(&H3)
Else
Exit Sub
End If
End Sub
希望对你能有所帮助。
追问
Chr(&H8183) + Chr(&H52) + Chr(&H3)是什么,我试了这个output不行啊。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询