Author Topic: How to stucture a program in C# for Modbus RTU communication  (Read 1841 times)

kallileo

  • Newbie
  • *
  • Posts: 7
    • View Profile
How to stucture a program in C# for Modbus RTU communication
« on: October 26, 2018, 02:40:29 AM »
Hi,

I want to read 3 Modbus registers from a level measuring device.
What I do now is to list all the available com ports and then after selecting the correct one I start reading and displaying the 3 registers by using BasicLabel controls.

It works but this is not how I want it to work.
Is it possible to establish Modbus communication and then start reading Modbus data?

So I use the following code:

Code: [Select]
private void MainForm_Load(object sender, EventArgs e)
        {
            //show list of valid com ports
            foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
            {
                comboBox1.Items.Add(s);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            modbusRTUCom1.PortName = comboBox1.SelectedItem.ToString();
        }

        private void modbusRTUCom1_ConnectionEstablished(object sender, EventArgs e)
        {
            button1.Text = "Connected";
            Timer timerReadPLC = new Timer();
            timerReadPLC.Interval = 500;
            timerReadPLC.Tick += new EventHandler(timerReadPLC_Tick);
            timerReadPLC.Start();
        }

        void timerReadPLC_Tick(object sender, EventArgs e)
        {
                litersMod.Text = modbusRTUCom1.Read("30001").ToString();
                kilosMod.Text = modbusRTUCom1.Read("30003").ToString();
                int x = Int32.Parse(modbusRTUCom1.Read("30002")) / 10;
                levelMod.Text = x.ToString();
        }

Is it a proper way to make a program?

Also I don't really undestand what modbusRTUCom1.Subscribe and modbusRTUCom1.BeginRead methods do.


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to stucture a program in C# for Modbus RTU communication
« Reply #1 on: October 26, 2018, 03:31:53 AM »
The 6th post in this thread explains the different reading methods:

https://www.advancedhmi.com/forum/index.php?topic=2086.msg11879#msg11879

The built in controls use subscriptions and encapsulate all of the complexity of it along with error handling. By not using the BasicLabel you are bypassing all of this and put it in the hands of your code.

kallileo

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to stucture a program in C# for Modbus RTU communication
« Reply #2 on: October 26, 2018, 05:11:24 AM »
Archie,

The problem I have with BasicLevel is that I can't set scaling to "0.1" as Visual studio it doesn't accept "." as delimiter and it doesn't work propely when I put ",".
« Last Edit: October 26, 2018, 05:48:00 AM by kallileo »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to stucture a program in C# for Modbus RTU communication
« Reply #3 on: October 26, 2018, 08:35:45 AM »
The problem I have with BasicLevel is that I can't set scaling to "0.1" as Visual studio it doesn't accept "." as delimiter and it doesn't work propely when I put ",".
I'll check into this because the latest version of 3.99x should have everything setup for localization unless something crept into one the latest revisions.