NModbus4 工具类

NModbus4 工具类

NModbus的一个分支 目前已停止更新,但是还是非常好用的一个开源的Modbus协议的第三方.net库.安装在最新的.net6或.net core上会显示叹号,不一定兼容.酌情使用!!!
NModbus4 工具类_第1张图片

public class NModbusHelper
    {
        private static IModbusMaster master;
        private static SerialPort serialPort;
        public NModbusHelper(string portname, int baudrate)
        {
            serialPort = new SerialPort();
            serialPort.PortName = portname;
            serialPort.BaudRate = baudrate;
            serialPort.Parity = Parity.None;
            serialPort.StopBits = StopBits.One;
            serialPort.Open();
            master = ModbusSerialMaster.CreateRtu(serialPort);
            master.Transport.ReadTimeout = 2000;
        }
        /// 
        /// 读取线圈状态
        /// 
        /// 从站地址(0-255)
        /// 起始地址(0-65535)
        /// 线圈数量(0-2000)
        /// 
        public bool[] ReadCoils(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
        {
            //01 01 00 01 00 01 AC 0A 
            return master.ReadCoils(slaveAddress, startAddress, numberOfPoints);
        }

        /// 
        /// 读取输入线圈
        /// 
        /// 从站地址(0-255)
        /// 起始地址(0-65535)
        /// 线圈数量(0-2000)
        /// 
        public bool[] ReadInputs(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
        {
            //01 02 00 01 00 01 E8 0A 
            return master.ReadInputs(slaveAddress, startAddress, numberOfPoints);
        }

        /// 
        /// 读取保持型寄存器
        /// 
        /// 从站地址(0-255)
        /// 起始地址(0-65535)
        /// 寄存器数量(1-125)
        /// 
        public ushort[] ReadHoldingRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
        {
            //01 03 00 01 00 01 D5 CA 
            return master.ReadHoldingRegisters(slaveAddress, startAddress, numberOfPoints);
        }

        /// 
        /// 读取输入寄存器
        /// 
        /// 从站地址(0-255)
        /// 起始地址(0-65535)
        /// 寄存器数量(1-125)
        /// 
        public ushort[] ReadInputRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
        {
            //01 04 00 01 00 01 60 0A 
            return master.ReadInputRegisters(slaveAddress, startAddress, numberOfPoints);
        }

        /// 
        /// 写入单个线圈
        /// 
        /// 从站地址(0-255)
        /// 线圈地址(0-65535)
        /// 写入值(0-1)
        public void WriteSingleCoil(byte slaveAddress, ushort startAddress, string data)
        {
            //01 05 00 01 FF 00 CC 1B(将地址01写入1)
            bool result = false;
            if (data.Equals("true", StringComparison.OrdinalIgnoreCase) || data.Equals("1", StringComparison.OrdinalIgnoreCase))
            {
                result = true;
            }
            master.WriteSingleCoil(slaveAddress, startAddress, result);
        }

        /// 
        /// 写入单个寄存器
        /// 
        /// 从站地址(0-255)
        /// 起始地址(0-65535)
        /// 写入值(0-65535)
        public void WriteSingleRegister(byte slaveAddress, ushort startAddress, string data)
        {
            //01 06 00 01 00 01 19 CA 
            ushort result = Convert.ToUInt16(data);
            master.WriteSingleRegister(slaveAddress, startAddress, result);
        }

        /// 
        /// 批量写入线圈
        /// 
        /// 从站地址(0-255)
        /// 起始地址(0-65535)
        /// 写入值(使用','号分割)
        public void WriteArrayCoil(byte slaveAddress, ushort startAddress, string data)
        {
            //01 0F 00 00 00 03 01 02 0E 96 
            List<string> strList = data.Split(',').ToList();
            List<bool> result = new List<bool>();
            strList.ForEach(m => result.Add(m.Equals("true", StringComparison.OrdinalIgnoreCase) || m.Equals("1", StringComparison.OrdinalIgnoreCase)));
            master.WriteMultipleCoils(slaveAddress, startAddress, result.ToArray());
        }

        /// 
        /// 批量写入寄存器
        /// 
        /// 从站地址(0-255)
        /// 起始地址(0-65535)
        /// 写入值(使用','号分割)
        public void WriteArrayRegister(byte slaveAddress, ushort startAddress, string data)
        {
            //01 10 00 01 00 04 08 00 01 00 00 00 01 00 00 0A B9 
            List<string> strList = data.Split(',').ToList();
            List<ushort> result = new List<ushort>();
            strList.ForEach(m => result.Add(Convert.ToUInt16(m)));
            master.WriteMultipleRegisters(slaveAddress, startAddress, result.ToArray());
        }
    }

2023/5/25

你可能感兴趣的:(C#,c#)