title: create modbus service
body: <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>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&lt;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 &amp;&amp; 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&lt;plc_input.Length;j++){
  
                  Console.WriteLine("the output at " + j + " value is:" + plc_output[j]);}
  
          }
      }
  }
  </code></pre>