Optimization options

Sometimes the obvious way is not always the most efficient way. The following article shows some possibilities how you can optimize the S7 data communication.

Sample 1: reading many BOOL/BIT variables

The configuration in the TIA project contains several (in this sample 10) BOOL/BIT variables, which are located in the memory directly "one after the other" and are to be read out:

Optimization options 1:

The most obvious implementation would be to attach the 10 variables of type BOOL to a request.

Optimization options 2:

This implementation would then look like this:

Optimization options 3:

The various variables are queried individually here, with each "item" entry being 12 bytes long. The entire payload of the S7 request is thus 132 bytes long. A corresponding response would then look like the following:

Optimization options 4:

Each "Item" is 5 bytes long and the whole S7 Response 73 bytes.

A more efficient implementation would be to read a byte array, in this case 2 bytes.

Optimization options 5:

The byte array would then be "mapped" to the BOOL variables.

Optimization options 6:

The request in this case would look like this:

Optimization options 7:

In this case, the payload is only 24 bytes long (i.e. 108 bytes less). The corresponding response would then be:

Optimization options 8:

In this case, the payload of the response is only 20 bytes long (previously 73 bytes).

Request:

Response:

Optimization options 9:

Further Information

If a request frame is longer than the S7 controller can process, the TwinCAT3 S7 communication driver must split the request into several requests. This can extremely increase the response time.

Sample 2: reading out many WORD variables

The configuration in the TIA project contains several (in this sample 5) WORD variables, which are located in the memory directly "one after the other" and are to be read out:

Optimization options 10:

The same principle applies as in sample 1: each entry increases the payload size of the request and the associated response. The same approach can be used for optimization (please note the "endianess"). An optimized PLC program could then look as follows:

Optimization options 11:

Optimization options 12:

The entries must then be "mapped" accordingly and "rotated" correctly.

Optimization options 13:

The savings compared to direct (obvious) implementation would then be as follows:

Request:

Response: