7.15.3 MB_TCP_SERVER
Function
Process ModbusTCP client connection requests, receive and Modbus requests and send responses
| Function code (16#byte) | Operation data width | Description |
|---|---|---|
| 0x01 | Bit | Read coils |
| 0x02 | Bit | Read discrete inputs |
| 0x03 | Word | Read holding Registers |
| 0x04 | Word | Read input registes |
| 0x05 | Bit | Write single coil |
| 0x06 | Word | Write a single holding register |
| 0x0F | Bit | Write multiple coils |
| 0x10 | Word | Write multiple holding registers |
LD graphics

Parameter description
| Interface description | Declaration | Data type | Description |
|---|---|---|---|
| DISCONNECT | INPUT | BOOL | MB_TCP_SERVER instruction disconnects from the partner. The server responds to the connection request with the IP address entered in the CONNECT parameter. After accepting a connection request, you can use this parameter to control the disconnection: FLASE/0: Establish a passive connection when there is no communication to connect TURE/1: Terminate the connection. If this input is set, operations such as connecting, sending and receiving data, etc. will no longer be performed |
| CONNECT | IN_OUT | TCP_CONNECTCONFIG | Connection description structure object, using the TCP_CONNECTCONFIG structure, describes all address parameters required to establish a specified condensation, including local IP, local port, peer IP, peer port, and whether to actively establish a connection five parameters. The default address is 0.0.0.0 (any IP address), you can also enter a specific IP address. The actively established connection item (ActiveEstablished) in the connection structure used for MB_TCP_SERVER should be FALSE, otherwise the error code status code 0x8BB will be output For details, refer to TCP_Connectconfig |
| COIL_DATAAREA | IN_OUT | ANY | points to the Modbus coil data area in the "MB_TCP_SERVER" command The coil data area contains the Modbus client's Modbus function: 0x01: Read coils; 0x05: Write Single coil; 0x0F: Write values accessible to multiple coils |
| DISCRETE_DATAAREA | IN_OUT | ANY | points to the Modbus discrete input data area in the "MB_TCP_SERVER" instruction. The discrete input data area contains the Modbus client through the Modbus function: 0x02: Read the value accessible by the discrete input |
| HOLDING_REG_DATAAREA | IN_OUT | ANY | Points to the Modbus holding register data area in the "MB_TCP_SERVER" instruction The holding register data area contains the Modbus client through the Modbus function: 0x03: Read the holding register; 0x06: Write a single holding register; 0x10: Write values accessible to multiple holding registers |
| INPUT_REG_DATAAREA | IN_OUT | ANY | Points to the input register data area in the "MB_TCP_SERVER" instruction The coil data area contains the Modbus client through the Modbus function: 0x04: Read the value accessible from the input register |
| NDR | OUTPUT | BOOL | "New Data Ready": FALSE/0: Unintentional data TRUE/1: Modbus client writes new data Modbus function writes coil (0x05, 0x0F) and writing to the holding register (0x06, 0x10) will set NDR |
| DR | OUTPUT | BOOL | “Data Read”: FALSE/0: Data is not read; TRUE/1: Modbus client section reads data; Read coil in Modbus function ( 0x01); Reading the holding register (0x03); reading the input register (0x04) will set DR. |
| ERROR | OUTPUT | BOOL | If an error occurs during calling the "MB_TCP_SERVER" command, the ERROR parameter will output TRUE/1 The detailed information of the error will be specified by the STATUS parameter |
| STATUS | OUTPUT | WORD | Detailed status information of the command |
info
- Modbus protocol usually uses 502 as the service segment port
- ANY belongs to the generic data type, refer to Generic Data Type
- The set value is maintained for 1 cycle
- Little-endian storage is used in Baosky PLC
Description of connection and data sending and receiving status
| STATUS(hexadecimal) | Description |
|---|---|
| 0000 | Instruction not executed, initial state |
| 7002 | Waiting for communication connection to be established |
| 7007 | Communication connection closed |
| 7004 | Communication connection established |
| 7005 | Sending data |
| 7006 | Receiving data |
Parameter error status description
| STATUS(hexadecimal) | Description |
|---|---|
| 80BB | The ActiveEstablished parameter value is invalid. Allow only passive connections to service segments |
| 8389 | Data areas overlap |
Protocol error:
| Corresponding to Modbus protocol response abnormal message error code | STATUS(16#WORD) | Description |
|---|---|---|
| / | 8380 | The received Modbus frame format is incorrect or the number of bytes is too small |
| 01 | 8381 | Unsupported function code |
| 03 | 8382 | Data length error, received Modbus frame length is invalid or inconsistent with the actual number of bytes received |
| 02 | 8383 | The access is out of bounds. The Modbus frame data address is incorrect or an area other than the bound data area is accessed |
| 03 | 8384 | The data value is wrong. Corresponding function code 0x05, when writing a single coil, the data value 0XFF00 means setting it to 1, 0x0000 means setting it to 0, and the rest are illegal data |
Example
- LD sample pictures are shown below:

- ST sample code is as follows:
MB_TCP_SERVER_2(
disconnect:=aa,
CONNECT:=config,
coil_dataarea:=bb
discrete_dataarea:=dataarea,
holding_reg_dataarea:=dataarea _holding,
reg_dataarea:=reg_dataarea,
NDR=>n,
DR=>dr,
ERROR=>error,
STATUS=>xx
);