Just about NModbus (RTU)

This article is intended for beginners.

Theory


Modbus is a communication protocol based on the master-slave architecture. Uses RS-485, RS-422, RS-232 interfaces, as well as Ethernet TCP / IP networks for data transfer.

NModbus is a large C # library that includes the implementation of all protocol operation modes. The model for implementing the classes of this library allows you to work with any Modbus device, but only with one, since the library classes encapsulate a port in itself, preventing synchronization between several Modbus objects. This protocol is quite popular when developing different peripherals for a smart home, as well as the Internet of things.

Teams
, , TCP/IP .

So, in order to manage something, we need to know what and where to send. So we need a register card.

I’ll clarify that the register cells are:
1. Read only
2. Read and write
3. Write only (confirmation of the record is permissible to give an error code)

The simplest card looks something like this:
Screenshot


Also, there are various explanations, for example, what maximum / minimum value can be transmitted to the address, etc.

Practice


I use the trial version of the simulator from Modbus Tools and COM ports from MOXA with RS-485 interface.

COM port


Once you have created the project, you need to integrate NModbus into it. I recommend using VisualStudio, since it is quite easy to do it with NuGet, as shown below:

1st step


2nd step


In ModbusTools we need to set the connection parameters (they may differ for you, these are suitable for my COM ports):

The parameters open on F3, or on the “Connection” tab.

For Master and Slave, the settings should match, except for the item with COM ports.

Screenshot


After setting, you can click “OK” and the devices will connect to each other. Either this does not happen and an inscription appears that the connection was interrupted for N seconds, or it is completely absent. In this case, check the settings and connection, make sure that the drivers for COM ports are installed correctly and work correctly.

Simple program example
using Modbus.Device;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Modbus
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort serialPort = new SerialPort(); //Create a new SerialPort object.
            serialPort.PortName = "COM1";
            serialPort.BaudRate = 9600;
            serialPort.DataBits = 8;
            serialPort.Parity = Parity.None;
            serialPort.StopBits = StopBits.One;
            serialPort.Open();
            ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(serialPort);

            byte slaveID = 1;
            ushort startAddress = 0;
            ushort numOfPoints = 1;
            ushort[] holding_register = master.ReadHoldingRegisters(slaveID, startAddress,
            numOfPoints);
            Console.WriteLine(holding_register);
            Console.ReadKey();
        }
    }
}


Screenshot


Important: if you are developing something that is more complicated than the example above, you must ensure that packages are not lost. There is a need to calculate checksums, you can read about it here.

Source: https://habr.com/ru/post/undefined/


All Articles