2

I wrote a simple piece of code in C# to read and write to a plc via the modbus protocol (modbus RTU), my laptop being the master and the plc being the slave.I used a modbus dll that I found online. The code works fine.

My problem is that I now need to make this into a windows service. I did a simple tutorial on this and made a basic service. But when I tried to integrate this with the modbus code I'm getting no success. C# is very new to me and theres alot of things I'm not clear off. If someone could suggest the best approach to making my modbus code into a service I'd very much appreciate it, even if they could provide some type of well commented template. The C# modbus code is as follows.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
//using System.Data;
using Modbus.Data;
using Modbus.Device;
using Modbus.Utility;
//using System.ServiceProcess;
using System.Collections;

namespace Simple_modbus_write
{
    class MainClass
    {
        public static void Main (string[] args)
        {

            SerialPort port = new SerialPort("COM6");

            // configure serial port
            port.BaudRate = 9600;
            port.DataBits = 8;
            port.Parity = Parity.None;
            port.StopBits = StopBits.One;
            port.Open();

            // create modbus master
            IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);
            byte slaveId = 1;
            ushort outputAddress = 2; // this is the address of the first output of the plc
            ushort inputAddress = 0; // this is the address of the first input of the plc
            ushort numRegisters = 2;


            // write to three coils
            master.WriteMultipleCoils(slaveId,outputAddress, new bool[]{true});

            // delay of 10 seconds
            Thread.Sleep(10000);


            // read the inputs of the plc
            bool[] plc_input = master.ReadInputs(slaveId,inputAddress,numRegisters);


            for (int i=0; i<plc_input.Length;i++){

                // write the status of the plc inputs (i.e. true or false)
                Console.WriteLine(" the input at " + i + " value is:" + plc_input[i]);}



            // if the input is false (i.e. switch is off) then turn off the output
            if(plc_input[0]==false && plc_input[1]==false)

            {
                master.WriteMultipleCoils(slaveId,outputAddress, new bool[]{false});
            }



            // at the end read the status of the output and print its status to the screen
            bool[] plc_output = master.ReadCoils(slaveId,outputAddress,numRegisters);

            for (int j=0; j<plc_input.Length;j++){

                Console.WriteLine("the output at " + j + " value is:" + plc_output[j]);}

        }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2390108
  • 21
  • 1
  • 2
  • 1
    what exactly do you mean by `But when I tried to integrate this with the modbus code I'm getting no success`? What isn't working in a service? – Axarydax May 16 '13 at 14:54
  • Hi Axarydax , basically what I meant is that I created a windows service from a tutorial online, so i have a very basic sevice that just writes logs. Then I tried to put the above modbus code into it so that I'd have a service that communicates with the PLC – user2390108 May 16 '13 at 15:49
  • @user2390108 and what isn't working? – kenny May 30 '13 at 09:57
  • I realise this is an old thread, but I'm having a similar problem. If anyone else is investigating this, I think it's caused by service dependencies. I'm trying modbus over TCP and had to add the Tcpip service dependency – Jered Jul 26 '16 at 05:35

1 Answers1

0

I have also done a similar project,

You can refer to the following readme.md instructions

Modbus Server/Client

https://github.com/asgardpz/Modbus_Server

https://github.com/asgardpz/Modbus_Client

Modbus Master/Slaver with HslCommunication

https://github.com/asgardpz/Hsl

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – MD. RAKIB HASAN Jun 09 '22 at 09:27