首先仿真软件ModbusSlave简单配置3条数据:
连接配置:
VS中安装NuGet包:NModbus4
简单获取湿度
SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);serialPort.Open();Modbus.Device.ModbusSerialMaster master = Modbus.Device.ModbusSerialMaster.CreateRtu(serialPort);ushort[] datas = master.ReadHoldingRegisters(1, 1, 1);
if (datas != null && datas.Length > 0)
{Console.WriteLine(datas[0]);
}