NModbus4的.netCore版本使用记录

插件源码地址:https://github.com/HakamFostok/NModbus4.NetCore

  This library is driven from this library https://github.com/NModbus4/NModbus4 with three changes.

  1. This library is targeting .NET Core framework while the original targeting .NET Framework.
  2. This library supports the SerialPort out-of-the-box while the original library needs some configuration to support that optionally (by Defining the Compile-time constant).
  3. This library is simplified by removing the unit testing which exists in the original library

1、在nuget里引用 NModbus4.NetCore

2、引用System.IO.Ports

3、代码显示

    class Program
    {
        static void Main(string[] args)
        {
            var serialPort1 = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
            serialPort1.Open();

            IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(serialPort1);
            byte slaveId = 1;//当前设备的地址为1
            var returnData = master.ReadHoldingRegisters(slaveId, 0, 2);
            serialPort1.Close();
            Console.WriteLine("湿度为:" + returnData[0] / (double)10);
            Console.WriteLine("温度为:" + returnData[1] / (double)10);
            Console.WriteLine();
        }
    }

4、效果图

  

原文地址:https://www.cnblogs.com/wjx-blog/p/13442000.html