C# 使用NModbus4、EasyModbus进行串口ModbusRTU数据收发,立即获取返回响应

一、先介绍NModbus4,NuGet安装NModbus4稳定版v2.1.0

1.新建类NModbus4Helper.cs

添加命名空间

using Modbus.Device;
using Modbus.Message;

        byte _DeviceId = 8;
        ModbusMaster master;

        /// 
        /// 读取单个寄存器
        /// 
        /// 
        /// 
        /// 
        public byte[] ReadSingleRegister(ushort regAddr)
        {
            try
            {
                ReadHoldingInputRegistersRequest readRegistersReq = new ReadHoldingInputRegistersRequest(0x03, _DeviceId, regAddr, 1);
                //获取响应报文
                ReadHoldingInputRegistersResponse readRegistersRes = master.ExecuteCustomMessage(readRegistersReq);
                return readRegistersRes.ProtocolDataUnit;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// 
        /// 读取多个保持型寄存器
        /// 
        /// 
        public Task ReadHoldingRegisters(ushort regStartAddr, ushort regEndAddr)
        {
            try
            {
                ushort Lenth = (ushort)(regEndAddr - regStartAddr + 1);
                return Task.Run(() =>
                {
                    ReadHoldingInputRegistersRequest readRegistersReq = new ReadHoldingInputRegistersRequest(0x03, _DeviceId, regStartAddr, Lenth);
                    //获取响应报文
                    ReadHoldingInputRegistersResponse readRegistersRes = master.ExecuteCustomMessage(readRegistersReq);
                    return readRegistersRes.ProtocolDataUnit;
                });
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public byte[] ReadRegister(ushort regAddr)
        {
            try
            {
                ushort Lenth = 1;
                ushort[] rec = master.ReadHoldingRegisters(_DeviceId, regAddr, Lenth);
                byte[] recByte = new byte[rec.Length];
                Buffer.BlockCopy(rec, 0, recByte, 0, rec.Length);
                return recByte;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public byte[] ReadRegisters(ushort regStartAddr, ushort regEndAddr)
        {
            try
            {
                ushort Lenth = (ushort)(regEndAddr - regStartAddr + 1);
                ushort[] rec = master.ReadHoldingRegisters(_DeviceId, regStartAddr, Lenth);
                byte[] recByte = new byte[rec.Length];
                Buffer.BlockCopy(rec, 0, recByte, 0, rec.Length);
                return recByte;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

二、使用EasyModbus,NuGet安装EasyModbusTCP 5.6.0

添加命名空间using EasyModbus;

byte _DeviceId = 8;
ModbusClient modbusClient = null;
object _transferLockObj = new object();

        public void OpenSerialPort()
        {
            try
            {
                modbusClient = new ModbusClient("COM1");
                modbusClient.UnitIdentifier = _DeviceId;
                modbusClient.Baudrate = _baudRate;
                modbusClient.Parity = Parity.None;
                modbusClient.StopBits = StopBits.One;
                modbusClient.ConnectionTimeout = 500;
                modbusClient.Connect();
            }
            catch (Exception ex)
            {
            }
        }

        /// 
        /// 写入单个寄存器并获取响应
        /// 
        public byte[] WriteSingleRegister(ushort regAddr, ushort value)
        {
            try
            {
                lock (_transferLockObj)
                {
                    modbusClient.receiveData = null;
                    modbusClient.WriteSingleRegister(regAddr, value);
                    byte[] Rec = modbusClient.receiveData;
                    return Rec;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// 
        /// 读取寄存器
        /// 
        public int[] ReadRegisters(ushort regStartAddr, ushort regEndAddr)
        {
            try
            {
                lock (_transferLockObj)
                {
                    ushort Lenth = (ushort)(regEndAddr - regStartAddr + 1);
                    int[] rec = modbusClient.ReadHoldingRegisters(regStartAddr, Lenth);
                    return rec;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

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