Resolved Perform task on timely basis

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

CHINT ReadData.png

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
 
Solution
You need an event to kick of a task. That event can be whatever is appropriate, e.g. Button.Click, TextBox.TextChanged, ComboBox.SelectedindexChanged or whatever. If you want a task to be initiated at a specific time or after a specific amount of time then you use a Timer and handle its Tick event. You can add an instance to the form in the designer and configure it in the Properties window, just as you would a control. You can set the Interval to a particular value and the event will be raised that many milliseconds after the Timer is enabled and repeatedly thereafter, until it is disabled. If you want the task to occur at a specific time of day, write code to calculate the number...
You need an event to kick of a task. That event can be whatever is appropriate, e.g. Button.Click, TextBox.TextChanged, ComboBox.SelectedindexChanged or whatever. If you want a task to be initiated at a specific time or after a specific amount of time then you use a Timer and handle its Tick event. You can add an instance to the form in the designer and configure it in the Properties window, just as you would a control. You can set the Interval to a particular value and the event will be raised that many milliseconds after the Timer is enabled and repeatedly thereafter, until it is disabled. If you want the task to occur at a specific time of day, write code to calculate the number of milliseconds until that time and assign that to the property.
 
Solution
@jmcilhinney I tried Timer and it is working fine. Thanks for you reply.
I have some questions regarding timer.
1- Can we use same timer for multiple tasks or we have to use different timers for different tasks?
2- If I want to set timer values from any text box through GUI , can we do that? also if I want to enable/disable timer from GUI. can I do that?
 
1- Can we use same timer for multiple tasks or we have to use different timers for different tasks?
Can you use the same Button for multiple tasks? A Button simply raises a Click event when you click it. What you do in the event handler is completely up to you. It can be as simple or as complex as you like. The same goes for the Tick event of a Timer. That said, if you want to perform multiple tasks on different schedules then whether it's easier to use one Timer or multiple would depend on those schedules.
2- If I want to set timer values from any text box through GUI , can we do that? also if I want to enable/disable timer from GUI. can I do that?
It's just properties and methods, like any other object. The Interval property is type int so you assign an int value to it. Where that value comes from is irrelevant to the Timer. Similarly, the Enabled property is type bool and where the value comes from is irrelevant. Maybe it's controlled by the Checked property of a CheckBox or somewhere else. Alternatively, maybe you have two Buttons and you call the Start and Stop methods. Totally up to you.
 
Back
Top Bottom