• Modbus入门


    Modbus

    modbus使用范围广泛,广泛应用于各类仪表,PLC等。它属于应用层协议,底层硬件基于485/以太网。

    Modbus的存储区有:输入线圈(布尔,只读,代号1区),输入寄存器(寄存器,只读,代号3区),输出线圈(读写,布尔,代号0区),输出寄存器(寄存器,读写,代号4区)

    Modbus是典型的半双工模式,有且只有一个master,请求由master发出,slave响应。slave之间不能通讯,只能通过master转达。相当于master是客户端,slave都是服务器。

    Modbus模拟工具

    模拟工具使用Modbus Slave 以及 Modbus Poll 。其中 Slave相当于服务器(Modbus Slave),Poll相当于客户端(Modbus Master)。

    模拟工具使用

    配置Slave

    配置Slave
    在这里插入图片描述
    基本配置,配置完选择ok,接下来只要配置要使用的接口方式(网卡,串口等)

    在这里插入图片描述
    在这里插入图片描述
    选择接口方式,选择串口,初始化波特率、数据位、校验位、停止位,然后选择ok即可打开链接。
    在这里插入图片描述

    配置Poll

    打开Poll选择需要进行的操作
    在这里插入图片描述

    选择写入可以寄存器,会发现Slave这边对应的已经改变了
    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述
    上面type中可以写入浮点数等类型。

    C#使用ModBus通讯

    使用NuGet中的NModbus4通讯库,进行ModBus RTU(串口)通讯

    namespace ModusCommunication
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                ModuleHandle moduleHandle = new ModuleHandle();   
    
                // NuGet 安装 NModbus4 库
                // 确定通讯方式 这边是串口
                SerialPort port = new SerialPort("COM2");
                // 波特率
                port.BaudRate = 9600;
                // 数据位
                port.DataBits= 8;
                // 停止位
                port.StopBits = StopBits.One;
                // 校验位
                port.Parity = Parity.None;
    
                port.Open();
    
                var master = ModbusSerialMaster.CreateRtu(port);
                //  读取保持型寄存器  slaveid 寄存器起始位置 读取个数
                ushort [] values = master.ReadHoldingRegisters(1,10,2);
                Console.WriteLine($"index 10:{values[0]},index 11:{values[1]}");
                // 写入保持型寄存器  slaveid 在18号寄存器 写入 133
                master.WriteSingleRegister(1, 18, 133);
    
                // 读写线圈型寄存器 
                bool [] coils = master.ReadCoils(2, 0, 3);
                Console.WriteLine($"index 0:{coils[0]},index 2:{coils[2]}");
                // 写入线圈状态 将2号线圈寄存器值改为false
                master.WriteSingleCoil(2, 2, false);
    
                // 在10寄存器处写入浮点数(实际占用10,11两个寄存器)
                master.WriteFloat(1, 10, 3.14f);
                Console.WriteLine($"写入浮点数:3.14");
    
                // 读取浮点数  浮点数在Modbus中是由两位寄存器构成(一位寄存器是16bit)
                float val = master.ReadFloat(1, 10, 2);
                Console.WriteLine($"浮点数 index 10-11:{val}");
                Console.ReadLine();
                port.Close();
    
    
            }
    
            
        }
    }
    
    public  static class NModbusExtensions
    {
        /// 
        /// 从寄存器中读取float值
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static float ReadFloat(this ModbusSerialMaster master , byte slaveAddress, ushort startAddress, ushort numberOfPoints)
        {
            try
            {
                float? floatValue;
                ushort[] registers = master.ReadHoldingRegisters(slaveAddress, startAddress, 2); // 2寄存器对应一个浮点数
    
                if (registers.Length == 2)
                {
                    // 从两个16位整数重新组合为浮点数
                    ushort intValue1 = registers[1]; // 低位
                    ushort intValue2 = registers[0]; // 高位
    
                    byte[] bytes = new byte[4];
                    Buffer.BlockCopy(new ushort[] { intValue1, intValue2 }, 0, bytes, 0, 4);
    
                    floatValue = BitConverter.ToSingle(bytes, 0);
                    return floatValue.Value;
                }
                else
                {
                    throw new Exception();
                }
            }catch(Exception ex)
            {
                throw new Exception("读取失败");
            }
            
        }
    
        /// 
        /// 向寄存器中写入浮点数
        /// 
        /// 
        /// 
        /// 
        /// 
        public static void WriteFloat(this ModbusSerialMaster master, byte slaveAddress, ushort startAddress, float value)
        {
            // 将浮点数转换为字节数组
            byte[] bytes = BitConverter.GetBytes(value);
    
            // 提取字节数组中的两个16位整数
            ushort intValue1 = BitConverter.ToUInt16(bytes, 0); // 低位
            ushort intValue2 = BitConverter.ToUInt16(bytes, 2); // 高位
    
            master.WriteMultipleRegisters(slaveAddress, startAddress, new ushort[] { intValue2, intValue1 });
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114

    在这里插入图片描述

    在无法使用 SerialPort类 的情况下使用TCP进行

    在Web以及移动端是不能使用SerialPort的,这时候可以使用TCP进行链接
    在slave连接配置中是有ip地址以及端口的,可以通过这个进行ModbusTcp通讯
    在这里插入图片描述

    NuGet 安装 ModbusTcp 包

    
    static async void ModbusTCPTest()
    {
        ModbusTcp.ModbusClient client = new ModbusTcp.ModbusClient("127.0.0.1", 502);
        client.Init();
        // 读取一个 整数
        short[] values = await client.ReadRegistersAsync(15,1);
        Console.WriteLine($"index 15:{values[0]}");
        // 读取两个浮点数
        float[] value = await client.ReadRegistersFloatsAsync(10, 4);
        Console.WriteLine($"index 10-11:{value[0]},index 12-13:{value[1]}");
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

  • 相关阅读:
    【探索Linux】文件描述符 | 重定向 | 基础IO —— 强大的命令行工具 P.12
    2023计算机毕业设计SSM最新选题之java乡村疫情防控管理系统37804
    1.django部署(案例)
    java-php-python-ssm计算机类专业考研交流学习平台计算机毕业设计
    c#using关键字的作用
    单元测试(JUint)
    Kubernetes(k3s)基础学习(三) -- Deployment、Service、Ingress
    在JavaScript中利用代码来实现ATM的效果,具有存钱,取钱,查看余额,退出功能
    <数据结构> - 数据结构在算法比赛中的应用(下)
    Git学习总结
  • 原文地址:https://blog.csdn.net/xiuyuandashen/article/details/134260240