Anda di halaman 1dari 22

Transferring data with variable message lengths via the TCP protocol Display part number

QUESTION: How can I receive data with variable message lengths as a TCP data stream? ANSWER: Behavior of the TCP protocol: When data is transferred with the TCP, the transfer is made in the form of a data stream. In this case no information is given about the length nor about the beginning and end and so the recipient cannot tell where one message ends in the data stream and where the next begins. Therefore the sender has to define a message structure that can be interpreted by the recipient. The message structure can comprise of the data plus a terminating control character such as "carriage return" that indicates the end of the message. The following sample program includes a TCP connection via which the data can be sent with variable message lengths with FC5 to a station and received with FC6. Sample program: Transfer of data (with FC5 "AG_SEND" and FC6 "AG_RECV") with variable message lengths via the TCP protocol General description: The STEP 7 project includes two S7-300 stations with CPU 315-2DP and CP 343-1 for communication via Industrial Ethernet. The communication basis is a TCP connection between the two stations. If you open the Properties of the TCP connection in NetPro via "Right- click > Object Properties", you can view the block parameter "ID" of the communication function block. This specification is to be noted when calling FC5 and FC6 so that the data can be transferred via the TCP connection.

Fig. 1:Properties of the TCP connection In order to be able to retrieve the messages from the data stream sent the data to be sent must have a specific structure. The message can comprise of the data plus a terminating control character such as "carriage return" so that the recipient can tell where the message ends.

Fig. 2: Structure of a message Note: The sample program is based on the message structure shown in Fig. 2, but you can also define your own message structure. If you do want to use a different message structure (e.g. change the end character), then simply change the sample program supplied accordingly to meet your particular requirements. The data that you want to send with FC5 (AG_SEND) therefore has to be prepared with this predefined structure in a data block (DB221). The data is then receive byte for byte with FC6 (AG_RECV). Description of the STEP 7 program The STEP 7 program consists of blocks OB100, OB1, FB100, DB100 (instance DB of FB100), FB102 (multiinstance in DB100), DB221, DB222, FC5, FC6. OB100

OB100 is a startup OB and is run when the CPU is restarted (warm restart). The first communication trigger is issued in this OB with M1.0 and M0.1.

Fig. 3: OB100 OB1 OB1 is called cyclically. This OB contains the call of FB100 (instance dB: DB100) with M1.0 and M0.1. Once FB100 has been run M1.0 is reset.

Fig. 4: OB1

FB100: FB100 is called in the OB1 cycle. This FB contains the call of FC5 "AG_SEND" and FB101 "AG_RECV_CR". Send block FC5 "AG_SEND" FC5 is activated by the input parameter "ACT" when clock marker M10.7 has a rising edge and "SND_BUSY" is not set. "SND_BUSY" is set while the job is running and no function can be triggered during that time. This function block is particularly important because the function is asynchronous and can take several cycles. Constant activation of the function without waiting for it to terminate can cause a communication overload. The input parameters "ID" and "LADDR" have to taken from the Properties dialog of the TCP connection in NetPro (Fig. 1). In the "SEND" parameter you have to specify the address of the data to be sent (P#DB221.DBX2.0 BYTE 48). For "LEN" you enter the number of bytes to be sent (26). The output parameters "DONE", "ERROR" and "STATUS" are required for job evaluation and are only valid in one and the same cycle.

Fig. 5: FB100 - call of FC5

If the block is run through without error, "SND_BUSY" is reset and you can then call FC5 again. If the block is terminated with an error, the status word of the block is saved for error analysis and "SND_BUSY" is also reset.

Fig. 6: FB100: evaluation of the FC5 call Receive block FB102 "AG_RECV_CR" When you call FB102 "AG_RECV_CR, you must take the parameters "ID" and "LADDR" from the Properties dialog of the connection in NetPro (Fig. 1). In "RECV_BUF" you have to specify the storage location for the data to be received (P#DB222.DBX0.0 BYTE50). The output parameters "NDR", "ERROR" and "STATUS" are required for job evaluation and are only valid in one and the same cycle.

Fig. 7: Call of FB102 If the block is run through without error, the length of the data received is saved. If the block is terminated with an error, the status word of the block is saved for error analysis.

Fig. 8: Evaluation of the FB102 call FB102 "AG_RECV_CR" Initializing: Initialization steps are executed in Network1 and Network2 of FB102, like resetting the byte counter (#COUNT) to zero and resetting the Error and NDR bit (Out parameter of FB102 "AG_RECV_CR"). Receiving the data: With FC6 "AG_RECV" the data is received byte for byte. If the function terminates with an error,

"#STATUS" is issued as output parameter on FB102 ("AG_RECV_CR").

Fig. 9: Receiving data byte for byte If the data is received without error with FC6, the received byte is saved and the byte offset (#B_OFFSET) is calculated via the "#COUNT" parameter for the ANY Pointer receive area. Then the end character "CR" (carriage return) is checked. The the byte received corresponds to the "carriage return", then the length of the data is forwarded as output parameter "#LENGTH" and the byte counter "#COUNT" is set to zero.

Fig. 10: Determining the byte offset and checking for CR

Creating the receive buffer The receive buffer (ANY Pointer) specified when FB101 is called is verified with the byte offset determined and saved in a temporary ANY Pointer (VAR_BUF). The byte received is written to the receive buffer via the "BLKMOV" function.

Fig. 11: Creating the receive buffer and saving the byte If an error occurs while writing the byte to the receive buffer, this is indicated in FB102 via the "#STATUS" and the "#ERROR" bit.

Fig. 12: Error evaluation

DB221: DB221 contains the data structures that are sent with FC5.

Fig. 13: Data structures

DB222: The data received with FC6 is saved in DB222.

The download contains the sample program described. It has been created with STEP 7 V5.3 and SIMATIC NET V6.2.

T_cp_var_cr.exe Copy the "T_cp_var_cr.exe" file into a separate directory and then start the file with a double-click. The STEP 7 project unpacks automatically with all the subdirectories. You can then use the SIMATIC Manager to open and process the project.

transferring data (with FC5 "AG_SEND" and FC6 "AG_RECV") with variable message lengths via the TCP protocol Display part number

QUESTION: How can I transfer data with variable message lengths via the TCP protocol using the communication functions FC5 "AG_SEND" and FC6 "AG_RECV"? ANSWER: Behavior of the TCP protocol: When data is transferred with the TCP, the transfer is data-stream-based. In this case no information is given about the length nor about the beginning and end of a message. This is not a problem when sending, because the sender knows how many data bytes are to be sent. The recipient however cannot tell where one message ends in the data stream and where the next begins. Therefore you have only two options for linking via pure

TCP: 1. You work with a fixed message length, i.e. both the sender and the recipient work with a predefined data length. This means that the message limit is always clearly defined. 2. You work with a variable message length. This this requires more time on the Receive and on the Send side, because in the first bytes you have to enter a start delimiter as well as the data length of the data sent. In this way the message sent can be exactly interpreted by the recipient. The following sample program includes a TCP connection via which the data can be sent with variable message lengths with FC5 to a station and received with FC6. Sample program: Transfer of data (with FC5 "AG_SEND" and FC6 "AG_RECV") with variable message lengths via the TCP protocol General description: The STEP 7 project includes two S7-300 stations with CPU 315-2DP and CP 343-1 for communication via Industrial Ethernet. The communication basis is a TCP connection between the two stations. If you open the Properties of the TCP connection in NetPro via "Right- click > Object Properties", you can view the block parameter "ID" of the communication function block. This specification is to be noted when calling FC5 and FC6 so that the data can be transferred via the TCP connection.

Fig. 1: Properties of the TCP connection To make data transfer work via the TCP protocol the data to be sent must have a specific data structure. A message must consist of start delimiters (so that the recipient can recognize where the message begins), specification of the length of the data (to define the receive buffer) and the data itself. In this example we have defined start delimiters of 4 bytes (preset with the values: 0x11, 0x12, 0x13, 0x14) and an S7 string for a message. An S7 string consists of two bytes of length information and the actual data string. All ASCII

characters can be transferred with the S7 string.

Fig. 2: Structure of a message Note: The sample program is based on the message structure shown in Fig. 2, but you can also define your own message structure. If you do want to use a different message structure (e.g. start delimiters of only 3 bytes), then simply change the sample program supplied accordingly to meet your particular requirements. The data that you want to send with FC5 (AG_SEND) therefore has to be prepared with this predefined structure in a data block (DB220). The data is then received with FC6 (AG_RECV) in several steps: 1. Receive start delimiters (byte for byte) and identify the message 2. Receive length information of the S7 string 3. Receive data Description of the STEP 7 program The STEP 7 program consists of blocks OB100, OB1, FB100, DB100 (instance DB of FB100), FB101 (multiinstance in DB100), DB220, DB221, FC5, FC6. OB100 OB100 is a startup OB and is run when the CPU is restarted (warm restart). The the release for the first communication trigger is issued in this OB with M1.0 and M0.1.

Fig. 3: OB100 OB1 OB1 is called cyclically. This OB contains the call of FB100 (instance DB: DB100) with M1.0 and M0.1. Once FB100 has been run M1.0 is reset.

Fig. 4: OB1 FB100: FB100 is called in the OB1 cycle. This FB contains the call of FC5 "AG_SEND" and FB101 "AG_RECV_VARIABLE".

Send block FC5 "AG_SEND" FC5 is activated by the input parameter "ACT" when clock marker M10.7 has a rising edge and "SND_BUSY" is not set. "SND_BUSY" is set while the job is running and no function can be triggered during that time. This function block is particularly important because the function is asynchronous and can take several cycles. Constant activation of the function without waiting for it to terminate can cause a communication overload. The input parameters "ID" and "LADDR" have to taken from the Properties dialog of the TCP connection in NetPro (Fig. 1). In the "SEND" parameter you have to specify the address of the data to be sent (P#DB220.DBX0.0 BYTE 48). For "LEN" you enter the number of bytes to be sent (48). The output parameters "DONE", "ERROR" and "STATUS" are required for job evaluation and are only valid in one and the same cycle.

Fig. 5: FB100 - call of FC5 If the block is run through without error, "SND_BUSY" is reset and you can then call FC5 again. If the block is terminated with an error, the status word of the block is saved for error analysis and "SND_BUSY" is also reset.

Fig. 6: FB100 - evaluation of the FC5 call Receive block FB101 "AG_RECV_VARIABLE" When you call FB101 "AG_RECV_VARIABLE", you must take the parameters "ID" and "LADDR" from the Properties dialog of the connection in NetPro (Fig. 1). In "RECV_BUF" you have to specify the storage location for the data to be received (P#DB221.DBX0.0 BYTE50). The output parameters "NDR", "ERROR" and "STATUS" are required for job evaluation and are only valid in one and the same cycle.

Fig. 7: FB100 - call of FB101 If the block is run through without error, the length of the data received is saved. If the block is terminated with an error, the status word of the block is saved for error analysis.

Fig. 8: FB100 - evaluation of the FB101 call FB101 ("AG_RECV_VARIABLE"): State machine: The entry point into the program in FB101 is defined via the variable "STATE_CNT". Depending on the state of the variable "STATE_CNT" a skip is made to the relevant network for further processing of the program. Program processing according to "STATE_CNT": "STATE_CNT" 0 1 2 3 4 5 Description The first byte of the start delimiter has been received (skip from NW9 to NW13) The second byte of the start delimiter has been received (skip from NW10 to NW14) The third byte of the start delimiter has been received (skip from NW11 to NW15) The fourth byte of the start delimiter has been received (skip from NW12 to NW16) Start delimiters have been recognized. Data information is being received (skip from NW3 to NW17) Receive buffer is created and data received (skip from NW4 to NW20) Receiving the start delimiters (byte for byte) and identifying the message (network 5 - 16)

The start delimiters (4 bytes) are received byte for byte with FC6 "AG_RECV". When you call FC6 "AG_RECV", the parameters "ID" and "LADDR" from the input parameters of FB101. In "RECV" you have to specify the storage location for the byte received (SD buffer: byte variable). The output parameters "NDR", "ERROR" and "STATUS" are required for job evaluation and are only valid in one and the same cycle. If the block is run through without error, the byte received is saved for further processing. If the block is terminated with an error, the status word of the block is saved for error analysis. After the byte has been received the defined start information is checked (Byte0 = 11; Byte1 = 12; Byte2 = 13; Byte3 = 14).

Fig. 9: Identifying a message via the start delimiters Receiving the length information of the S7 string (network 3, 17 - 20) If the information received is identified as start delimiters, then the length information of the S7 string (2 bytes) is

received with FC6 "AG_RECV". When you call FC6 "AG_RECV", the parameters "ID" and "LADDR" are taken from the input parameters of FB101. In "RECV" you have to specify the storage location for the two length information bytes (VAR_LEN: structure with 2 bytes). The output parameters "NDR", "ERROR" and "STATUS" are required for job evaluation and are only valid in one and the same cycle. If the block is run through without error, the actual length of the data received is used for the Receive buffer (ANY-Pointer). If the block is terminated with an error, the status word of the block is displayed as output parameter on FB101.

Fig. 10: Receiving the length information The information about the actual length of the string (VAR_LEN.ACT_LEN) is required for creating the Receive buffer (ANY Pointer). Here when FB101 is called, the Receive buffer specified (ANY Pointer) is verified with the actual length of the data and stored in a temporary ANY Pointer (VAR_BUF).

Fig. 11: Creating the Receive buffer Receiving the data (network 4, 21- 23) The data is then received with FC6. When you call FC6 "AG_RECV", the parameters "ID" and "LADDR" are taken from the input parameters of FB101. In "RECV" you have to specify the storage location for the data (VAR_BUF: temporary ANY Pointer). The output parameters "NDR", "ERROR" and "STATUS" are required for job evaluation and are only valid in one and the same cycle. If the block is run through without error, the length of the data received is saved and forwarded to FB101 as output parameter. If the block is terminated with an error, the status word of the block is displayed as output

parameter on FB101.

Fig. 12: Receiving data DB220: The data structures used with FC5 are located in DB220.

Fig. 13: DB220 - Send data structures DB221: The data received with FC6 is stored in DB221. The download contains the sample program described. It has been created with STEP 7 V5.3 and SIMATIC NET V6.2.

Tcp_data_exe Copy the "T_cp_data.exe" file into a separate directory and then start the file with a double-click. The STEP 7

project unpacks automatically with all the subdirectories. You can then use the SIMATIC Manager to open and process the project. Filter criteria: Hardware platform: Software: Entry contents: Industrial Ethernet, SIMATIC S7-300/S7-400 STEP 7 up to V5.x Function blocks/tools

Entry ID:8707570 Date:2004-06-03 PROFIBUS DP diagnostics block for CP342-5 (Master) Display part number

QUESTION:. How can I do a PROFIBUS DP station diagnosis for the CP342-5? ANSWER: Using the FC block "DP_DIAG" (FC 3) contained in the "SIMATIC_NET_CP" library you can query and evaluate diagnostics information from the CP342-5 in your user program. In addition to job types like Request DP diagnostics lists, Request DP single status, Acyclic reading of input/output data of a DP slave, Read older DP single diagnostics, Read DP operating mode for PLC/CP STOP, Read current status of the DP slave, you can also request a DP station list from the CP. For this you must transfer the following input parameters to the diagnostics block. FC parameters for station diagnostics: Parameter Declaration CPLADDR DTYPE STATION DIAG INPUT INPUT INPUT INPUT Data type WORD BYTE BYTE ANY W#16#..... B#16#0 B#16#0 ANY length 16 bytes Block start address of the CP Code for station diagnostics Not relevant for station diagnostics Storage location of the diagnostics data, 16 bytes in DB, marker or PA area NDR ERROR STATUS DIAGLNG OUTPUT OUTPUT OUTPUT OUTPUT BOOL BOOL WORD BYTE 1: new data Error display Status display Length of the data record available, here always 16 bytes If you call this function with the parameters described, you obtain a diagnostics data record 16 bytes long. This data record displays whether a station you have configured has failed. If a station has failed, you can acquire Value Description

the associated PROFIBUS address. Structure of the data record: The individual bits of the 16-byte long data record reflect the possible 127 slave addresses. If the configured station is not available, then in the data record a 1 is set on bit belonging to the slave address. Bit 7 Byte 1.: Byte 2 Byte 3. .. .. Byte 16 0 8 16 .. .. 120 Bit 6 1 9 17 .. .. 121 Bit 5 2 10 18 .. .. 122 Bit 4 3 11 19 .. .. 123 Bit 3 4 12 20 .. .. 124 Bit 2 5 13 21 .. .. 125 Bit 1 6 14 22 .. .. 126 Bit 0 7 15 23 .. .. 127 Slave address Slave address Slave address .. .. Slave address

By monitoring the data record provided by the CP you can detect station failures and determine the slave address of the failed station. Note: The FC block "DP_DIAG" is only useful in "DP Master" mode. More information on this block is available in the online Help. Sample function: The following STEP 7 project contains the function block FB13 that implements a PROFIBUS DP slave diagnosis. By continuously calling the function "DP_DIAG" in the function block FB13 each DP slave recognizes failure and enters the information given below into a ring buffer in the instance data block DB13. The following entries are stored in the ring buffer: Serial error number Slave failure incoming/outgoing Number of failed slaves PROFIBUS address of the failed station; if there are several, the highest address is entered Time stamp of the failure or recovery

Also stored in the instance data block are the current station status (station list) of the FC3 and the data record of the last station failure. Errors are specified via the output parameter "STATUS". The function block FB13 has the following call parameters: Parameter Declaration activ addr INPUT INPUT BOOL WORD Activate block Address of the CP342-5 Data type Description

check STATUS

INPUT OUTPUT

BOOL WORD

Not needed STATUS BYTE of FC3

The download contains a STEP 7 V5 project with the function block FB13, the associated instance data block and an organization block OB1. You can adapt the blocks as you wish to suit your requirements.

diag_fb.exe Copy the "diag_fb.exe" file into a separate directory and then start it with a double-click. The S7 V5 project is now unpacked with all the associated subdirectories. You can then open and process the unpacked project with the SIMATIC Manager.

Anda mungkin juga menyukai