NET CORE NMODBUS4 reads the value of serial port devices

Use NModbus4 to read serial port

     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 startAddre ss = 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("Humidity:" + sd / 10 + "%"); Console.WriteLine("Temperature:" + wd / 10 + "%"); Thread .Sleep(1000);}} //port.BytesToRead} }

  

Equipment information

High precision temperature and humidity Sensor RS485 Thermo-hygrometer Greenhouse rain-proof industrial grade 4-20ma

The value of the equipment

Share pictures

share picture

Read once every second

     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("Humidity:" + sd / 10 + "%"); Console.WriteLine ("Temperature:" + wd / 10 + "%"); Thread.Sleep(1000);}} //port.BytesToRead} }< /pre>

Leave a Comment

Your email address will not be published.