Skip to main content

7.15.1 TRCV_C

TRCV_C (Establish TCP connection and receive data)

Functionality

  • Supports Duplex Mode: The PLC can function as both the active connecting party (client) and the passive connecting party (server) for data reception.

TRCV_C System Library: Used for asynchronously establishing a TCP connection and receiving data. This instruction operates asynchronously and includes the following features:

  • Establish communication connection

    • Configuration and Connection Setup: Users configure the correct IP address and port number in the connect block. By setting the CONT parameter to true, the connection function is enabled, and a communication connection is established.
  • Receive data via established connection

    • Data Reception: Once the connection is successfully established, if the ENR parameter is set to true, the data reception function is activated. Users need to bind the data variable for receiving data in the DATA block and specify the length and mode of reception. If the reception length is not specified (LEN = 0), the system defaults to receiving data based on the size of DATA.
  • Terminate and reset connection

    • Setting the CONT parameter to 0 will terminate the communication connection.After a connection is successfully established, setting the COM_RST parameter to true will reset the current connection. If the connection has not been established or if CONT is set to 0, setting COM_RST to true will not reset the connection.
info

Instruction TRCV_C internally uses communication instructions "TCON", "TRCV", "T_RESET" and "TDISCON"

ADHOC mode description

  • ADHOC Mode, also known as "Dynamic Reception Mode"

    • Using ADHOC mode allows for the reception of dynamically sized data. Assigning the value 1 to the ADHOC parameter sets the current reception function to ADHOC mode. Once ADHOC mode is enabled, the TRCV_C function block will immediately transfer any received data block to the reception area, even if the block is as small as one byte, and set done = true. RCVD_LEN indicates the actual size of the data currently received, with a minimum value of 1.
  • Static Receiving Mode, receiving data of specified length

    • Assigning the value 0 to the ADHOC parameter sets the current reception function to static reception mode, which receives data of a specified length. If ADHOC mode is disabled, data reception is only completed after receiving the data length specified by the LEN parameter. After reception, the RCVD_LEN parameter reflects the actual received data length (in bytes), which should match the data length specified in the LEN parameter. In static reception mode, if LEN = 0, the required data size for reception is determined by the size of the DATA block, and Done is set for one cycle only after receiving data of the size of DATA.

LD graphics

alt text

Parameter description

Interface VariablesDeclarationData TypeDescription
EN_RInputBOOLWhen EN_R is True, the receive function is enabled. If EN_R remains True, the receiving function will continue to be enabled
CONTInputBOOLControl communication connection:
0: Disconnect communication connection
1: Establish and maintain communication connection
LENInputUDINTThe number of bytes of data to be received
If =0, data of Data size will be received by default
If not 0, data will be received according to the size of LEN
If the length of LEN is > 65535 or > the size of DATA, an error will be reported
The actual received size also depends on the value of ADHOC
ADHOCInputBOOLReceive mode:
0: Indicates that the Static Receive Mode is enabled. Only when data of the specified size is received, done will be set to true and maintained for a period
1: Indicates that the dynamic receiving mode is enabled. As long as data is received, even 1 byte, done will be set to true and maintained for a period
CONNECTInOutTCP_ConnectConfigExample of a function block to configure connection parameters, refer to TCP_Connectconfig
DATAInOutANY_TYPEThe data to be sent, ANY _TYP type, supports basic types, String and UDT
COM_RSTInOutBOOLReset connection parameters. If true, the existing connection will be reset. It will take effect if and only if the connection is successfully established
DONEOutputBOOLState parameter with the following possible values:
0: The receiving job has not been started or is still in progress
1: The receiving job has been successfully executed
This status will only display one Cycle
BUSYOutputBOOLState parameter with the following possible values:
0: The receiving job has not yet started or has been completed
1: The receiving job has not yet been completed
ERROROutputBOOLState parameter with the following possible values:
0: No error
1: An error occurred while establishing the connection, transmitting data, or terminating the connection
STATUSOutputWORDThe status of the current function execution process
RCVD_LENOutputUDINTThe actual received data length, only when done = true, it will be updated once and maintained for one cycle
Supplementary instructions for BUSY, DONE and ERROR parameters
  • Use the BUSY, DONE, ERROR and STATUS parameters to check the status of the job. Parameter BUSY indicates that the receiving function is executing

  • Use the DONE parameter to check whether the receiving task has been executed successfully

    • If "ERROR" is 1, it means an error occurred during connection/disconnection/reset/receiving process
    • Error information is output through parameter STATUS
  • Among them, Done and Busy will not be set during the connection establishment process.

  • The following table lists the relationship between parameters BUSY, DONE and ERROR
BUSYDONEERRORDescription
100Sending job is being processed
010Sending job completed successfully
001The job ended due to an error
The cause of the error is output through the parameter STATUS
000No new jobs assigned

Status code description

STATUS(WORD 16#)Description
0000Uncalled
0001Currently creating socket
7000No task is activated, REQ detects rising edge, but CONT is false
7001Socket created successfully
7002Establishing communication connection
7003Communication establishment is closing
7004Connection successfully established
7006Reciving data
7007Active disconnection
7008Data received successfully
8000Resetting connection
The following are exception status codesDescription
8080Failed to create socket, insufficient system socket resources
8081The IP address is incorrect for the following reasons:
The IP address corresponding to LocalAddress does not exist, i.e., LocalAddress does not match the IP of all the PLC network ports;
The PLC network corresponding to the currently used LocalAddress The port protocol is not Ethernet protocol;
The current LocalAddress value is 0.0.0.0;
8082Setting IP address and port number reusability errors
8083Bind address and port number with the socket fails, mainly because the current port is already occupied
8084Failed to add server socket to EPOLL listening queue
8086Failed to receive new connection
8087Failed to remove the Epoll event corresponding to the client
80A0Connection failed
80A1The specified connection has not been established yet, the current receiving function has been activated, and CONT is set to TRUE, but the connection has not yet been established
80A2Active disconnection failed
80A3Reset connection failed
80A4Data block is empty
80A580A5 appears for the following three reasons: parameter LEN is greater than 65536
The value of LEN is greater than the size of the data block
LEN is 0 and the size of the data block is greater than 65536
80A6The received length is smaller than expected, for Static Receive Mode only
80A7Error getting send/receive buffer BUF
80AAFailed to receive data
80ABThe KeepAlive mechanism detects a physical layer network disconnection, holds it for one cycle, and subsequently stays in the 7002 state
80ACAsynchronous function enqueue failed
80C3The connection corresponding to the current TConnectConfig is already used by other TRCV_C blocks
80C5During the process of receiving data, the peer actively disconnected

Notes

  • When EN_R is True, the receive function is turned on; if ADHOC = false, the receive data function will synchronize the received data to DATA only when it receives data of the specified size; if EN_R is set to false to turn off the receive function before it receives data of the specified size, the part of the data that has been received previously will be cleared. At this time, if the sender has been in the middle of sending, re-opening the receive function will cause the received data to be inconsistent with the sender, in which case it is necessary to disconnect or reset the connection in order to recover.

  • During the sending/receiving process, modifying the size of the DATA block may cause inconsistency in the sent and received data. Ensure that the current sending/receiving action is stopped before modifying the size of the DATA block;Or after modifying the DATA block size, it is found that The sending and receiving data is inconsistent. At this time, you can reset the connection to solve the problem

  • When the current TRCV_C uses the TCP_Connectconfig_1 connection block for receiving action, if the specified length of data has not been received during the current receiving process, delete the TRCV_C function block, and then create a new TRCV_C_2 that also uses the TCP_ConnectConfig_1 connection. Connect the block to receive the action. At this time, an error 0x80C3 will be reported. This is because the current connection block TCP_ConnectConfig_1 is still occupied by the previous TRCV_C function block and cannot be released. At this time, it is recommended to disconnect or reset the connection to return to normal; or avoid During the reception process, delete the TRCV_C instance.

  • When the current TRCV_C uses the TCP_Connectconfig_1 connection block for receiving action, if the specified length of data has not been received during the current receiving process, modify the initial value of TRCV_C in the GVS table and perform incremental download, and the connection block TRC will still be used in the future. V_C performs the receiving action. At this time, an error 0x80C3 will be reported. This is because modifying the initial value incremental download is equivalent to creating a new TRCV_C receiving block. At this time, it is necessary to disconnect or reset the connection to return to normal; or avoid the receiving process. Modify the initial value of the TRCV_C instance.

  • When using UDT as a data block to receive, please note that UDT is 4-byte aligned by default. Part of the data in UDT that is less than four bytes may be padded with 0s during the compilation stage until it meets the 4-byte aligned arrangement; If you use TCP communication block to communicate within Baosky PLC, you don’t need to worry about UDT4 byte alignment. If you communicate with L2 or third-party PLC manufacturers, it is recommended to pay attention to this issue.

  • Baosky PLC data storage is based on little-endian storage. When using TCP function blocks to send data, it is also sent in little-endian byte order. If communicating with third-party PLC manufacturers and retrograde networks, It is necessary to perform big-endian conversion according to the actual scenario.

  • When multiple TRCV_C function blocks use the same TCP_ConnectConfig connection block, only one TRCV_C function block is receiving data at the same time, so only one receiving function block can work normally at this time, and the others will report error 80C3; In this In this scenario, avoid using TASKs with different priorities to bind different TRCV_C function blocks (these TRCV_C function blocks use the same TCP_ConnectConfig connection block), otherwise the received data will be disordered. If the above TRCV_C function block does not use the same TCP_ConnectConfig connection block, binding it to TASKs of different priorities will not be affected.

Example

  • LD example is shown in the following animation:

alt text

  • ST example is shown in the following code:
TRCV_C_1(
EN_R:=bVar,
CONT:=Switch,
LEN:=0,
ADHOC:=true,
CONNECT:=TCP_CONNECTCONFIG_1,
DATA:=iVar,
COM_RST:=RESET,
DONE=>OUT,
BUSY=>OUT2,
ERROR=>OUT3,
STATUS=>OUT4,
RCVD_LEN=>OUT5
);