WPF 使用libmodbus库实现Modbus协议
作者:互联网
C# WPF 使用libmodbus库实现Modbus协议
主要功能点:
1 读取线圈寄存器
2 读保持寄存器
3 写线圈寄存器
4 写保持寄存器
更多方法请参考下面代码的方法,还有一些没有使用
如果需要IP地址进行连接也可以
这是效果图,在调用之前需要在libmodbus官网下载类库后调用方法即可`
//RTU模式创建的关联函数
[DllImport("modbus.dll", EntryPoint = "modbus_new_rtu",
CharSet = CharSet.Ansi)]
public static extern IntPtr modbus_new_rtu(string device, int baud,
char parity, int data_bit, int stop_bit);
//TCP模式创建的关联函数
[DllImport("modbus.dll", EntryPoint = "modbus_new_tcp",
CharSet = CharSet.Ansi)]
public static extern IntPtr modbus_new_tcp(string ip_address, int port);
//共通的操作函数
[DllImport("modbus.dll", EntryPoint = "modbus_set_slave",
CharSet = CharSet.Ansi)]
public static extern int modbus_set_slave(IntPtr ctx,int slave);
[DllImport("modbus.dll", EntryPoint = "modbus_connect",
CharSet = CharSet.Ansi)]
public static extern int modbus_connect(IntPtr ctx);
[DllImport("modbus.dll", EntryPoint = "modbus_read_bits",
CharSet = CharSet.Ansi)]
public static extern int modbus_read_bits(IntPtr ctx, int addr,
int nb, byte[] dest);
[DllImport("modbus.dll", EntryPoint = "modbus_read_input_bits",
CharSet = CharSet.Ansi)]
public static extern int modbus_read_input_bits(IntPtr ctx,
int addr, int nb, byte[] dest);
[DllImport("modbus.dll", EntryPoint = "modbus_read_registers",
CharSet = CharSet.Ansi)]
public static extern int modbus_read_registers(IntPtr ctx,
int addr, int nb, UInt16[] dest);
[DllImport("modbus.dll", EntryPoint = "modbus_read_input_registers",
CharSet = CharSet.Ansi)]
public static extern int modbus_read__input_registers(IntPtr ctx,
int addr, int nb, UInt16[] dest);
[DllImport("modbus.dll", EntryPoint = "modbus_write_bit",
CharSet = CharSet.Ansi)]
public static extern int modbus_write_bit(IntPtr ctx,
int coil_addr, int status);
[DllImport("modbus.dll", EntryPoint = "modbus_write_register",
CharSet = CharSet.Ansi)]
public static extern int modbus_write_register(IntPtr ctx,
int reg_addr, int value);
[DllImport("modbus.dll", EntryPoint = "modbus_write_bits",
CharSet = CharSet.Ansi)]
public static extern int modbus_write_bits(IntPtr ctx,
int coil_addr, int nb, byte[] status);
[DllImport("modbus.dll", EntryPoint = "modbus_write_registers",
CharSet = CharSet.Ansi)]
public static extern int modbus_write_registers(IntPtr ctx,
int reg_addr, int nb, UInt16[] status);
[DllImport("modbus.dll", EntryPoint = "modbus_write_and_read_registers",
CharSet = CharSet.Ansi)]
public static extern int modbus_write_and_read_registers(IntPtr ctx,int write_addr, int write_nb, UInt16[] src,int read_addr, int read_nb, UInt16[] dest);
[DllImport("modbus.dll", EntryPoint = "modbus_close",
CharSet = CharSet.Ansi)]
public static extern void modbus_close(IntPtr ctx);
[DllImport("modbus.dll", EntryPoint = "modbus_free",
CharSet = CharSet.Ansi)]
public static extern void modbus_free(IntPtr ctx);
//测试
[DllImport("modbus.dll", EntryPoint = "modbus_new_rtu",
CharSet = CharSet.Ansi)]
public static extern void modbus_new_rtu(IntPtr ctx);
需要这个文档说明和软件的请联系 QQ1442601713
标签:modbus,libmodbus,int,CharSet,dll,Modbus,static,WPF,public 来源: https://blog.csdn.net/yangtianv/article/details/120199643