Skip to main content

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 widthDescription
0x01BitRead coils
0x02BitRead discrete inputs
0x03WordRead holding Registers
0x04WordRead input registes
0x05BitWrite single coil
0x06WordWrite a single holding register
0x0FBitWrite multiple coils
0x10WordWrite multiple holding registers

LD graphics

alt text

Parameter description

Interface descriptionDeclarationData typeDescription
DISCONNECTINPUTBOOLMB_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
CONNECTIN_OUTTCP_CONNECTCONFIGConnection 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_DATAAREAIN_OUTANYpoints 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_DATAAREAIN_OUTANYpoints 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_DATAAREAIN_OUTANYPoints 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_DATAAREAIN_OUTANYPoints 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
NDROUTPUTBOOL"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
DROUTPUTBOOL“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.
ERROROUTPUTBOOLIf 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
STATUSOUTPUTWORDDetailed 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
0000Instruction not executed, initial state
7002Waiting for communication connection to be established
7007Communication connection closed
7004Communication connection established
7005Sending data
7006Receiving data

Parameter error status description

STATUS(hexadecimal)Description
80BBThe ActiveEstablished parameter value is invalid. Allow only passive connections to service segments
8389Data areas overlap

Protocol error:

Corresponding to Modbus protocol response abnormal message error codeSTATUS(16#WORD)Description
/8380The received Modbus frame format is incorrect or the number of bytes is too small
018381Unsupported function code
038382Data length error, received Modbus frame length is invalid or inconsistent with the actual number of bytes received
028383The access is out of bounds. The Modbus frame data address is incorrect or an area other than the bound data area is accessed
038384The 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:

alt text

  • 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
);