NModbus4 读取串口设备数值
使用NModbus4 读取串口
public static void aget() { byte[] array = new byte[8]; using (SerialPort port = new SerialPort("COM3")) { port.BaudRate = 9600; port.DataBits = 8; port.Parity = Parity.None; port.StopBits = StopBits.One; port.Open(); array[0] = 253; array[1] = 3; array[2] = 1; array[3] = 0; array[4] = 0; array[5] = 2; array[6] = 209; array[7] = 203; port.Write(array, 0, 8); port.ReadTimeout = 1000; Thread.Sleep(800); if (port.BytesToRead > 0) { IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port); byte slaveId = 1; ushort startAddress = 100; ushort[] registers = new ushort[] { 1, 2, 3 }; // write three registers //ushort[] shorts = master.r(slaveId, 0, 80); while (true) { ushort[] shorts2 = master.ReadHoldingRegisters(slaveId, 0, 5); double sd = double.Parse(shorts2[0].ToString()); double wd = double.Parse(shorts2[1].ToString()); Console.Clear(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("湿度:" + sd / 10 + "%"); Console.WriteLine("温度:" + wd / 10 + " ℃"); Thread.Sleep(1000); } } //port.BytesToRead } }
设备信息
精讯畅通的 高精度温湿度传感器RS485温湿度计 大棚防雨水工业级4-20ma
设备的数值
每一秒读去一次