I am using the EasyModbus library from GitHub to communicate to the PLC, I tried the required functions and I accomplish to run them on visual studio 2019 individually, and worked fine.
I did a basic program to switch on and off an output of the PLC by using two buttons. And also reading/monitoring an output from the same PLC by changing two colors(yellow On and red Off) on C# form1.
Then, when I click the on and off buttons they work fine but on the other hand, the reading/monitoring function didn't work. I need someone to illustrate some modifications how to run these functions simultaneously and at the same time changing the on/off status of the output and also reading the status of an output of the PLC.
Thank you
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
}
}
}