How to measure a counter using Modbus

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"))
            {
                // 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 < 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
        }
    }
}
+3
source share
2 answers

Why don't you use some Java MODBUS library when you are already familiar with Java? I did not work with Socomec counters, but in general for MODBUS devices you need to know the protocol and addresses that interest you. Then try to read tags from the device using some tool that, as you know, works well, for example MODPOLL . , , , , . , , . ... , MODBUS ASCII. , 95% RS232/RS485, , MODBUS RTU. , .

+5

Java Windows. Tomcat Java, Java. , , .

, -

"% ~ dp0windows\tomcat6" //IS// % 1 --DisplayName% 1 -- "gmServer for% 1" ^  --JavaHome "% JAVA_HOME%" --Classpath "% PR_CLASSPATH%" --LogPrefix gmserver ^  --StartMode jvm --StopMode jvm --Jvm auto --StartPath "% ~ dp0." ^  --LogPath "% ~ dp0." --LogLevel debug --StdOutput% 1.out --StdError% 1.err ^  --StartClass greenMonitor.gmServer --StartParams -I #% # -u # 3600 ^  --StopMethod windowsService --StopParams stop --StopTimeout 10

( "^" ) .BAT . Tomcat Tomcat.

Modbus Java , , j2mod Sourceforge. jamod, , .

+1

All Articles