Modbus TCP gateway

Материал из Wiren Board
Это утверждённая версия страницы. Она же — наиболее свежая версия.

Wiren Board allows to display messages from specific topics in the MQTT message queue in Modbus TCP registers. This can be useful for setting up simple interaction between the Wiren Board and SCADA systems with Modbus TCP support.


Installation and configuration on the controller

Installation

First you need to install the wb-mqtt-mbgate package. In the device console, enter

root@wirenboard:~# apt-get update && apt-get install wb-mqtt-mbgate

Settings

Gateway configuration page

After installation, you need to specify the topics that will be displayed on the Modbus TCP registers, specify their addresses and specify the format of the registers. To do this, go to the Configs section in the web interface and open the /etc/wb-mqtt-mbgate.conf configuration file.

Modbus binding

Here you can configure the port and address to which the Modbus TCP server will be available.

Required parameters:

  • Bind address - the address to which the Modbus TCP server will be available. The default value is * , which means that you can connect to any address the controller is available (for example, an IP address or a domain name).
  • Server TCP port - the port number on which the Modbus TCP server will be available. The default value is 502, the standard port for Modbus TCP.

MQTT connection

Here you can configure the parameters to connect to the MQTT message broker. By default, the gateway uses the controller's MQTT broker. It is possible to configure authentication.

Required parameters:

  • Address - the address of the message broker. By default - localhost - connection to the controller broker.
  • Port - the port number on which the broker is available. By default it is 1883.

Advanced settings:

  • Keep-alive interval
  • Enable username+password authentication - set this flag if the broker requires login and password authentication.
  • Login - user name to connect to the broker. Required if the Enable username+password authentication flag is set.
  • Password - password to connect to the broker. Required if the Enable username+password authentication flag is set.

Register bindings

In this section, you can configure topic and register matches.

This section contains a list of all found channels in the MQTT message queue, distributed by type. Channels are identified by the name of a topic: made a list of all the topics that match the pattern /devices/+/controls/+, while in the list they are shown as +/+. For example, if a message with the /devices/alarms/controls/alarm_test topic is found in the message broker, the channel will be recorded as alarms/alarm_test in this list.

If new channels appear in the message broker (for example, when a new device is connected), they will be added to the list when the service is restarted (this happens when the configuration file is saved or when the controller is restarted).

The channels are divided into 4 groups - register types in Modbus:

  • Discrete inputs- binary values (1/0) that are read-only (for example, GPIO input States);
  • Coils - registers flags - binary values available for reading and writing (for example, switches state relay);
  • Input registers - read-only numeric values (e.g. value from temperature sensor);
  • Holding registers - storage registers - numeric values available for reading and writing (for example, brightness).

The distribution takes place automatically on the basis of the description of the channel (in topics /devices/+/controls/+/meta/+).

The following options are available for discrete and numeric registers:

*Enabled - flag that enables binding of the channel to the register. By default, all channels are disabled.
  • MQTT channel - channel name in the format of +/+ (as a match to /devices/+/controls/+)
  • Meta-type - the channel type obtained from the MQTT queue. The parameter is entered only "for the user", does not affect the operation of the service.
  • Unit ID and Start address - Modbus TCP address fields. Determined by hashing on behalf of the channel; thus, the same channels on different controllers are likely to have the same address fields. Note: Unit ID 1 and 2 are reserved for the user - no channels will be automatically assigned to these addresses.

Important: Because the controller is a gateway, it assigns different Unit IDs to different devices connected to it. Some software is designed to work with one Unit ID (for example 1) on one IP address. In some programs, the start address is considered to start with 1 instead of 0. In this case, you should add 1 to the starting address from the gateway configuration when specifying it in your software.

For numeric registers, in addition to the information above, the following parameters are also available:

  • Register format - type of stored data:
    • Signed integer ;
    • Unsigned integer;
    • BCD is a binary decimal unsigned integer. (For example, a decimal value of 123 would be encoded as 0x123 == 291);
    • IEEE 754 float -floating point number;
    • Varchar - fixed size text field (1 character per case).
  • Size - the size of the data in bytes. Keep in mind that for all types except text, two bytes are stored in one register.
    • for integer types - 2, 4 and 8 bytes (respectively, in registers it will be 1, 2 and 4 registers);
    • for floating-point numbers that's 4 and 8 bytes (2 and 4 registers);
    • for text fields - any non-negative value.
  • Maximum value of the field. Required for channels of type range. All values greater than the maximum will be replaced by the maximum.
  • Value scale - multiplier of the value. For example, if the multiplier is 2 and the channel has a value of 10, the register value will be 20. Conversely, if a value of 30 has been written to the register, a message with a value of 15 will go to the broker.
  • Byte swap - changes the byte order in Modbus registers. By default, values are written to registers in the big-endian format. If this flag is set, the register will be interpreted as little-endian. Important: the flag only applies to individual registers; for compound values (larger than 2 bytes), only the order of each two-byte word changes. For example, the value 0x12345678 will be written as 0x34127856,
  • Word swap - change word order for compound values. For example, the value 0x12345678 will be written as 0x56781234.

So, you need to set both last flags to write the value in little-endian format.

General settings:

  • Remap addresses after edit - update the addresses of the devices in the case was the imposition of addresses. Useful if you want to resize the data to a larger size. When this flag is set, after saving the configuration file, it will be analyzed for address overlays; if they are found, the service will try to remove them, after which the flag will be removed. (In order to get the updated configuration file, you need to reload the page after saving). Attention! This flag can be used only on the configured system, as the addresses of any channels, including those already configured, can be changed.