【问题标题】:C# .NET How to read continuously an input from a PLC through ModbusC# .NET 如何通过 Modbus 从 PLC 连续读取输入
【发布时间】:2021-12-25 16:25:19
【问题描述】:

我正在使用来自 GitHub 的 EasyModbus 库与 PLC 通信,我尝试了所需的功能并完成了在 Visual Studio 2019 上单独运行它们,并且工作正常。

我做了一个基本程序,通过两个按钮打开和关闭 PLC 的输出。还可以通过在 C# form1 上更改两种颜色(黄色开和红色关)来读取/监控同一 PLC 的输出。

然后,当我单击打开和关闭按钮时,它们工作正常,但另一方面,读取/监控功能不起作用。我需要有人来说明一些修改如何同时运行这些功能,同时改变输出的开/关状态以及读取 PLC 的输出状态。

谢谢

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using EasyModbus;
using System.Windows.Forms;

namespace PLC
{
    public partial class Form1 : Form
    {
        ModbusClient modbusClient;
        
        public Form1()
        {
            InitializeComponent();
            ON.Visible = true; //yellow
            OFF.Visible = true;//red
            modbusClient = new ModbusClient("COM5");//communication settings
            modbusClient.UnitIdentifier = 1;
            modbusClient.Baudrate = 19200;
            modbusClient.Parity = System.IO.Ports.Parity.None;
            modbusClient.StopBits = System.IO.Ports.StopBits.One;
            modbusClient.Connect();


    // reading function begin
            var value = modbusClient.ReadCoils(0, 1);// read coil zero and if it is true change color yellow else change color to red
            

            if (value[0] == true)
            {
                ON.Visible = true;
                OFF.Visible = false;
            }
            else
            {
                ON.Visible = false;
                OFF.Visible = true;
            }

    // end of reading function

        } 
        private void button1_Click(object sender, EventArgs e)// on button
        {
            modbusClient.WriteSingleCoil(0, true);//toggle coil zero to on
        }

        private void button2_Click(object sender, EventArgs e)// off button
        {
            modbusClient.WriteSingleCoil(0, false);// toggle coil zero to off
        }
    }
}

    

  

【问题讨论】:

    标签: c# plc easy-modbus


    【解决方案1】:

    尝试使用计时器。 这是您修改后的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using EasyModbus;
    using System.Windows.Forms;
    
    
    namespace StackOverflowAnswer
    {
        public partial class Form1 : Form
        {
            ModbusClient modbusClient;
    
            public Form1()
            {
                InitializeComponent();
                ON.Visible = true; //yellow
                OFF.Visible = true;//red
                // ### changed to TCP - I have no COM ports ###
                modbusClient = new ModbusClient("127.0.0.1", 502); //communication settings
                //modbusClient.UnitIdentifier = 1;
                //modbusClient.Baudrate = 19200;
                //modbusClient.Parity = System.IO.Ports.Parity.None;
                //modbusClient.StopBits = System.IO.Ports.StopBits.One;
                modbusClient.Connect();
    
                #region Modified section
                var readModBusTimer = new Timer()
                {
                    Interval = 500,
                    Enabled = true
                };
    
                readModBusTimer.Tick += ReadModBusTimer_Tick;
                readModBusTimer.Start();
    
                // reading function begin
                ReadCoils();
    
                // end of reading function
    
            }
    
            private void ReadCoils()
            {
                var value = modbusClient.ReadCoils(0, 1);// read coil zero and if it is true change color yellow else change color to red
    
    
                if (value[0] == true)
                {
                    ON.Visible = true;
                    OFF.Visible = false;
                }
                else
                {
                    ON.Visible = false;
                    OFF.Visible = true;
                }
            }
    
            private void ReadModBusTimer_Tick(object sender, EventArgs e)
            {
                ReadCoils();
            }
            #endregion
            private void button1_Click(object sender, EventArgs e)// on button
            {
                modbusClient.WriteSingleCoil(0, true);//toggle coil zero to on
            }
    
            private void button2_Click(object sender, EventArgs e)// off button
            {
    
                modbusClient.WriteSingleCoil(0, false);// toggle coil zero to off
            }
    
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-20
      • 2021-08-03
      • 2022-07-04
      • 2022-12-22
      • 1970-01-01
      • 2022-07-05
      • 2016-06-13
      • 2014-01-15
      • 1970-01-01
      相关资源
      最近更新 更多