C# program and libraries for modbus rtu rs485.

Thread Starter

Saviour Muscat

Joined Sep 19, 2014
187
Hello helpers,

Hope I am in the right place

Currently, I am trying to interface PLC with Modbus RTU RS485 and my mentor suggested me to find c# program and libraries that are able to communicate from PC to PLC and then use them to generate log/text files for the relevant Modbus registers. I am a newbie in c# programming then I followed basic tutorials on youtube to have a general idea, I downloaded and installed visual studio 2019 and I ran some programs found on google search and GitHub but with no success, sometimes they freeze or they are too complex to run and a little bit hard to understand. Please could someone advise/ suggest, books, articles, source code, libraries to accomplish my task?

Thank you in advance for your kind help,

SM
 

Thread Starter

Saviour Muscat

Joined Sep 19, 2014
187
Hello members,

From the last post, I found a library called EasyModbus that I was able to run on C# with minimal effort. I accomplished to write coil 0 and to write holding register through Modbus but when I tried to read coil 0, I found some difficulty.

I did a tentative program on c# as follows
1 when button one is pressed coil 0 toggles to On.(works)
2 when button two is pressed coil 0 toggles to Off.(works)
3 when input 0.0 of the plc is on is it supposed to show yellow square and when it is Off a red square.(don,t work and I tried several times).
Kindly could someone indicate/advise an amendment of the program when reading the input 0.0 of the plc as explained in the third bullet, please?

Please see the attached program and tentative GUI
C#:
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 plctry
{
    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();




            var value = modbusClient.ReadCoils(0, 1);// read coil zero and if it is true change color yellow else change collor to red
            
            if (value[0] == true)
            {
                ON.Visible = true;
                OFF.Visible = false;
            }
            else
            {
                ON.Visible = false;
                OFF.Visible = true;
            }




        }

        private void button1_Click(object sender, EventArgs e)
        {
            modbusClient.WriteSingleCoil(0, true);//toggle coil zero to on
            //modbusClient.WriteSingleRegister(19,123);
          

        }

        private void button2_Click(object sender, EventArgs e)
        {
           //modbusClient.WriteSingleRegister(19, 456);
            modbusClient.WriteSingleCoil(0, false);// togglr coil zero to off
        }

      
      
    }
}

Thank You

SM
 

Attachments

ApacheKid

Joined Jan 12, 2015
1,610
I'm not quite sure what question your asking here, but I did notice this is too wordy:

Code:
           if (value[0] == true)
            {
                ON.Visible = true;
                OFF.Visible = false;
            }
            else
            {
                ON.Visible = false;
                OFF.Visible = true;
            }
This is shorter:

Code:
   ON.Visible = value[0];
   OFF.Visible = !value[0];
 
Top