求助vb串口数据转换问题
我使用VB6.0的MSCOMM控件与一个电压表通信,读取电压值,使用的是MODBUS协议。
发送的查询命令是:01 04 00 34 00 04 B0 07
接收到的是: 01 04 08 43 C2 D9 4F 43 C1 92 5D 2D C2
已知电压表电压值在380V左右。我对接受到的第4-11数据做了处理,转换成Double输出,但是结果总是不正确。
我的代码如下:
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _ ByRef Destination As Any, _ ByRef Source As Any, _ ByVal Length As Long)Private Sub Form_Load() MSComm1.CommPort = 1 MSComm1.Settings = "9600,N,8,1" MSComm1.RThreshold = 13 MSComm1.InputLen = 0 MSComm1.SThreshold = 1 MSComm1.InputMode = comInputModeBinary MSComm1.PortOpen = True Text1.Text = ""End SubPrivate Sub MSComm1_OnComm() Dim InByte() As Byte Dim OutByte(0 To 7) As Byte Dim dou As Double Select Case MSComm1.CommEvent Case comEvReceive InByte = MSComm1.Input For I = LBound(InByte) To UBound(InByte) Buf = Buf + Str(InByte(I)) + Chr(32) Next I For t = 3 To 10 OutByte(t - 3) = InByte(t) Next t CopyMemory dou, OutByte(0), 8 Text9.Text = Buf Text10.Text = UBound(InByte) Text11.Text = LBound(InByte) Text12.Text = FormatNumber(dou, 2) End SelectEnd Sub
Dim OutByte(0 To 3) As ByteDim v as single...For t = 0 to 3 OutByte(t) = InByte(6-t)NextCopyMemory v, OutByte(0), 4
[解决办法]
应该会有通讯协议给你的,你的是基于modbus协议的,格式肯定是按照modbus来的,我们想知道的是00 34 这个起始地址后面的4个单元中是否存放的就是电压值呢,而且返回的是8个数据,不知道是不是4代表的是寄存器个个数,每个寄存器包含两个字节,协议中应该指定来每个自己的含义啊!