net core NModbus4 讀取串口設備數值
阿新 • • 發佈:2018-10-02
.data clas write 水工 store mage ring serial 技術
使用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
設備的數值
每一秒讀去一次
net core NModbus4 讀取串口設備數值