发表于:2004-06-18 09:09:00
楼主
请教各位:我用VB编写与FC6000流量演算器通讯,采用Modbus RTU协议。具体代码如下,
出现一些问题:发送01 03 40 21(16进制)应该收至37个字节的。MSComm.InBufferCount显示的是37个字节,可是赋给str变量时就不是37字节(一般只有31,32,33个字节)且str变量收到的是一串看不懂的乱码,只有用ASC函数才可以看得懂。
请教各位,如何来解决此问题。如果有哪位大侠有用VB编写过Modbus RTU,方便的话请发一个例子让我参考参考好吗?ZYY181@SINA.COM 谢谢!!
//初始化
MSComm.CommPort = 1
MSComm.Settings = "9600,N,8,1"
MSComm.PortOpen = True
MSComm.NullDiscard = False
MSComm.InputMode = comInputModeText
//发送/接收
Dim a(3) As Byte
Dim d As Integer
Dim i As Integer
Dim abc(100) As String
Dim abc1 As String
Dim str As Variant
a(0) = &H1 //站号
a(1) = &H3 //功能码
a(2) = &H40 //高字节校验码
a(3) = &H21 //低字节校验码
MSComm.InputLen = 37
MSComm.RThreshold = 0
MSComm.Output = a
If MSComm.InBufferCount >= 0 Then
str = MSComm.Input
End If
MSComm.InBufferCount = 0
d = Len(str)
If d >= 1 Then
For i = 1 To d
abc(i) = Hex(Asc(Mid(str, i, 1)))
abc1 = abc1 + abc(i)
Next
End If
Text1.Text = Text1.Text + " " + abc1