engr.ahayee
Member
- Joined
- Aug 10, 2021
- Messages
- 16
- Programming Experience
- Beginner
Hello everyone.
I want communication between C# Application and external device using Modbus Protocol. For that I use easymodbus library.
Code is working fine. When "Read Data" button is pressed then it reads the data from device.
I want to do that task on timely basis without pressing the button.
Kindly guide me how can I achieve this task.
following is the attached image of GUI and code
Thanks and best regards
I want communication between C# Application and external device using Modbus Protocol. For that I use easymodbus library.
Code is working fine. When "Read Data" button is pressed then it reads the data from device.
I want to do that task on timely basis without pressing the button.
Kindly guide me how can I achieve this task.
following is the attached image of GUI and code
program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EasyModbus;
using System.Windows.Forms;
namespace Modbus_Master
{
public partial class Form1 : Form
{
ModbusClient MBclient;
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
MBclient = new ModbusClient(txtComPort.Text);
MBclient.Baudrate = int.Parse(cbBaudRate.Text);
if (cbParity.Text == "None") MBclient.Parity = System.IO.Ports.Parity.None;
else if (cbParity.Text == "Even") MBclient.Parity = System.IO.Ports.Parity.Even;
else if (cbParity.Text == "Odd") MBclient.Parity = System.IO.Ports.Parity.Odd;
try
{
MBclient.Connect();
lblStatus_update.Text = "Connected";
}
catch (Exception ex)
{
lblStatus_update.Text = "Error!" + ex.ToString();
}
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
MBclient.Disconnect();
lblStatus_update.Text = "Disconnected";
}
private void btnReadData_Click(object sender, EventArgs e)
{
try
{
int[] vals;
//vals = MBclient.ReadHoldingRegisters(0, 10);
vals = MBclient.ReadHoldingRegisters(int.Parse(txtStarting_Address.Text),10);
txtAddress0.Text = vals[0].ToString();
txtAddress1.Text = vals[1].ToString();
txtAddress2.Text = vals[2].ToString();
txtAddress3.Text = vals[3].ToString();
txtAddress4.Text = vals[4].ToString();
txtAddress5.Text = vals[5].ToString();
txtAddress6.Text = vals[6].ToString();
txtAddress7.Text = vals[7].ToString();
txtAddress8.Text = vals[8].ToString();
txtAddress9.Text = vals[9].ToString();
}
catch(Exception ex)
{
lblStatus_update.Text = "Error!" + ex.ToString();
}
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
}
}
}
Thanks and best regards