C# 读取Modbus

           using (TcpClient client = new TcpClient("127.0.0.1", 1502))
            {
                var factory = new ModbusFactory();
                IModbusMaster master = factory.CreateMaster(client);

                // read five input values
                ushort startAddress = 100;
                ushort numInputs = 5;
                //bool[] inputs = master.ReadInputs(1, startAddress, numInputs);

                ushort[] registers = master.ReadHoldingRegisters(1, 4003, 1);

               
               
            }

            for (int i = 0; i < this.dataGridViewReadConfig.Rows.Count-1; i++)
            {
                string unit = textBoxUnit.Text.ToString();
                string startAddress = this.dataGridViewReadConfig.Rows[i].Cells[1].Value.ToString();
              
                string number = this.dataGridViewReadConfig.Rows[i].Cells[2].Value.ToString();
                var shortData = client.ReadHoldingRegisters<ushort>(1, 4003,  1);

                foreach (var item in shortData)
                {
                    this.dataGridViewReadConfig.Rows[i].Cells[4].Value = item+",";
                }
               
            }
		使用组件库
		FluentModbus
		NModbus
posted @ 2021-12-07 11:31  zhaogaojian  阅读(1331)  评论(0编辑  收藏  举报