1

我正在使用来自 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
        }
    }
}

    

  
4

1 回答 1

0

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

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
        }


    }
}
于 2021-11-13T21:50:19.043 回答