Differences and Similarities between TwinCAT and Step 7 Programming

    

People familiar with Siemens PLC programming always have some concepts to re understand when using TwinCAT PLC for the first time. This article compares the two based on the my personal experience. I hope to be systematic, comprehensive, and correct, but in reality, I may not necessarily achieve it. Welcome to leave a message for supplementation and correction.

The similarity between the two is that they both comply with the IEC61131-3 programming standard. The differences include but are not limited to the following points:

1. Program execution cycle


Traditional PLC programs execute PLC code in a line by line scanning mode. Immediately after the completion of the first pass, the second scan execution begins. The time interval between two consecutive output updates is not fixed.

TwinCAT PLC needs to call the program according to the specified task cycle, with a default cycle of 10ms. After the first execution of the program statement is completed,

2. Interruption

Traditional PLCs have interrupt tasks, which immediately interrupt normal programs and execute interrupt programs after an event occurs.
There is no interrupt in TwinCAT PLC, but there is time-sharing multitasking.At each Base Time tick, high-priority tasks are executed first.The shortest Base Time can be set to 50us, which actually serves as a substitute for interrupt tasks.

3. Organization object of the program


Siemens is divided into OB, FB, and FC.The controller has a number of OB, FB, and FC numbers, some of which are fixed for specific purposes, such as OB86, OB100, FB52, etc., while others are freely programmable by customers.When the PLC is running, it recognizes the number of these objects.The symbol or name of the object is mainly for developers to understand and increase the readability of the program.

TwinCAT PLC is divided into PRG, FB, and FC.They correspond to OB, FB, and FC in Siemens PLC, but PRG, FB, and FC can be freely named without requiring numbers, so there is no limit on the number.

OB, FB, and FC in Siemens project files can be modified and downloaded separately, while updates to TwinCAT PLC programs require the entire program to be fully downloaded each time, and cannot be modified or downloaded individually for a program unit.

3.1. Comparison between OB and PRG


Siemens PLC has reserved several numbered OB, FB, and FC, some of which have fixed special purposes, such as OB86, OB100, FB52, etc., while some of which can be freely programmed and used by customers.

In TwinCAT PLC, all PRGs can freely define functions, and there are no special PRGs.Even the MAIN program automatically added when creating a new program can be renamed or even deleted by the user.

3.2. Comparison with FB

For the FB in Siemens PLC, each instantiation creates a Shadow DB.The Shadow DB also occupies a DB number.

FB in TwinCAT PLC requires a unique name for each instance.The name of an instance is equivalent to the Shadow DB in Siemens PLC.However, FB instances do not require a number or address to be specified.When the PLC starts, it allocates a separate memory area for each FB instance, which includes all interface variables and intermediate variables corresponding to the FB.As long as there is no power failure, this memory area can "remember" the values of the intermediate variables.After a power failure and restart, the values of the intermediate variables are reset to zero.

FBs in TwinCAT PLC can be nested, and multiple small FBs can be encapsulated into a large FB. Multi-layer nested FBs make modular programming possible.FBs in TC3 can also inherit and extend, making the program more reusable.

3.3. Comparison of FC

Siemens FC is often used as a subroutine or to call other FCs, and can also operate on global variables, DB data, and so on. At this point, the return value of the FC is basically not of concern.

In TwinCAT PLC, because there are infinitely many PRGs that can be customized, FCs are typically used only as functions that immediately compute results.In the same project, FCs can be run repeatedly an infinite number of times.FCs do not "remember" the values of intermediate variables.

4. Public DB and Structure


Siemens' Public DB is retained after power loss, and its content can be customized.After defining a Public DB, its number, name, internal element type, and order are determined.The values in the DB are retained after power loss.Is there a multiple Public DB with the same variable element?The internal elements of the Public DB can be accessed by name or address offset.

In TwinCAT PLC, the equivalent of Public DB is the Structure and its instances in the custom DataType.However, defining a Structure only determines its name and the type and order of its internal elements.The PLC will allocate a memory area for each instance after instantiating the Structure.The Structure itself does not have an address, only the structural variable has an address, which is actually the address of its first element, that is, the starting address of the entire structural variable, equivalent to a pointer.As long as there is no power failure, the value of this memory area will be retained.After power failure and restart, the value of the structural variable will be cleared.The internal elements of the structural variable are accessed entirely by name.Usually, the memory address of each internal element is not concerned.If the address of the internal element needs to be considered, it is necessary to pay attention to the alignment mode of different platforms.

To implement the Public DB function in TwinCAT PLC, some settings are required to maintain the structure variable during power loss.Structure can also be nested.

5. Timer and Counter

In Siemens PLC, several timers and counters are provided, such as 99, 999, or 9999.The timers are distinguished by ID numbers.Timers can also be named, but only for the convenience of developers.The same applies to counters.The number of timers and counters is limited, but the intermediate values can be retained during power loss.

In TwinCAT PLC, timers and counters are both function blocks in the Standard.lib. Each reference only requires a name, which can be any character and has no limit on quantity.After the PLC restarts, the intermediate values of timers and counters will be reset unless special processing is performed.In addition, the instance name of the timer or counter function block cannot be directly used to replace its output point.In other words, the instance name represents a memory area, similar to a Shadow DB, rather than a BOOL output point.

6. Rise and fall edges

In Siemens PLC, the rising edge and falling edge are similar to a function, placed in a row of Ladder, and the output is immediately available.

In TwinCAT PLC, the rising edge and falling edge are both function blocks in Standard.lib. Each reference only requires a name, which can be any character and has no limit on quantity.You cannot directly use the instance name of the rising edge and falling edge function blocks to replace its output point.In other words, the instance name represents a memory area, similar to a Shadow DB, rather than a boolean output point.

7.  First Cycle

In Siemens PLC, there are some special tag bits, such as the first cycle tag and the 1s pulse tag.

In TwinCAT PLC, if you need the FirstCycle flag, there are two ways. One way is to create a variable with an initial value of True and set it to False at the end of the program.The other way is to reference the TcSystem.lib library, and then you can use the FirstCycle variable in the global variable TcTaskInfoArray array.

8.   1s pulse

In Siemens PLC, there are some special flag bits, such as the first cycle flag and the 1s pulse flag.

In TwinCAT PLC, there is no ready-made 1s pulse tag, and users need to create one using timers.Usually in a project, it is possible to create commonly used tag bits: 1s pulse, 1s square wave, as global variables that can be used in all programs.

9. Symbol and Address

In Siemens PLC, the key information of variables is the address, and all operations are based on the address.In particular, the variable addresses of the channels in the IO modules strictly correspond to the installation locations of the hardware modules.

In TwinCAT PLC, the key information of a variable is its name. When the PLC starts up, a variable name corresponds to a memory address.All operations are actually based on this memory address.For IO variables, even if it does not correspond to a channel of a hardware module, it can run operations directly in memory.Therefore, TwinCAT PLC can run without hardware simulation, and only when it needs to connect to hardware, it is necessary to map PLC variables to hardware channels.

10. Persistent power-down

In Siemens PLC, there is a dedicated address area that is maintained during power loss.Any variables defined to this address area are automatically maintained during power loss.

In TwinCAT PLC, all address areas are PC memory.There are multiple ways to achieve power-down persistence, one of the simplest methods is to directly declare a variable as a Persistent type. This type of variable is automatically stored in a special file when the PLC is turned off, and the variable value will be automatically restored from the file when it is next started.

11. Structures and Arrays

TwinCAT PLC supports custom structures.The structure is similar to the DB block in Siemens PLC.

TwinCAT PLC supports multidimensional arrays, allowing you to manipulate different elements through array subscripts.

12. Enumeration

TwinCAT PLC supports custom enumeration.

13. Pointer

TwinCAT PLC supports pointer operations, typically using the ADR() function to obtain the variable address to assign to a pointer-type variable.Then, the variable is operated on through pointer operations, or memory is copied, cleared, or copied.

14. Customize FB, FC, and Library Files

Users can customize STRUCTURE, Global Variable, PRG, FB, and FC, and can export them directly for use by other engineers, or package them into Lib library files for encryption and sharing.

15. Object-oriented programming

PLC in TwinCAT 3 supports object-oriented programming, which supports the attributes and methods of FB, as well as object inheritance.It also abstracts the concept of Interface.