您的位置:控制工程论坛网论坛 » PLC与PAC » 求助:用VB编写Modbus RTU通讯

tony_k

tony_k   |   当前状态:离线

总积分:917  2024年可用积分:0

注册时间: 2008-09-27

最后登录时间: 2008-11-28

空间 发短消息加为好友

求助:用VB编写Modbus RTU通讯

tony_k  发表于 2008/10/8 15:46:25      1820 查看 5 回复  [上一主题]  [下一主题]

手机阅读

请教各位:我用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 回复
  • tony_k

    tony_k   |   当前状态:离线

    总积分:917  2024年可用积分:0

    注册时间: 2008-09-27

    最后登录时间: 2008-11-28

    空间 发短消息加为好友

    tony_k   发表于 2008/10/6 13:49:47

    这样试一试:
    改:MSComm.InputMode = comInputModeBinary


    在读入数据处按以下方式下该修改程序:
    Dim buf() As Byte
    Dim i As Integer

      buf = MSComm1.Input
      For i = LBound(buf) To UBound(buf)
      txtReceive.Text = txtReceive.Text + " " + Hex(buf(i))
      Next i

    End Sub
    2楼 回复本楼

    引用 tony_k 2008/10/6 13:49:47 发表于2楼的内容

总共 , 当前 /