一个业余写代码的

一个业余写代码的

一.编译libmodbus库供C#使用

如何编译?请移步:https://blog.csdn.net/weixin_42205408/article/details/119530811

上面博主的文章除了所写的modbus.cs内的代码有点问题外(可能上面博主和我的Win 10 64位 Visual Studio 2019平台不一样吧),其他写的很详细。

二.实际应用

把编译得到的modbus.dll文件添加到C#项目中

我在他的基础上更改了modbus.cs(我的是libmodbus.cs),其实类名可以自己定义。

1.libmodbus.cs类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Robotics_Studio
{
    class libmodbus
    {
        ///[modbus.h]MODBUS_API int modbus_set_slave(modbus_t *ctx, int slave);
        [DllImport("modbus.dll", EntryPoint = "modbus_set_slave", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_set_slave(IntPtr ctx, int slave);

        ///[modbus.h]MODBUS_API int modbus_get_slave(modbus_t *ctx);
        [DllImport("modbus.dll", EntryPoint = "modbus_get_slave", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_get_slave(IntPtr ctx);

        ///[modbus.h]MODBUS_API int modbus_set_socket(modbus_t *ctx, int s);
        [DllImport("modbus.dll", EntryPoint = "modbus_set_socket", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_set_socket(IntPtr ctx, int s);

        ///[modbus.h]MODBUS_API int modbus_get_socket(modbus_t *ctx);
        [DllImport("modbus.dll", EntryPoint = "modbus_get_socket", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_get_socket(IntPtr ctx);

        ///[modbus.h]MODBUS_API int modbus_get_response_timeout(modbus_t* ctx, uint32_t* to_sec, uint32_t* to_usec);
        [DllImport("modbus.dll", EntryPoint = "modbus_get_response_timeout", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_get_response_timeout(IntPtr ctx, UInt32[] to_sec, UInt32[] to_usec);

        ///[modbus.h]MODBUS_API int modbus_set_response_timeout(modbus_t* ctx, uint32_t to_sec, uint32_t to_usec);
        [DllImport("modbus.dll", EntryPoint = "modbus_set_response_timeout", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_set_response_timeout(IntPtr ctx, UInt32 to_sec, UInt32 to_usec);

        ///[modbus-rtu.h]MODBUS_API modbus_t *modbus_new_rtu(const char *device, int band, char parity, int data_bit, int stop_bit);
        [DllImport("modbus.dll", EntryPoint = "modbus_new_rtu", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr modbus_new_rtu(string device, int baud, char parity, int data_bit, int stop_bit);

        ///[modbus-tcp.h]MODBUS_API modbus_t *modbus_new_tcp(const char *ip_address, int port);
        [DllImport("modbus.dll", EntryPoint = "modbus_new_tcp", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr modbus_new_tcp(string ip_address, int port);

        ///[modbus-tcp.h]MODBUS_API int modbus_tcp_listen(modbus_t *ctx, int nb_connection);
        [DllImport("modbus.dll", EntryPoint = "modbus_tcp_listen", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr modbus_tcp_listen(IntPtr ctx, int nb_connection);

        ///[modbus-tcp.h]MODBUS_API int modbus_tcp_accept(modbus_t *ctx, int *s);
        [DllImport("modbus.dll", EntryPoint = "modbus_tcp_accept", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr modbus_tcp_accept(IntPtr ctx, int[] s);

        ///[modbus.h]MODBUS_API int modbus_connect(modbus_t *ctx);
        [DllImport("modbus.dll", EntryPoint = "modbus_connect", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_connect(IntPtr ctx);

        ///[modbus.h]MODBUS_API void modbus_close(modbus_t *ctx);
        [DllImport("modbus.dll", EntryPoint = "modbus_close", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern void modbus_close(IntPtr ctx);

        ///[modbus.h]MODBUS_API void modbus_free(modbus_t *ctx);
        [DllImport("modbus.dll", EntryPoint = "modbus_free", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern void modbus_free(IntPtr ctx);

        ///[modbus.h]MODBUS_API void modbus_flush(modbus_t *ctx);
        [DllImport("modbus.dll", EntryPoint = "modbus_flush", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern void modbus_flush(IntPtr ctx);

        ///[modbus.h]MODBUS_API int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
        [DllImport("modbus.dll", EntryPoint = "modbus_read_bits", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_read_bits(IntPtr ctx, int addr, int nb, byte[] dest);

        ///[modbus.h]MODBUS_API int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
        [DllImport("modbus.dll", EntryPoint = "modbus_read_input_bits", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_read_input_bits(IntPtr ctx, int addr, int nb, byte[] dest);

        ///[modbus.h]MODBUS_API int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
        [DllImport("modbus.dll", EntryPoint = "modbus_read_registers", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_read_registers(IntPtr ctx, int addr, int nb, UInt16[] dest);

        ///[modbus.h]MODBUS_API int modbus_read_input_registers(modbus_t* ctx, int addr, int nb, uint16_t* dest);
        [DllImport("modbus.dll", EntryPoint = "modbus_read_input_registers", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_read_input_registers(IntPtr ctx, int addr, int nb, UInt16[] dest);

        ///[modbus.h]MODBUS_API int modbus_write_bit(modbus_t *ctx, int coil_addr, int status);
        [DllImport("modbus.dll", EntryPoint = "modbus_write_bit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_write_bit(IntPtr ctx, int coil_addr, int status);

        ///[modbus.h]MODBUS_API int modbus_write_register(modbus_t *ctx, int reg_addr, const uint16_t value);
        [DllImport("modbus.dll", EntryPoint = "modbus_write_register", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_write_register(IntPtr ctx, int reg_addr, UInt16 value);

        ///[modbus.h]MODBUS_API int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);
        [DllImport("modbus.dll", EntryPoint = "modbus_write_bits", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_write_bits(IntPtr ctx, int addr, int nb, byte[] data);

        ///[modbus.h]MODBUS_API int modbus_write_registers(modbus_t* ctx, int addr, int nb, const uint16_t* data);
        [DllImport("modbus.dll", EntryPoint = "modbus_write_registers", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_write_registers(IntPtr ctx, int addr, int nb, UInt16[] data);

        ///[modbus.h]MODBUS_API int modbus_write_and_read_registers(modbus_t *ctx, int write_addr, int write_nb, const uint16_t* src, int read_addr, int read_nb, uint16_t *dest);
        [DllImport("modbus.dll", EntryPoint = "modbus_write_and_read_registers", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int modbus_write_and_read_registers(IntPtr ctx, int write_addr, int write_nb, UInt16[] scr, int read_addr, int read_nb, UInt16[] dest);
    }
}
2.C#逻辑主程序

注意事项:

2.1 假设你的类名叫 libmodbus.cs

需注意类中的引用部分,例如

[DllImport("modbus.dll", EntryPoint = "modbus_new_tcp", CharSet = CharSet.Ansi)]

全部都需要加上: CallingConvention = CallingConvention.Cdecl

即:

[DllImport("modbus.dll", EntryPoint = "modbus_new_tcp", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]

否则出现:原因可能是托管的PInvoke签名与非托管的目标签名不匹配

上面的libmodbus.cs就是已经改好过来的。

2.2 使用类时需注意

libmodbus modbus_tcp = new libmodbus();
modbus_tcp.modbus_set_slave(modbus_tcp.modbus_new_tcp(ip1, 502),2);//此行会报错,无法使用实例引用来访问成员,请改用类型名来限定它

应改为
 

//libmodbus modbus_tcp = new libmodbus();//注释掉此行
libmodbus.modbus_set_slave(modbus_tcp.modbus_new_tcp(ip1, 502),2);//更改后

2.3 已测试好的例子如下:

        private void button_Connect_Click(object sender, EventArgs e)
        {
            if (button_Connect.Text == "连接")
            {
                string RobotIP = this.textBox_IP.Text.Trim();
                //by libmodbus
                IntPtr Machine = libmodbus.modbus_new_tcp(RobotIP, 502);//创建TCP连接
                libmodbus.modbus_set_slave(Machine, 2);
                int connectSta = 0;
                int connectCnt = 0;
                while (true)
                {
                    connectSta = libmodbus.modbus_connect(Machine);
                    if (connectSta == 0)//连接状态
                    {
                        break;
                    }
                    connectCnt += 1;
                    if(connectCnt == 4)//连接失败次数计数
                    {
                        connectSta = -1;
                        break;
                    }
                }
                if (connectSta == -1)
                { 
                    MessageBox.Show("Try many times were failed,Try again please!", "Info:");
                    return;
                }
                try
                {
                    //读单个保持寄存器
                    ushort[] servoSta = { 0 };//伺服状态
                    libmodbus.modbus_read_registers(Machine, 0x0006, 1, servoSta);
                    Console.WriteLine("read servo status: {0}", servoSta[0]);

                    //读多个保持寄存器
                    ushort[] current_pos = new ushort[12];//位置坐标
                    libmodbus.modbus_read_registers(Machine, 0x00F0, 12, current_pos);
                    Console.WriteLine("read xH,xL: {0},{1}", current_pos[0], current_pos[1]);
                    Console.WriteLine("read yH,yL: {0},{1}", current_pos[2], current_pos[3]);
                    Console.WriteLine("read zH,zL: {0},{1}", (Int16)current_pos[4], (Int16)current_pos[5]);//(Int16)是把无符号16位整型转有符号16位整型
                    Console.WriteLine("read cH,cL: {0},{1}", current_pos[10], current_pos[11]);
                    Console.WriteLine("read x: {0}", Two16Int_2_One32Int(current_pos[0], current_pos[1]));
                    Console.WriteLine("read y: {0}", Two16Int_2_One32Int(current_pos[2], current_pos[3]));
                    Console.WriteLine("read z: {0}", Two16Int_2_One32Int(current_pos[4], current_pos[5]));
                    Console.WriteLine("read c: {0}", Two16Int_2_One32Int(current_pos[10], current_pos[11]));

                    //写单个保持寄存器
                    ushort value1 = 32767;//write Int16 (2^15)-1 = 32767
                    libmodbus.modbus_write_register(Machine, 0x2000, value1);

                    //写单个保持寄存器
                    ushort[] value2 = { 32767 };//write Int16 (2^15)-1 = 32767
                    libmodbus.modbus_write_registers(Machine, 0x2002, 1, value2);

                    //写多个保持寄存器
                    ushort[] value3 = { 65535,32767 };//write Int32 (2^31)-1 = 2147483647
                    libmodbus.modbus_write_registers(Machine, 0x2004, 2, value3);
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Read error!\n" + ex.Message, "Info:");
                }
            }
        }

运行结果如下:

C#使用libmodbus库与工业设备进行读写测试-LMLPHP

工业设备端:

 C#使用libmodbus库与工业设备进行读写测试-LMLPHP

 完结

08-03 11:32