The FB_ModbusTcpServer function block is used to implement a Modbus TCP server in your application.
The Modbus TCP server provides access to the four different memory blocks specified by the Modbus data model:
Discrete inputs (single bits) – read only
Coils (single bits) – read and write
Input registers – read only
Holding registers – read and write
For each of the memory block types, a separate application memory must be provided to the function block via the corresponding inputs. The inputs must be assigned at each function block call.
The following Modbus function codes (FC) are supported by the FB_ModbusTcpServer function block:
FC01 (0x01
): Read Coils
FC02 (0x02
): Read Discrete Inputs
FC03 (0x03
): Read Holding Registers
FC04 (0x04
): Read Input Registers
FC05 (0x05
): Write Single Coil
FC06 (0x06
): Write Single Register
FC15 (0x0F
): Write Multiple Coils
FC16 (0x10
): Write Multiple Registers
FC23 (0x17
): Read/Write Multiple Registers
The Modbus TCP server accepts requests addressed to the unit IDs 0...255.
The Modbus TCP server is capable of managing up to 30 TCP connections at the same time. During one function block call, up to three Modbus requests per connection can be processed.
If the maximum number of client connections (the value of i_uiMaxNumOfClientConnections) are held, new incoming connections are not processed. Connections that are inactive for 60 seconds are closed by the server.
Some controllers, such as the Modicon M241 Logic Controller, Modicon M251 Logic Controller or the Modicon M262 Logic/Motion Controller provide an embedded Modbus TCP server which uses the default port 502. On these controllers, the function block FB_ModbusTcpServer cannot be used in conjunction with port 502 (i_uiServerPort), and therefore you must explicitly assign a different port using the input i_uiServerPort. If the port specified at i_uiServerPort is used by another application, or no port is specified, the function block indicates the corresponding error message.
Ensure that the port specified at i_uiServerPort is enabled in the firewall configuration of your controller. The function block is not able to detect a blocked port.
The following processes are executed synchronously with the task in which the function block is called:
Processing Modbus requests
Data exchange with the TCP stack
Connection management
Therefore, the execution time of the function block varies, depending on the number of connected clients and the number of Modbus requests to be processed.
To help prevent other processes from being blocked by processing of the Modbus TCP server, create a separate task with low priority (>24) for the Modbus TCP server functionality. In addition, consider whether the watchdog for this task may be disabled to avoid watchdog exceptions during processing. For further information, refer to the chapter System and Task Watchdogs in the Programming Guide of your controller.
The CPU load of the controller is affected by the number of managed client connections. When commissioning, verify CPU utilization with the maximum number of expected client connections. A value of 85 % must not be exceeded. Consult the Programming Guide specific to your controller for further information on how to monitor the CPU load.
Do not process the data from the memory blocks accessed by the Modbus TCP server within other tasks while the function block is executed. Implement suitable measures, for example semaphores, for access control to the corresponding memory blocks.
For the implementation of a semaphore, you can use the function SysCpuTestAndSet() from the SysCpuHandling library.