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、效果圖

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM