请问,VB MScomm和单片机通讯,怎么从接收帧中把数据分离显示在TEXT控件上.多谢
请教,VB MScomm和单片机通讯,如何从接收帧中把数据分离显示在TEXT控件上.谢谢
通讯协议,Modbus RTU ;地址+读功能码+寄存器起始号+数据点数+CRC校验
读1号主机起始地址为40000开始的4个寄存器的值(40011-40014)
发送帧:01 03 00 0A 00 04 64 0B
接收帧:01 03 08 00 E3 00 E3 00 E3 00 E3 32 B0
///////////////////////////
******略去****************
Private Sub MSComm1_OnComm()
Dim i%, buf$
Dim nPos
Dim hexdisp As String
Dim inByte() As Byte
Dim tmp As Variant
buf = " "
Select Case MSComm1.CommEvent
Case comEvReceive
tmp = MSComm1.Input
inByte = tmp
For i = LBound(inByte) To UBound(inByte)
buf = buf + Hex(inByte(i)) + Chr(32)
Next i
hexdisp = hexdisp + buf
End Select
p11 = Val( "&H " & Mid(hexdisp, 8, 4))
p12 = Val( "&H " & Mid(hexdisp, 13, 4))
p13 = Val( "&H " & Mid(hexdisp, 18, 4))
p14 = Val( "&H " & Mid(hexdisp, 23, 4))
Text2.Text = str(p11)
Text3.Text = str(p12)
Text4.Text = str(p13)
Text1.Text = str(p14)
end sub
*********略去*********************
//////////////////////////////////
上面读四个寄存器地址值的时候,TEXT1-4都可以正确显示收到的数据。
问题是,我改为如果读八个寄存器地址的值时,TEX1-TEXT8都无法得到正确的数据。如下
通讯协议,Modbus RTU ;地址+读功能码+寄存器起始号+数据点数+CRC校验
读1号主机起始地址为40000开始的4个寄存器的值(40011-40014)
发送帧:01 03 00 0A 00 04 64 0B
接收帧:01 03 08 00 E3 00 E3 00 E3 00 E3 32 B0
///////////////////////////
******略去****************
Private Sub MSComm1_OnComm()
Dim i%, buf$
Dim nPos
Dim hexdisp As String
Dim inByte() As Byte
Dim tmp As Variant
buf = " "
Select Case MSComm1.CommEvent
Case comEvReceive
tmp = MSComm1.Input
inByte = tmp
For i = LBound(inByte) To UBound(inByte)
buf = buf + Hex(inByte(i)) + Chr(32)
Next i
hexdisp = hexdisp + buf
End Select
p11 = Val( "&H " & Mid(hexdisp, 8, 4))
p12 = Val( "&H " & Mid(hexdisp, 13, 4))
p13 = Val( "&H " & Mid(hexdisp, 18, 4))
p14 = Val( "&H " & Mid(hexdisp, 23, 4))
Text2.Text = str(p11)
Text3.Text = str(p12)
Text4.Text = str(p13)
Text1.Text = str(p14)
end sub
*********略去*********************
//////////////////////////////////
上面读四个寄存器地址值的时候,TEXT1-4都可以正确显示收到的数据。
问题是,我改为如果读八个寄存器地址的值时,TEX1-TEXT8都无法得到正确的数据。如下