请教各位:我用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 = 1MSComm.Settings = "9600,N,8,1"MSComm.PortOpen = TrueMSComm.NullDiscard = FalseMSComm.InputMode = comInputModeText//发送/接收Dim a(3) As ByteDim d As IntegerDim i As IntegerDim abc(100) As StringDim abc1 As StringDim str As Varianta(0) = &H1 //站号a(1) = &H3 //功能码a(2) = &H40 //高字节校验码a(3) = &H21 //低字节校验码MSComm.InputLen = 37MSComm.RThreshold = 0MSComm.Output = aIf MSComm.InBufferCount >= 0 Thenstr = MSComm.InputEnd IfMSComm.InBufferCount = 0d = Len(str)If d >= 1 ThenFor i = 1 To dabc(i) = Hex(Asc(Mid(str, i, 1)))abc1 = abc1 + abc(i)NextEnd IfText1.Text = Text1.Text + " " + abc1
1楼
0
0
回复