I am trying to ping Socomec using the Modbus protocol, having researched, I found NModbus, the C # library. I have never used libraries or C # before (usually Java), but I have to dive directly.
I installed Visual Studio Express for C # and installed .Net. I copied the contents of the NModbus file to the project folder and added links to the two main DLLs. It did not work with .Net 4, but I redirected to 3.5 (and removed the Microsoft.Csharp link), and everything seemed to compile.
I use this example to try to connect to a slave. When I ran this and set the startAdress variable to the right one (in Socomec documentation), all I get is an empty console window.
In short, am I using the correct method / parameters, incorrect configuration / code? How to connect to this counter?
My code is:
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"))
{
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);
byte slaveId = 1;
ushort startAddress = 50536;
ushort numRegisters = 5;
ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);
for (int i = 0; i < numRegisters; i++)
Console.WriteLine("Register {0}={1}", startAddress + i, registers[i]);
Console.ReadLine();
}
}
}
}
source
share