May 30, 2020

Configuration of Modbus RTU and Modbus TCP in Redul PLC

Configuration of Modbus RTU and Modbus TCP in Redul PLC

This tutorial contains information on setting up Modbus communication on the Redul BX00 programmable logic controllers. Setup is done using Sigma LD software.

Introduction

General Information

The Redul controller software allows you to configure it both as a Modbus Master and as a Modbus Slave. These implementations are independent of each other. The controller can be a “master” on one of the communication lines, and on the other, a “slave”.

As a Modbus Master, the controller polls the slaves via the RS-232 serial line | RS-485 (Modbus RTU | ASCII modes) or via Ethernet (Modbus TCP mode). As a Modbus Slave, the controller also operates in three modes (RTU | ASCII | TCP).

The following functions of the Modbus protocol are supported:

  • Reading and writing relay states Read Coils (0x01), Write Single Coil (0x05), Write Multiple Coils (0x0F)
  • Read Discrete Inputs (0x02)
  • Read and write storage registers – Read Holding Registers (0x03), Write Single Register (0x06), Write Multiple Registers (0x10)
  • Read input registers – Read Input Register (0x04)
Recommended Documents

For more information on configuring other parameters of the Redul BX00 series controllers in the Sigma LD development environment, it is recommended that you read the following documents:

  • Sigma LD Software. User Manual.
  • Redul B600. System Manual.
  • Redul B500. System Manual.
  • Redul B400. System Manual.
  • Redul B200. System Manual.

Getting Started

  • Install Sigma LD software on your computer. A description of the process of installing the program, as well as instructions on how to use the program, are given in the document “Sigma LD Software. User’s manual”. The installation program and documentation are available Support / Downloads.
  • Launch Sigma LD. Create or open a project. Make sure that there is a controller in the project that will participate in the data exchange via the Modbus protocol. If the controller is missing, add it using the Redul Configuration Wizard (see the Sigma LD Software User Guide).
Principle of Adding Devices

To add a device, place the cursor on its name (in the device tree window), click the right mouse button. In the context menu that appears, select Add device … (Figure 1).

Figure 1. Context menu

The Add Device window opens where by default the list of devices that is currently available for the user to insert is displayed, for example, when adding a crate the list of crates, when placing modules in the crate the list of modules, to configure Modbus the serial port, slave and master devices (Figure 2).

Figure 2. Add Device window

Select the desired device, click the Add device button or double-click the left mouse button. The selected device will appear in the project in the device tree.

In the device tree you need to connect (depending on the situation) not only devices that correspond to real-life modules, but also virtual devices that do not have a hardware equivalent. For example, Modbus devices are virtual and are designed to configure the parameters of the IEC library that is automatically loaded into the project when a specific device is connected.

When configuring various modes, Modbus devices are connected to the corresponding serial port or directly to the controller. In the figure 3 are shown examples of configurations when configuring various Modbus modes:

  • Option 1 – Modbus Serial Master, where the communication channel is the built-in serial port of the controller
  • Option 2 – Modbus Serial Slave, the communication channel is also an integrated serial port
  • Option 3 – Modbus Serial Master on the serial port of the communication module
  • Option 4 – Modbus Serial Slave on the serial port of the communication module
  • Option 5 – Modbus TCP Master
  • Option 6 – Modbus TCP Slave

Figure 3. Device configuration for various Modbus modes

Modbus ASCII and Modbus RTU Configuration

Adding a Serial Port

Before configuring Modbus Serial, you must add a serial port to the controller configuration.

Adding a Port

If any of the RS-485/232 ports of the processor module are used for data exchange, then the Redul Serial Port device must be added to the controller in the configuration (Redul -> Serial Port -> Redul Serial Port). The number of possible devices of this type is limited by the number of physical RS-485 (RS 232) ports on the central processing unit.

Figure 4. Redul Serial Port settings

If the communication module port is used for data exchange then the Extended Redul Serial Port device (Redul -> Serial Port -> Extended Redul Serial Port) must be added to the communication module in the configuration. The number of possible child devices is limited to the module model.

Figure 5. Serial port settings of Extended Redul Serial Port

Port Settings

Double-click the port name to open the main tab of the port settings. Go to the internal tab Serial Port Settings (Figure 4, Figure 5). Set the switch in the Port number field (matches the number indicated on the module). Next, selecting a value from the drop-down list, set the following parameters:

  • The speed is from 9600 to 115200
  • Number of data bits – values: 8.7, 6, 5, 4
  • Parity – parity, values: None, Odd, Even
  • Duration of stop-bit – 1 or 2
Configuring Modbus Serial Master

In the case when the controller will be used as the master polling device, the Modbus Serial Master configuration is necessary. When you configure this mode, you need to set the parameters of the slave device that will be polled by the controller. In addition, it is required to describe the data set that will be requested by Modbus.

Add a Modbus Serial Master device to the Redul Serial Port or Extended Redul Serial Port (Redul -> Modbus -> Serial Modbus Master -> Modbus Serial Master). Double-click the Modbus Serial Master device name to open the parameters tab (Figure 6). Selecting the check box in the Debug mode field enables debug mode (additional information about the operation will be recorded in the controller log).

Figure 6. Setting Modbus Serial Master parameters

Next, one or several external slave devices (outer slaves) that will be polled by the controller (Redul -> Modbus -> Serial Modbus Master -> Modbus Serial Outer Slave) need to be connected to the Modbus Serial Master device. The maximum number is limited by the range of available Modbus addresses. Double-click the Modbus Serial Outer Slave device name to open the parameters tab. By default, the first inner tab of the Modbus Setup tab modbus serial outer slave is opened (Figure 7).

Figure 7. Setting parameters for Modbus Serial Outer Slave

Checking the box in the Debug mode field enables debug mode.

Set the following parameters:

  • Slave ADR is the slave address of the device according to the Modbus protocol. Each of the devices (outer slave) on the common polling bus (serial line) must have a unique address in the range from 1 to 247
  • Responsetimeout – time to wait for a response to a request. The default is 1000 ms
  • Behavior in STOP mode – behavior in STOP mode. Indicates what to do if the RUN | STOP switch of the controller is set to the STOP position: No activity option means stop polling, the Normal work option means continuing to work in normal mode (the Modbus operating cycle does not depend on the Run | Stop controller)
  • Serial Mode – polling mode. Possible values: ASCII or RTU

Note: it is possible to independently activate “behavior in STOP mode” in the program code. This may be necessary, for example, in a redundant system if the slave controller is required not to conduct a poll. When adding a Modbus device an instance of the corresponding function block of the same name is also created. To activate the mode the program requires the instance of the Modbus Serial Master function block (not a slave!) to set the property TRUE to the ActivateStopBehavior property:

Modbus_Serial_Master.ActivateStopBehavior:=TRUE;

After that all slaves connected to this master will switch to STOP mode.

Next, you need to describe the data that the controller will request from the slave devices. Go to the internal tabs of the channels modbus slave. Each Modbus request is presented in the form of a so-called “modbus slave channel”. To set the description of the new channel, click the Add Channel button, the dialog box opens. (Figure 8).

Sigma LD - Modbus channels

Figure 8. Adding a Modbus Slave channel

The Edit button opens the same form, but in the edit mode of an existing channel. The Delete button deletes the description of the specified Modbus channel.

Set the following parameters:

  • Name – friendly name (human readable)
  • Function Code – the number of the Modbus function used to read / write data
  • Channel type – if you select the Timer value, this request will be executed at the frequency specified in the Call interval (ms) parameter, if you select the Trigger value the request will be executed on an event (then you need to specify the variable that triggers the event)
  • Offset – the address of the first requested item, according to the Modbus protocol
  • Length – the number of requested items (registers or flags depending on the function code)
  • Call interval (ms) – the frequency of polling this channel, in milliseconds
  • Description – optional text description

Figure 9 shows two examples: adding a channel to read 32 discrete values with a periodicity of 1000 ms and adding a command to set a single flag (single coil) with number 10 per event.

Figure 9. Modbus Slave channel examples

The added channels on the Modbus slave channels tab are as follows:

Figure 10. Tab Modbus slave channels

Next, go to the Modbus Serial Outer Slave I/O Mapping tab. The Connect parameter shows the connection status with the device based on the result of the last request (no response – no connection) (Figure 11).

Sigma LD - Modbus Outer slave I/O mapping

Figure 11. The Connect parameter displays the connection status with the device

For the data received from the slave device to be used in the controller program the I/O Ratio tool is used. It allows you to compare the data of the Modbus channels with the user variables of the controller program.

Sigma LD - Modbus Outer slave I/O mapping

Figure 12. Tab Modbus Serial Outer Slave I/O Mapping

Note: in the terminology of the development environment the channel is any input or output parameter of the device. To avoid confusion with the Modbus channels the channels listed in the table in Figure 12 are called parameters in this section.

Each Modbus channel has several such parameters.:

  • Data – actually transmitted Modbus data block. It is presented as an array of WORD elements (in fact, registers for register channels) or as an array of bytes (for bit / discrete channels). The development environment performs bit-wise copying: for inputs – from the Modbus data block to the controller variable, for outputs – from variables to the Modbus driver
  • Status – shows the current status of the channel. Possible values are described by the ChannelStatus enumeration declared in the PsModbusSerialMaster library. To track the channel status in the controller program it can be associated with a variable of type BYTE
  • Trigger – appears at the appropriate setting in the Channel Type field

When associating a variable with a Modbus data block (Data parameter) the following condition must be met – the size of the variable must be sufficient, i.e. be greater than or equal to the size of the data block.

The following is an example variable declaration for binding to a Modbus data block.

VAR_GLOBAL
    ai8:ARRAY [1..8] OF REAL;	 //8*4bytes reals=32bytes=16registers
    di32:UDINT;			//double unsigned int
    cmd_val: BOOL;		//command value
    cmd_trig: BOOL;		//command trigger
END_VAR

The following channel statuses are used when declaring a channel status transfer in the PsModbusSerialMaster library (Table 1):

Table 1. Channel statuses in the PsModbusSerialMaster library

To associate an input / output parameter with a variable, on the Modbus Serial Outer Slave I/O Mapping (Figure 12) double-click in the line of the desired channel. A button appears that opens the Input Assistant window (Figure 13). Find the desired variable. If the check box in the Structured view field is checked then open the lists using the button. If the check box is cleared and the variables are presented in one large list use the filter to make the search easier.

Figure 13. Input Assistant dialog box

The Modbus Serial Outer Slave Tab_The ratio of inputs / outputs, where the channel has already been assigned to variables, is as follows (Figure 14):

Sigma LD - Modbus Outer slave I/O mapping

Figure 14. Tab Modbus Serial Outer Slave I/O Mapping

When polling for an event, you must associate the Trigger parameter with a variable of type BOOL. The event will be the switching of the variable from the FALSE state to the TRUE state. The Modbus driver does not resets the trigger. To repeat the request, you must independently transfer the variable to the FALSE state and in the next cycle of the controller’s task to re-trigger the event.

Configuring Modbus Serial Slave

In the case when the controller will be a slave device and will be polled by the local control or data acquisition system the Modbus Serial Slave setting is necessary.

Add a Modbus Serial Slave Device to the Redul Serial Port or Extended Redul Serial Port (Redul -> Modbus -> Serial Modbus Slave -> Modbus Serial Slave Device). Double-click on the name of a Modbus Serial Slave Device device, open the parameters tab. By default, the first internal tab of the modbus configuration serial slave opens (Figure 15).

The added channels on the Modbus slave channels tab are as follows:

Figure 15. Configuring parameters for Modbus Serial Slave

Selecting the check box in the Debug mode field enables debug mode (additional information about the operation will be recorded in the controller log).

Set the following parameters:

  • Slave ADR is the slave address assigned to the controller according to the Modbus protocol
  • Behavior in STOP mode – behavior in STOP mode. Indicates what to do if the RUN | STOP switch of the controller is set to the STOP position — No activity option means that the controller stops responding; Normal work option means continuing to work in normal mode (Modbus duty cycle does not depend on the Run | Stop controller). If Modbus Exception is selected, the response to any Modbus request will contain error code 06 (Device busy)
  • Serial Mode – polling mode. Possible values: ASCII or RTU
  • ASCII Mode Timeout – timeout for the end of the message (line feed and carriage return – LF, CR) in milliseconds

Note: it is possible to independently activate “behavior in STOP mode” in the program code. To activate the mode it is required in the program to set the ActivateStopBehavior property to TRUE on the instance of a Modbus Serial Slave device:

Modbus_Serial_Slave_Device.ActivateStopBehavior:=TRUE;

After that, the slave device will switch to STOP mode.

Go to the internal tab of the modbus slave data area, where the description of the Modbus data area available for querying from the external Master devices is placed. To set a description of the new area, click the Add Data Area button, the dialog box opens (Figure 16).

Sigma LD - Modbus data areas

Figure 16. Adding a Modbus Slave data area

The Edit button opens the same form, but in the edit mode of an existing data area. The Delete button deletes the description of the specified Modbus Slave data area.

Set the following parameters:

  • Name – name of the area (human-readable)
  • Type – Modbus data type (Discrete Inputs, Coils, Input Registers, Holding Registers)
  • Offset – the address of the first element available for the request, according to the Modbus protocol
  • Length – the number of elements available for the request (registers or flags, depending on the type of data Modbus)
  • Variable name – here you specify the variable (controller program) in which the transmitted data is stored the size of the variable must be at least the size of the declared Modbus data area (the Length field)
  • Description – optional text description

To fill in the Variable name field, click in this field the button that opens the Input Assistant window. Find the desired variable. If the check box in the Structured view field is checked, then open the lists using the button . If the check box is cleared and the variables are presented in one large list use the filter to make the search easier.

Figure 17 shows an example when 4 data fields are created, one for each Modbus data type. The size of each data area is 2000 elements. For data areas such as Coils and Discrete Inputs, the data element is a bit, respectively, the size of the variable to be bound in bytes is calculated by the formula: (channel length-1) / 8 + 1. For data areas of type Input Registers and Holding Registers, the data element is a register that corresponds in size to the WORD type in the development environment. In our example, arrays coils_area and di_area, corresponding to 250 bytes in size (Figure 18). They are associated with data fields of the type Coils and Discrete Inputs, respectively (Figure 18). The arrays ir_area and hr_area, respectively each with 2000 WORD elements are associated with the Input Registers and Holding Registers data areas (Figure 18)

Figure 17. Examples of Modbus Slave data areas

Note: Variable binding is implemented at the PsModbusSerialSlave library level, and not using the I/O Mapping mechanism, as in previous versions.

Sigma LD - Modbus communication variables

Figure 18. Variable declaration for Modbus channels with M-Memory and references

When working with data transmitted via Modbus, the strategies described below can be applied.

The first strategy is to use the M-memory controller. For each data type Modbus is created in one variable with a size sufficient to accommodate all transmitted values. The variable is declared with the AT directive and an indication of the address in the M memory. Next, you should create one Modbus channel for each data type (Discrete Inputs, Coils, Input Registers, Holding Registers) linking them with previously declared variables. These variables fulfill only the role of data buffers available for reading and writing. The data that is required to be transmitted via Modbus may have any type of development environment and must be declared also with the AT directive located at the addresses that fall into the corresponding buffer. This approach is implemented in the example shown in Figures 17 и 18 (marked as “Work Option 1”). The arrays coils_area, di_area, hr_area, ir_area are data buffers, the variables coils1, coils2, di1, di2, hr1, hr2, ir1, ir2 are test data elements placed in the specified buffers.

The second strategy is to link the channel with the structure (data type STRUCT). When you declare a structure in the development environment, you need to specify in it all the fields that are intended to be issued via Modbus, then create a channel and associate it with a variable — an instance of this structure.

Sigma LD - Modbus communication code

Figure 19. An example of linking a channel with a structure

Modbus TCP Configuration

Configuring Modbus TCP Master

In the case when the controller will be used as a master polling device, the Modbus TCP Master configuration is necessary. When you configure this mode, you need to set the parameters of the slave device that will be polled by the controller. In addition, it is required to describe the data set that will be requested by Modbus.

Add a Modbus TCP Master device to the controller (Redul à Modbus à TCP Modbus Master Modbus TCP Master). Using a double click on the name of the Modbus TCP Master device open the parameters tab (Figure 20).

Figure 20. General parameters of Modbus TCP Master

The general parameters of the device include the parameters, which usually do not need to be changed:

  • Socket Timeout– it is a timeout in milliseconds for a select operation ();
  • Reconnect interval– if there is no TCP connection, after this time interval expires (in milliseconds), an attempt will be made to re-establish the TCP connection.

Next, one or several outer slaves (outer slaves) that will be polled by the controller (Redul à Modbus à TCP Modbus Master à Modbus TCP Outer Slave) need to be connected to the Modbus TCP Master device. Double-click the Modbus TCP Outer Slave device name to open the parameters tab. By default, the first inner tab of the modbus Tcp outer Slave Setup opens (Figure 21. Configuring parameters for Modbus TCP Outer Slave).

Figure 21. Configuring parameters for Modbus TCP Outer Slave

Selecting the check box in the Debug mode field enables debug mode (additional information about the operation will be recorded in the controller log).

Set the following parameters:

  • Unit ID– this is the address of the polled device. It may be required for some specific devices that require explicit indication of the Modbus Slave address;
  • Timeout– waiting time for a response to a request. The default is 1000 ms.
  • IP Address– address of the slave device
  • Port– TCP port number opened on the slave to connect
  • Behavior in STOP mode– behavior in STOP mode. Indicates what to do if the RUN | STOP switch of the controller is set to the STOP position — No activity option means stop polling, Normal work means continuing normal operation (Modbus operation cycle does not depend on Run | Stop of controller), Close Connection means that the connection will be closed.

Note: it is possible to independently activate “behavior in STOP mode” in the program code. To activate the mode, in the program, the corresponding ActivbusStopBehavior property of the corresponding Modbus TCP Master function block of the same name is set to TRUE:

Modbus_Tcp_Master.ActivateStopBehavior:=TRUE;

After that, all slaves connected to this master will switch to STOP mode.

Next, you need to describe the data that the controller will request from the slave devices. This procedure and further settings are completely analogous to those described in the Configuring Modbus Serial Master section.

Configuring Modbus TCP Slave

In the case when the controller will be a slave device and will be polled by the local control or data acquisition system, the Modbus TCP Slave setting is necessary.

Add a Modbus Tcp Slave Device to the controller (Redul -> Modbus -> TCP Modbus Slave -> Modbus Tcp Slave Device). Double-click on the name of the Modbus Tcp Slave Device device, open the parameters tab. By default, the first internal tab of the modbus Tcp slave Setup opens. (Figure 22).

Selecting the check box in the Debug mode field enables debug mode (additional information about the operation will be recorded in the controller log).

Set the following parameters:

  • TCP Port– port number listened by the Modbus driver
  • Behavior in STOP mode– behavior in STOP mode. It indicates what to do when switch RUN|STOP of the PLC is moved into the STOP position. No activity means to stop polling, Normal work means continuing of the operation (cycle of Modbus does not depend on the Run|Stop of the controller), Close Connection means that the connection will be closed and all attempts for a TCP connection will be denied. Selection of Modbus Exception means that for any request to Modbus the reply will contain error code 06 (Device busy).

Note: it is possible to independently activate “behavior in STOP mode” in the program code. To activate the mode, it is required in the program to assign the value TRUE to the ActivateStopBehavior property of the Modbus TCP Slave device instance:

Modbus_TCP_Slave_Device.ActivateStopBehavior:=TRUE;

After that, the slave device will switch to STOP mode.

Figure 22. Configuring parameters for Modbus TCP Slave

The settings of the Modbus Slave data areas and the methods of working with them are completely identical to those described in the section Configuring Modbus Serial Slave.

en | v1.5.5

Table of Contents

Tutorials