Installation and the first example

It is better to use the same version of the software as mentioned by Beckhoff to avoid complications as we have used in HMI development. It does not mean you have to use the same version as we have used here. For example, we can use all the latest versions from Beckhoff.

 

=>We have used Windows 10 and Windows 7 for testing and developing the TwinCAT software

=> .Net 4.7.2 developer pack or equivalent (https://dotnet.microsoft.com/download/dotnet-framework/net472)

=> Visual Studio (2013, 2017 or 2019) community (make sure to include web-related components if visible) or pro version https://visualstudio.microsoft.com/downloads/

=> TwinCAT 3.1 – eXtended Automation Engineering (XAE), version 3.1.4024.7 (https://beckhoff.com/)

=> TE2000 | TC3 HMI  , version 1.10.1336.404 (https://beckhoff.com/ for developing HMI and GUI)

Beckhoff software has an engineering version and run time version. We shall use the engineering version for developing the application and on the other hand, run time is used for only executing the program. One good example is that when we install Beckhoff TwinCat Software to PLC, we install only the run time version. Even it is not possible to install the engineering version to PLC due to memory constraints. The engineering version offers all the development, debug, etc tools on top of run time.

Download all the software and run those executables as administrator. Good to install visual studio before installing TwinCAT 3.1

Now we should have all the necessary software so we can start to develop.

=>Start Visual Studio

=>Select TwinCAT PLC template as shown in the following figure.


Visual studio will create a solution as shown in the following figure

=>Select PLC as shown in the above image then press on right mouse, then add new item, standard PLC project and finally press Add to complete.

Now we should have something like the following image.

Now  double click on the MAIN (PRG) and it will open an editor where we can replace it with the following code (by copy paste to the upper part and lower part of the editor).

//Header, upper part
PROGRAM MAIN
VAR
testBool : BOOL := FALSE;
testInt : INT := 0;
testReal : REAL := 3.14;
testString : STRING := 'MyString';
myTimer : TON;
END_VAR


//Lower part (body of the program)
myTimer(IN:= TRUE, PT:=T#500MS); IF myTimer.Q = TRUE THEN myTimer(IN:= FALSE); testInt := testInt +1; IF testInt > 30999 THEN testInt:= 0; END_IF END_IF

Description of the program:

The upper part of the page is the header where we declare variables ( we call it also tag or symbol in PLC world). This is just a convenient way of accessing the content of the PLC. A good naming convention can be helpful for better readability and maintenance.

We have declared a boolean (for holding true or false value) variable as testBool. It has been initialized as false. See the difference between how we declare these variable in different language.

testBool : BOOL := FALSE; // in structured text language
Boolean testBool = false; // in C#, Java etc programming language

In the same way, we have declared other few variables testInt (integer type, initialized to 0), testReal (real number, initialized to 3.14),  testString (a string type, initialized as “MyString”). All these are declared  inside VAR  and  END_VAR block.

We have used a timer variable as well. The timer will give a signal in every 500 milliseconds then we increase the integer type variable (testInt) by one just to show that the program is doing something. Since the testInt is a signed number, we reset it when it reaches 30999. See different data type used in PLC structured text (https://www.hemelix.com/automation/data-type-used-in-plc-programming/)

 

If  we activate the program by pressing activate button in visual studio (arrow 1) and then login to the program (arrow 2) we see the above program running.

Congratulations! you have made your first ST program that is running in TwinCAT simulator.

Explanation of the program:

This is a very simple program, we are declaring 4 variables (those types are BOOL, INT, REAL, and STRING), and those variables are initialized with some default value. We have also declared a timer that ticks on each 500MS interval. testInt variable is increased by one on each timer tick, when the variable value is higher than 3099 then we reset the variable to 0  and it continues with this cycle until we stop the program.

Download the program

Know more about timer

Know more about ADS connection