Length measurement, or distance measurement, is ubiquitous in industrial production. This article presents a programming method for continuous length measurement using the SIEMENS SIMATIC S7-1200 connected to the OMRON E6B2-CWZ5B incremental encoder, based on a practical case. The goal is to achieve cyclic measurement, length counting, and subsequent speed measurement with minimal and straightforward programming.
According to Siemens’ official guidance documents, the high-speed counting channels for the S7-1200 series are as follows:

In this example, a CPU1215C is used to connect two encoders, as shown in the schematic below:

Since both encoders are used in the same way but installed in different positions, we will take one encoder as an example. First, create a project in TIA Portal and insert the CPU. Next, basic configuration of the CPU’s input ports is required to achieve high-speed counting. Double-click on the CPU in the device view, and in the properties page at the bottom of the window, find “High-Speed Counter (HSC)”. Click to open the dropdown menu, select “HSC1”, and configure it according to the encoder type as follows:
Select the hardware I/O channel address for High-Speed Counter 1 according to the schematic:
At this point, the basic configuration is complete, and we can start programming. First, design the calculation process. Since I am using the simplest method of directly reading effective symbol double integer variables based on the starting address, the following formula is used:
The letters ABCDS represent different processing values, making it easier to distinguish in the program. I have written a function block (FB) for easy portability to other programs, so I will present the program code in the form of an FB. First, declare the input variables:
Then declare the process data variables:
Due to space limitations, the actual output variables are processed other data, not #EN_S1. We will only discuss the calculation process, so we will skip this part. Then, complete the program according to the previous calculation process:
Thus, we can obtain a continuously updated measurement value #EN_S1 based on the actual counting of the encoder according to the PLC’s natural cycle. This measurement value can be accumulated in subsequent programs and can also be used for periodic sampling to calculate linear speed. Our application is for a steel strip storage device, where one encoder measures input and the other measures output, yielding the final real-time storage amount.
There is already a complete program print document for this FB function block, and we have successfully replicated it on the S7-200smart and S7-300 high-speed counting modules. If you are interested, feel free to leave a message, and I will share it with you. Follow me to step into a world of pure practical knowledge for industrial control professionals! Siemens PLC byte order issues.