Entry type: FAQ, Entry ID: 29738463, Entry date: 05/04/2015
(2)
Rate

How do you program the communication blocks FB67 "TUSEND", FB68 "TURCV", FB65 "TCON" and FB66 "TDISCON" in order to use the UDP protocol for data exchange via the integrated PROFINET interface of an S7-300/S7-400 CPU?

  • Entry
  • Associated product(s)
You can use the open communication of the PROFINET for data exchange by means of the integrated Industrial Ethernet interface of a CPU, for example.

The following protocols are supported for this:

  • TCP
  • ISO-on-TCP
  • UDP

The following communication blocks are available for "Open User Communication" via Industrial Ethernet using UDP protocol:

  • FB65 "TCON" for connecting the UDP end point
  • FB66 "TDISCON" for disconnecting the UDP end point
  • FB67 "TUSEND" for sending data
  • FB68 "TURCV" for receiving data

These communication blocks are available in the Standard Library -> Communication Blocks.
Copy the latest versions of the above-mentioned communication blocks from the standard library into your user program and then call them in your user program.
The parameters for connecting the UDP end point are saved in a data structure. The data structure "OUCW_2" will be used in this example. This is stored in the DB1 data block and is parameterized by the user.

Note

  • Use the "Open Communication Wizard" for easy establishment of the UDP end point. This is available for downloading in Entry ID !98957840!.
  • The UDP connection is not configured in NetPro.

Table 01 shows the "OUCW_2" data structure in which the parameters for establishing the UDP end point are stored.
 

Byte Parameter Data type Description
0 to 1 block_length WORD Length of the "OUCW_2" data structure
2 to 3 id BYTE Reference to this connection between user program and communication layer of the operating system
Value range: w#16#0001 bis w#16#0FFF
You must specify the value of this parameter for the ID in the block concerned.
4 connection_type BYTE UDP protocol version: B#16#13
5 active_est BOOL ID for how the connection is established
You must give the value FALSE to this parameter.
6 local_device_id BYTE Communication through the integrated PN interface of the CPU 315-2 PN/DP: B#16#02
7 local_tsap_id_len BYTE Length used for the local_tsap_id parameter: 2 Byte
8 rem_subnet_id_len BYTE This parameter is not used currently. It has the value B#16#00.
9 rem_staddr_len BYTE This parameter is not used currently. It has the value B#16#00.
10 rem_tsap_id_len BYTE This parameter is not used currently. It has the value B#16#00.
11 next_staddr_len BYTE This parameter is not used currently. It has the value B#16#00.
12 to 27 local_tsap_id ARRAY
[1 ..16] of BYTE
Local port number: 2000, for example
local_tsap_id[1] = B#16#7 (high byte of the port number in hexadecimal format)
local_tsap_id[2] = B#16#D0 (low byte of the port number in hexadecimal format)
local_tsap_id[3-16] = B#16#00
28 to 33 rem_subnet_id ARRAY
[1 ..6] of BYTE
This parameter is not used currently.
rem_subnet_id[1-6] = B#16#00
34 to 39 rem_staddr ARRAY
[1 ..6] of BYTE
This parameter is not used currently.
rem_staddr[1-6] = B#16#00
40 to 55 rem_tsap_id ARRAY
[1 ..16] of BYTE
This parameter is not used currently.
rem_tsap_id[1-16] = B#16#00
56 to 61 next_staddr ARRAY
[1 ..6] of BYTE
This parameter is not used currently.
next_staddr[1-6] = B#16#0
62 to 63 spare WORD Reserve, the parameter has the value W#16#0000.
Table 01

Description of the sample program
The S7 program contains the call of the FB65 "TCON" and the data structure "OUCW_2", in which the parameters for establishing the UDP end point are stored. The S7 program also includes the call of the function blocks FB67 "TUSEND" and FB68 "TURCV" from the Standard Library -> Communication Blocks. The FB67 "TUSEND" is for sending data to an S7 station, to a PC station, or to a third-party system. The FB68 "TURCV" is for receiving data from an S7 station, from a PC station or from a third-party system.

First create the hardware configuration for your S7-300 station. Configure the marker byte 100 as clock marker. The send job is triggered by this clock marker. Save and compile the hardware configuration of your S7-300 station and load it into the CPU.

The STEP 7 program consists of the blocks OB100, OB1, FB1, DB11, DB1, DB100, DB200 and FB63, FB64, FB67 and FB68.

OB100
The OB100 is a restart OB and is run when the CPU is restarted (warm start). In this OB, the first communication trigger is enabled with marker M0.3.

OB1
OB1 is called cyclically. The FB1 is called in OB1 with the instance data block DB11, with the marker M0.3 as INIT_COM parameter and with the marker M10.0 as ABORT parameter. The marker M0.3 is reset in OB1 after the FB1 has been called.


Fig. 01

FB1
The FB1 is called cyclically in OB1. The following function blocks are called in FB1.

  • FB65 "TCON"
  • FB67 "TUSEND"
  • FB68 "TURCV"
  • FB66 "TDISCON"

Define connection number
You can change the connection number separately. Change the connection number in network 2 of FB1 in accordance with your configuration.
The connection number is stored in a static tag and so in the instance data block DB11.
The connection number "3" is defined in this example.


Fig. 02

Establishing the connection
Connection of the UDP endpoint is started by a positive edge at the input parameter "REQ" of FB65 "TCON". The "OUCW_2" data structure with the parameters of the local UDP end point is incorporated in the data block DB1.
On the input parameter "CONNECT" of the FB65 "TCON", the memory area is specified that contains the parameterization of the local UDP endpoint.
The connection of the UDP endpoint is set up at system start and remains until it is disconnected with FB66 "TDISCON", the CPU goes into STOP mode, or the power supply is switched off.


Fig. 03

The send job is triggered by a positive edge on the input parameter "REQ" of the FB67 "TUSEND". The send request trigger is controlled by clock marker M100.6 and the variable "SEND_BUSY". If the send request is running, "SEND_BUSY" is set. It is then not possible to trigger a new send request.
You specify the memory area that contains the data to be sent at the input parameter "DATA".
You enter the number of bytes to be sent at the input parameter "LEN".
In this example the Send data is stored in DB100. 100 bytes of data is sent to the communication partner.
At the input parameter "ADDR" you specify the address of the data area where the recipient's IP address is stored. In this example, the address parameters of the communication partner are stored in the data structure "OUCW_3". This is incorporated in the data block DB1.
The output parameters "DONE", "ERROR" and "STATUS" are required for job evaluation.


Fig. 04

Table 02 shows the "OUCW_3" data structure in which the the address parameters of the communication partner are stored.
 

Byte Parameter Data type Description
0 to 3 rem_ip_addr ARRAY
[1 .. 4] of BYTE
IP address of the remote partner, 192.168.0.30, for example
rem_ip_addr[1] = B#16#C0
rem_ip_addr[2] = B#16#A8
rem_ip_addr[3] = B#16#00
rem_ip_addr[4] = B#16#1E
4 to 5 rem_port_nr ARRAY
[1 .. 2] of BYTE
Remote port number, 2000, for example
local_tsap_id[1] = B#16#7 (high byte of the port number in hexadecimal format)
local_tsap_id[2] = B#16#D0 (low byte of the port number in hexadecimal format)
6 bis 7 spare ARRAY
[1 .. 2] of BYTE
Reserve, the parameter has the value W#16#0000.
Table 02

If the send request is successfully completed, "SEND_BUSY" is reset. A new send job can now be triggered.
If the send request is completed with an error, then "SEND_BUSY" is likewise reset and the value of the output parameter "STATUS" of the FB67 is saved for an error analysis.


Fig. 05


Fig. 06

The data can be received as soon as the UDP end point is connected.
With the input parameter "DATA", you specify the address and length of the data area where the received data is saved.
In this example 100 bytes of data are received and stored in DB200.
At the input parameter "ADDR" you specify the address of the data area where the sender's IP address is stored. In this example, the address parameters of the communication partner are stored in the data structure "OUCW_3". This is incorporated in the data block DB1. Table 02 shows the "OUCW_3" data structure.


Fig. 07

The output parameter "NDR" is for showing that new data has been received. The "RCVD_LEN" output parameter indicates the length of the data received.
If the data is received successfully, then the value of the "RCVD_LEN" output parameter is saved.


Fig. 08

If the data is not received successfully, then the value of the "STATUS" output parameter is saved and evaluated.


Fig. 09

You can disconnect the UDP end point specifically with the FB66 "TDISCON". You start the job to disconnect the UDP endpoint with a positive edge at the input parameter "REQ" of FB66 "TDISCON".


Fig. 10

The STEP 7 project as a download
The STEP 7 project contains a sample program for calling FB1 and the function blocks FB65 "TCON", FB66 "TDISCON", FB67 "TUSEND" and FB68 "TURCV" with status evaluation. It was created with STEP 7 V5.5 SP3.

Registration required sample_open_udp.zip (294.6 KB)

Configuration of additional UDP connections
To configure additional UDP endpoints, you copy the FB1 so that you receive another function block (such as FB101). Change the parameters and generate a new instance data block. Using the Open Communication Wizard you create a new data structure in which parameters of the new UDP end point are stored.

Further Information

  • Detailed information about open communication over Industrial Ethernet is available in the manual "System and Standard Functions for S7-300/400 Volume 1 and Volume 2" in Entry ID: 44240604.
  • Instructions for configuring a UDP connection for communication by way of S7-300 and S7-400 Industrial Ethernet CPs are available in Entry ID: 47885893.