注册 登录
编程论坛 VB.NET论坛

VB.net 如何与modbus TCP server设备进行通讯

wengcxs 发布于 2021-05-15 13:20, 2701 次点击
请教 熟悉 sockets编程的大侠:
我有一个modbus TCP SERVER模块,输出开关量、输入模拟量信号
地址是192.168.1.232,端口10000

通过测试软件输出控制信号如下,测试没问题
打开通道1
发送:FE 05 00 00 FF 00 98 35
接收:FE 05 00 00 FF 00 98 35

打开通道2
发送:FE 05 00 01 FF 00 C9 F5
接收:FE 05 00 01 FF 00 C9 F5

全部关闭/全部打开
发送:FE 0F 00 00 00 02 01 00 91 93
接收:FE 0F 00 00 00 02 C0 05
发送:FE 0F 00 00 00 02 01 FF D1 D3
接收:FE 0F 00 00 00 02 C0 05

但是在用 2010进行编程时,碰到困难:
1、服务器模块的声明和连接,网上找到的代码都是根据hostname进行连接,通过ip地址连接的代码搞不懂。
2、发送上面的指令代码,并接收返回值,该怎么声明和操作?

搞了几天都没进展,谢谢
2 回复
#2
microcwj2021-09-25 02:30
成熟的方案有libModBUS,自己了解一下,底层封装已经全部做好。
#3
microcwj2021-09-25 02:38
libModbus源码是VC++的,编译之后,引用就行了。
Module libmodbus
    Public Declare Auto Function modbus_new_rtu Lib "modbus.dll" (ByVal device As Byte(), ByVal baud As Integer, ByVal parity As Byte, ByVal data_bit As Integer, ByVal stop_bit As Integer) As IntPtr

    Public Declare Auto Function modbus_set_slave Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal slave As Integer) As IntPtr

    Public Declare Auto Function modbus_connect Lib "modbus.dll" (ByVal ctx As IntPtr) As Integer
    '
    Public Declare Auto Function modbus_get_response_timeout Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal sec As Integer, ByVal usec As Integer) As Integer
    Public Declare Auto Function modbus_set_response_timeout Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal sec As Integer, ByVal usec As Integer) As Integer

    Public Declare Auto Function modbus_close Lib "modbus.dll" (ByVal ctx As IntPtr) As Integer
    Public Declare Auto Function modbus_free Lib "modbus.dll" (ByVal ctx As IntPtr) As Integer

    Public Declare Auto Function modbus_read_bits Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal addr As Integer, ByVal nb As Integer, ByVal data() As Byte) As Integer '01读取线圈
    Public Declare Auto Function modbus_read_input_bits Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal addr As Integer, ByVal nb As Integer, ByVal data() As Byte) As Integer '02读取离散输入
    Public Declare Auto Function modbus_read_registers Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal addr As Integer, ByVal nb As Integer, ByVal data() As UInt16) As Integer '03读取保持寄存器
    Public Declare Auto Function modbus_read_input_registers Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal addr As Integer, ByVal nb As Integer, ByVal dest() As UInt16) As Integer '04读取输入寄存器

    Public Declare Auto Function modbus_write_bit Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal coil_addr As Integer, ByVal status As Integer) As Integer '05写单个线圈
    Public Declare Auto Function modbus_write_register Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal reg_addr As Integer, ByVal value As Integer) As Integer '06写单个保持寄存器
    Public Declare Auto Function modbus_write_bits Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal coil_addr As Integer, ByVal nb As Integer, ByVal data() As Byte) As Integer '0x0F写多个线圈

    Public Declare Auto Function modbus_write_registers Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal addr As Integer, ByVal nb As Integer, ByVal data() As UInt16) As Integer

    Public Declare Auto Function modbus_write_and_read_registers Lib "modbus.dll" _
        (ByVal ctx As IntPtr, ByVal write_addr As Integer, ByVal write_nb As Integer, ByVal src() As UInt16, _
         ByVal read_addr As Integer, ByVal read_nb As Integer, ByVal dest() As UInt16) As Integer

    Public Declare Auto Function modbus_strerror Lib "modbus.dll" ()
    Public Declare Auto Function modbus_set_error_recovery Lib "modbus.dll" (ByVal ctx As IntPtr, ByVal ha As Byte)
End Module
1