What if I can't concatenate IEC operators in the TC2 ladder diagram

 Executive Summary

In the TwinCAT 2 ladder diagram editor, IEC operators - comparison, shift, arithmetic operations, etc. - must be on separate lines.When they need to be connected in series, they can only be divided into multiple lines, and intermediate variables are created as the output result of the previous line and the execution condition of the following line.The Lib introduced in this article encapsulates IEC operators into function blocks with enable input EN and execution output ENO, allowing these FB instances to be connected in series in the ladder diagram.

Keyword:

Ladder diagram, IEC operator, series connection

scope of application

Users who are accustomed to using ladder diagrams or who need to "translate" the PLC program of the original control system to TwinCAT 2 in emergency situations

Text of the document

In the ladder diagram editor of TwinCAT 2's PLC Control, IEC operators - comparison, shift, arithmetic operations, etc. - must be placed on separate lines.When they need to be connected in series with other logic, they can only be placed on multiple lines, with intermediate variables established as the output result of the previous line and the execution condition of the next line.For example:

Figure shows how to assign values and perform mathematical operations on variables based on the results of two comparisons. These instructions are very concise when edited using ST, as shown in the figure above, which is written as:

However, ST cannot completely replace ladder diagrams.Not only are traditional electrical engineers accustomed to using ladder diagrams, but in fact, for applications with clear logical sequences, using ladder diagrams to express and monitor them is indeed more intuitive.In TwinCAT 2, IEC operators cannot be concatenated to make ladder diagram programs very long - other PLCs can express logic in one line, but in TC2, it requires several lines, and frequent line breaks make it too "twisted" to monitor the logical correlation of each action.

This problem has been resolved in TwinCAT 3, but long-time users of TwinCAT 2 are still troubled by it. The author has created a library file "Lib_ForLD" that encapsulates IEC operators into function blocks with enable inputs EN and execution outputs ENO, allowing these FB instances to be cascaded freely in ladder diagrams.These function blocks include:



The FBs in Lib_ForLD use Siemens PLC compatible naming, which is also basically consistent with IEC standards.However, IEC instructions support multiple data types, while Lib_ForLD distinguishes between integers, real numbers, and strings, adding suffixes such as _I, _R, and _S.By opening each FB in the library in Resource, you can see its interface and function description, such as the function block "ADD_R":


By using the function block of the library file Lib_ForLD to rewrite the operation in Figure 1, it can be combined into one line, as shown in the figure:



It should be noted that the MOVE instruction currently cannot automatically identify type conversions, and when referencing FB, it is necessary to distinguish between string and numeric values.String assignment uses MOVE_S, while assignment to other variables uses MOVE_R.The output variable of MOVE_R requires explicit specification of its variable type using F_LREAL or F_INT.To monitor the execution of Figure 5 online, an example is shown below:


MOVE is called twice in the figure_ R instance, whose instance name is the same as the FB name, uses FB as FC completely - no need to remember, instant output of the calculation result, and the previous calculation result is cleared for the next calculation.Two MOVEs_ R, assigning a floating-point number to an integer for the first time, using F_ INT specifies the output format, and the result is a rounded integer.The second time, assign an integer to a floating point number, using F_ LREAL specifies the output format, and the result is definite and unambiguous.Try to move the first time The input value of R is changed from 15.69 to 15.49, and its output changes from 16 to 15, as shown in the following figure:

SIN(1) is equal to 0.8415, and SIN(2) is equal to 0.9093.Other function blocks are almost the same as the IEC operators, so it is expected that the meaning is clear from the context and does not require a detailed explanation.If you have any questions, please contact the author.

Special note:

(1) About TwinCAT 3

This issue has been resolved in TwinCAT 3, and there is no need to use this library.

TwinCAT 3 is recommended for new projects.

(2) About code "translation"

This Lib is not written to facilitate user translation of programs, but to facilitate the expression of logic during ladder diagram programming.

Customers who are used to Japanese products have always asked how to "translate" their existing ladder diagram programs into TwinCAT.Beckhoff officially does not recommend translating existing programs from one controller platform to another.Because each platform has its own characteristics, if you cannot understand the working mechanism of TwinCAT and the various tools provided by the system, mechanical "translation" cannot leverage the advantages of TwinCAT, and achieving 100% can only reach the level of the original system.However, there are still some special circumstances where it is possible to translate the program directly.

Therefore, it is recommended that, where possible, PLC programs should be rewritten based on TwinCAT programming templates.Specific process details can be referenced from the original program, but the program framework must be restructured.