使用nuget引入NModbus4
RTU 示例
1,串口通信示例
//
// RTU SerialPort serialPort = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);serialPort.Open();ModbusMaster master = ModbusSerialMaster.CreateRtu(serialPort);// 起始地址 “40001” "I0.0" "DB1.DBW100"ushort[] values = master.ReadHoldingRegisters(1, 0, 2);// 功能 效率// float数据 从两个ushort值转换成float// 设置从设备地址和要写入的寄存器地址及值 ushort startAddress = 0; // 起始地址 ushort valueToWrite = 123; // 要写入的寄存器值 // 将值写入从设备的寄存器中 master.WriteSingleRegister(1, startAddress, valueToWrite);
2 网口通讯示例
TcpClient myclient = new TcpClient("192.168.1.254", 502);ModbusMaster myMaster = ModbusIpMaster.CreateIp(myclient);myMaster.Transport.ReadTimeout = 2000;myMaster.Transport.Retries = 3;myMaster.Transport.WaitToRetryMilliseconds = 250;ushort[] value = myMaster.ReadHoldingRegisters(1, 0, 2);