Table of Contents

Modbus protocol driver

The Modbus protocol driver is probably the most frequently used driver. There are rich possibilities of Modbus datapoints configuration setting in Mervis IDE, and this is why we will have a look on the datapoint definition in more detail.

A datapoint defined in a Modbus driver is not available in the program, it is just a communication definition. In a program, we need variables to work with. Hence, it is necessary to map (attach) a datapoint to a global variable. This abstract enables the engineer to reconnect easily a variable from one datapoint (input or output) to another, e.g. in case of physical damage of an I/O module. Global variables can be created and attached to datapoints also on an automatic basis, using the Autogen function.

As a first thing, a communication channel has to be created. This is to specify on which physical port shall the PLC communicate to the Modbus server: a serial line (for Modbus RTU) or the Ethernet interface (for Modbus TCP) can be used.

Modbus Client Channel

Modbus channel

The following parameters must be identical both for the channel and for all the Modbus devices connected to this channel. Please follow the 3rd party documentation. If the devices have selectable parameters, check if the settings correspond with the channel settings in the PLC.

These parameters have no meaning for Modbus TCP communication and are not displayed if Modbus TCP is selected.

Right click the channel to add a Modbus slave (Device). A device is a Modbus server (Modbus address) with which we want to communicate, and contains Modbus registers. There may be up to  255 devices on a bus, however, maximum of 60 to 80 devices is used for easier commissioning and service. Now, set the device parameters:

Modbus - zařízení

Device properties

Modbus Device Parameters

Prototype Definition

Device info

Double click the device to open a workspace for Groups and datapoints definition in a device. The Modbus communication follows in groups of datapoints. There are more consecutive registers communicated within a single Group (which is one Modbus request for reading or writing). Group communication optimizes bus traffic and speeds up the data transfer rate. Datapoints are always defined as members of a particular Group.

Firstly, define a Group for reading of registers. This is necessary even if only a single register has to be read.

Skupina

Modbus Group Parameters

Example: A Group must read Modbus registers 11 to 15. The Group parameters will be set as follows: Starting Element to 11, Quantity of Elements to 5.

Secondly, the datapoints will be defined, to which the driver reads values from the Group communication.

Modbus dat. bod

These are the predefined transformations

Autogen

Mapping

Modbus Data Point Parameters

Definition of from which register and how the data point will be read. The main data, which is Modbus function and starting, are defined in the Group which the data point is assigned to. All other settings are related to the first register of the Group. As an example, a group of registers is defined which starts with Modbus register 2064 and contains a total of 8 registers:

The byte order (MSB, LSB) is the same as in the Modbus telegram. The driver reads the Group data in this order, and this must be taken into account when parsing the Group into the data points.

Write offset gap

Interface number of the offset where the writing data ends (if the data point is writable). Counted in bytes. Example: to write to register 2068 the Write offset gap would be 10. Please check that Write offset gap = Data offset + Multibyte length.

Data offset (Parser)

Starts from 0; specifies on which byte (not register!) the data of a datapoint start. Example: when reading from register 2068 the Data offset would be 8. Maximum value is 2 * Quantity of Elements - 1, which would read the last byte in the group. Note that in a Modbus telegram the higher byte (MSB) is transmitted as first, and the lower byte (LSB) as second; when mapping into byte-based data point types (BYTE, SINT, USINT) the higher (more significant) byte of a Modbus register has lower value of the Data offset.

Bit offset (Parser)

Moving in a byte between single bits - for bit-based datapoints read by functions F01 Read Coils, F02 Read Discrete Inputs or written by functions F05 Write Single Coil, F06 Write Single Register. Starts at 0, so to read the 4. bit (bit 3) in a byte (note - not necessarilly in a register!) the Bit offset = 3. The settable range is 0 to 7.

Multibyte length (Parser)

Number of bytes (not registers!) to be read into a data point or to be written into the Modbus table. It is mostly 2, as one Modbus register has 16 bits = 2 bytes. In special applications as e.g. cumulated values in energy meters, which use 4 bytes, the Multibyte Length may be higher. For bit-oriented data points the Multibyte Length = 1 (1 byte is enough - the one which contains the required bit).

Multibyte Order (Parser) In this string it is possible to change the byte order in which the values from the telegram will be mapped on the data type of the data point. Changing the order is used to fix problems with variable formatting, such as “rotate”, “non-rotate”, “byte swap”, “word swap”, “Intel byte order”. Default value is “12345678”, and usually it is not necessary to change it.

The rules described above can be summed up into four paragraphs

1. Reading of holding / input registers

If the holding/input registers are read, it has to be specified where to start within a group and how many bytes to read. The group is always defined by its starting register and number of elements. These numbers represent Modbus registers and length of the telegram.
After a new data point has been created, it has to be assigned to a group, and its data type must be specified. The data type must match the read value and it is important that it has equal number of bits. The data types description is listed in the elementary data type table.

The data point setting specifies which and how large part of the whole group telegram belongs to this data point. Values in this table are represented by bits and bytes rather than by registers. A register has 2 bytes = 16 bits.

Multibyte length specifies how many bytes belong to a data point. This value must match the selected data point type. E.g. for the INT data type, which has 16 bits, the value is 2.

Then it must be specified in which part of the telegram the reading shall start. For reading of registers, this parameter is set in the Data Offset field.
The first read byte has Data Offset of 0, the second byte has Data Offset of 1, etc. Thus, if the data type is INT and value from the 3rd register in the group has to be read, which is 5.-6. byte in the telegram, the Data Offset is 4.

Using function for reading of register it is also possible to read the bits. The byte decomposition follows in the device and it needs not to be programmed in the application.

Using Data Offset, move to the byte that has to be read, and use Bit Offset to move in the byte between bits. For this kind of datapoint, the datapoint type must be bit or bool.

Note also that the byte order in a telegram which is depicted on the figure is MSB –> LSB. If the LSB of the first register shall be read, the Data Offset must be 1. If the datapoint type is a 8 bit type and the MSB shall be read, the Data Offset is an even number. If the LSB shall be read, the Data Offset is an odd number.

2. Writing of holding registers

The parameters for writing into registers are defined similarly to the definition of the reading from registers. The only difference is that for writing it is necessary to define also the number of the last byte to write to.

If the first 2 bytes of a telegram shall be written, the Multibyte Length is 2, Data Offset is 0, and Write Offset Gap is 2. It means that 2 bytes are written into the interval between interface 0 and interface 2.

If the next two bytes shall be written, the Data offset is 2 and Write Offset Gap is 4.

In the same way as the single bits could be read, they can be written, too. Using the Data offset parameter move on the required byte in the telegram, and using Bit offset move to the required bit in this byte. It must be taken into account that the whole byte is written, or the whole register. Thus, if only a single bit shall be written, the other bits which are not defined as data points will be filled with zeros.

In general, if a Group has more registers, the registers must be consecutive (without gaps). So if registers 1-2-4-5 shall be written and register 3 must be untouched, two separate Groups for writing must be created.

If only several bits shall be written, it is easier to write the value e.g. as a INT number, where the bit composition is arranged in a program. If the device manufacturer provides also bit-oriented functions, it is advised to use them for writing.

3. Reading coils/discrete inputs

If the coils/discrete inputs table is read, the parameters are not defined using the Register number. The bit-oriented table does not work with registers at all. Instead, the coil number is defined. So if 10 coils shall be read starting on coil No. 150, the Starting Element of the Group is 150 and Quantity of Elements is 10.

The data type of the data points will be bit.

The Multibyte Length will be set to 1. Moving in a byte telegram is arranged using combination of Bit Offset and Data Offset. The Bit Offset can be entered in the range of 0 to 7.

If the Bit Offset is higher than 7 bits, the Data Offset must be increased by one, and the Bit Offset starts at 0. Example: If the data point shall be read with the shift by 35 bits, the Bit Offset is 2 and Data Offset is 4.

These parameters can be calculated easily:

4. Writing of coils

Writing of coils is set in a very similar way to reading of coils. Again, the only difference is the Write Offset Gap.
If the Data Offset parameter is 0, the Write Offset Gap is 1. If the datapoint moves in the telegram so as the Data Offset is larger than 0, the Write Offset Gap is always Data Offset + 1.

The bit shift is set in the same way as at Reading coils.

Optimizing the Modbus mapping engineering

To map the Modbus parameters more efficiently and faster, they can be edited right from the communication channel editor. The edisor is displayed by double click on the communication channel or right click to the communication channel + Open editor.

The “Data Offset”, “Bit offset”, “MultiByte Length” and “Write offset gap” allow faster editing of Modbus parameters.

Mapování modbusových parametrů

Modbus device export

To export a Modbus device to a CSV file, right click the Device - Export device.

Export zařízení