title: How to ping meter with Modbus
body: <p>I am trying to ping a <strong>Socomec</strong> meter using the Modbus protocol, having researched, I found NModbus, a C# library. I have never used libraries or C# before (normally Java), but I have to dive right in.</p>
  
  <p>I set myself up with Visual Studio Express for C# and installed .Net. I have copied then contents of the NModbus file into my project folder and added the references to the two main DLLs. Its didn't work with .Net 4, but I retargeted to 3.5 (and removed the Microsoft.Csharp reference) and things seemed to compile.</p>
  
  <p>I am using <a href="http://web.archive.org/web/20090601225835/http://www.nmodbus.com/Samples.aspx" rel="nofollow">this sample</a>, below, to attempt to connect to the slave device. When I run this, and set the startAdress variable to the desired one (found in Socomec documentation) however all I get is a blank console window.</p>
  
  <p>In short, am I using the correct method/parameters, is my setup/code incorrect? How do I connect to this meter?</p>
  
  <p>My code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.IO.Ports;
  using Modbus.Data;
  using Modbus.Device;
  using Modbus.Utility;
  
  
  namespace NModbus
  {
      class SerialMaster
      {
          static void Main(string[] args)
          {
              ModbusSerialAsciiMasterReadRegisters();
          }
  
          public static void ModbusSerialAsciiMasterReadRegisters()
          {
              using (SerialPort port = new SerialPort("COM1"))
              {
                  // 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.CreateAscii(port);
  
                  byte slaveId = 1;
                  ushort startAddress = 50536;
                  ushort numRegisters = 5;
  
                  // read five registers       
                  ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);
  
                  for (int i = 0; i &lt; numRegisters; i++)
                      Console.WriteLine("Register {0}={1}", startAddress + i, registers[i]);
                      Console.ReadLine();
              }
  
              // output:
              // Register 1=0
              // Register 2=0
              // Register 3=0
              // Register 4=0
              // Register 5=0
          }
      }
  }
  </code></pre>