码迷,mamicode.com
首页 > 数据库 > 详细

net core NModbus4 读取串口设备数值

时间:2018-10-02 14:00:15      阅读:415      评论:0      收藏:0      [点我收藏+]

标签:温湿度   start   rtu   ble   ima   void   add   byte   reg   

使用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 读取串口设备数值

标签:温湿度   start   rtu   ble   ima   void   add   byte   reg   

原文地址:https://www.cnblogs.com/leoxjy/p/9736206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!