Download - Twincat Manual
System Service of TwinCAT The TwinCAT System Service is represented by the TwinCAT icon in the Windows system tray.
The TwinCAT System Service can be accessed through the TwinCAT icon in the windows system tray (Right-Click and Left-Click provides the same menu)
From this menu the other parts of the TwinCAT system can be accessed and the TwinCAT System Properties can be changed
System Manager The TwinCAT System Manager is used to configure the links between Hardware and Software
I/O Configuration All Fieldbus Hardware
PLC Configuration PLC Run-Times (up to 4)
NC Configuration Axes (real and virtual), Cam Tables, Interpolation Channels
System Configuration Properties of the Target System and Real-Time Usage
Menus and Controls
File Menu Allows for creating a new file or opening a saved file.
In addition, it provides a way to open the CurrentConfig.tsm file from the Boot folder, byusing Open from Target also referred to as The Red Folder.
Actions Any time a change is made to the System Manager, the Activate Configuration must be done to implement this change into the running system. Note: The first 6 commands in the Actions menu will be sent to the Target system either local or remote.
The tree view on the left provides access to the configurations of the system manager. When an item on the left is selected its information will be displayed on the right. Items can be added to the System Manager be Right-Clicking on an existing item. Become familiar with this, almost every item you wish to add in both the system manager and the PLC will be done by Right- Clicking and select Add... or Append
System Configuration Provides information and settings for the overall TwinCAT System
The settings available from the Properties of the TwinCAT icon can be accessed from here on a remote system.
Real-Time Settings
Settings Here the Base Time is set; no task can be set to a faster interval than the base time.
The CPU limit of 80% means that TwinCAT will consume no more than 80% to run all of its tasks.
Online The Real Time Usage is graphed and the limit from the Settings tab is indicated bythe thick green line
System Latency should be no more than 5 micro seconds.
Note: Image taken from a laptop with power save features and CPU throttling enabled, both of these create latency problems.
Priorities The list of tasks and their priorities can be seen here
Additional Tasks
Task 1 (added by Right-Clicking on Additional Tasks)
These additional tasks are used by C++ code to talk to variables that are linked to hardware I/O
They can also be used for simulation
Route Settings
Current Routes The Remote Computers shown in this list are the same as in the Properties of the TwinCAT icon.
NC Configuration (Numerical Control) This is the software based motion controller of TwinCAT.The software side of all axes are configured here.
Axes The software limits the total number of axes to 255, the real limit is the amount of CPU andRAM in the computer.
PLC Configuration
IEC Project The PLC editor will create a .tpy file that contains addressed variables that can be linked to hardware. The name of the PLC project file is shown directly below the PLC- Configuration
The IEC1131 Tab shows the path of where the .tpy file was located when it was added to the project. If addressed variables are added to the PLC program the ReScan button can be used to update the list of variables in the System Manager
Standard Task The default task in the PLC is the Standard task and runs every 10ms
Inputs of the PLC Program Input variables have a yellow icon, Output variables have a red icon
Once a variable has been linked (connected) to hardware the icon changes as below
PLC Control The PLC Control provides the user with a combination of tools.
The IEC 61131-3 Language editors
A Visualization Editor
Task Configuration Utility
The Beckhoff Compilers specific to the Target Hardware (BC, BX, CX-ARM, X86)
The Left column provides four tabs at the bottom:
POUs Program Organizational Units This will contain the code written by the programmer, Programs, Function Blocks, and Functions
Data Types Here the programmer can create Structures and Enumerations to be used in the PLC code
Visualizations Interface screens for use by Maintenance personnel or Operators can be created.
Resources The resources tab contains several items. The Global Variable Lists, Library Manager, PLC Configuration, and Task Configuration are all accessible from this tab.
Additionally there is a Message Window at the bottom:
Data types and conversions are shown below:
Variable
A Variable is a name given to a location in memory that stores a value
A Variable has up to 5 properties
1. Name
2. Size (Defined by the Type)
3. Value
4. Memory Location
5. PLC Address
In accordance with IEC 61131-3 a variable name must adhere to the following rules
1. Must begin with a Letter or an Underscore
2. Can followed by Letters, Underscores, or Numbers
No distinction is made between Uppercase and Lowercase Letters
Special characters cannot be used (!@#$%^&*)
Blanks or Spaces are not allowed
Repeated or Sequential Underscores are not allowed
Declaration
All variables must be defined between VAR and END_VAR
Place the name of the variable to the left of the colon
Place the data type to the right of the colon
VariableName : VariableType ;
bStart : BOOL ; (*bStart is of type BOOL*)
iProductNumber : INT; (*iProduct Number is of type INT*)
lrPressure : LREAL ; (*lrPressure is of type LREAL*)
Variable Scope
Global Variables can be read and written to from anywhere in the PLC program.
Local Variables can only be written to from within the POU where they are defined
The local variable of any POU can be read by first accessing the POU instance that the variable is defined in and then using the . to access the local variables defined within that POU
Local variables cannot be written to from another POU.
There are 5 languages to write PLC code:
IL Instruction List
LD Ladder Diagram
FBD Function Block Diagram
SFC Sequential Function Chart
ST Structured Text
CFC Continuous Function Chart (Non-IEC)
IL Instruction List
IL has a similar structure to assembly language and is comparable to the statement list language provided by Siemens.
In IL only 1 command can be processed per line of code.
The command is then followed by a variable or a literal value.
For example the following will increase the variable Speed by a value of 5.
LD Ladder Diagram
LD was created with the intention of representing the electrical wiring diagrams of relay logic
LD is a graphical language that displays a power rail on each side that represents the supply and the common of the wiring diagram
The below examples shows a common latching circuit in LD
FBD Function Block Diagram
FBD is a graphical language that is similar to an electronic circuit diagram
The below example has the same functionality as the above latching circuit
SFC Sequential Function Chart
SFC; although defined as a language, is better thought of as a way to organize code and control the sequence of operation
Each Step and Transition in SFC has code inside of it that can be written in any of the other languages including SFC
ST Structured Text
ST is a high level language which looks similar in syntax to PASCAL
ST is the most powerful and flexible of all the languages
When using ST it is important to remember that the variable being written to (the output) is on the left
The below example provides the same latching circuit operation as the ones above
CFC Continuous Function Chart (Non-IEC)
CFC is an additional language provided within TwinCAT, yet it is not a part of the IEC 61131-3 Standard
CFC is a graphical language very similar to FBD
The order of execution is determined by the number, and is able to be modified by the programmer
Declaration
The Declaration of a Function contains 4 parts
The Name of the Function
The Return type of the Function
The Variables to be passed into the Function
The local variables used by the Function
The Name of the Function
Following the Beckhoff coding convention, the name of the Function starts with F_
The same IEC rules for naming of variables apply to the naming of Functions
Following the Name of the Function is the Return Type
A Function can only Return one variable
For example,
The variables to be passed into the function block are enable, time on, and time off values. The variables to be passed out of the function block Below the output variable has been added The variables that are internal to the function block Below the two timers to be used have been instantiated fbTON is of type TON fbTOF is of type TOF
Activity 1 - Blinker
First start TwinCAT PLC Control and choose File > New, a dialog box appears to choose target system type.
Choose target system type
Step 1: Make a function block for blinker. A dialog box appears to create a new POU project by selecting the Function Block and FBD for both type of POU and language of the POU respectively.
New POUStep 2: Next, right click on the function block diagram editor and select box, the function block AND would be placed automatically.
Function block diagram
Step 3: Right click on the function block diagram editor again and select box. Click on the text AND with the cursor and press F2 to obtain the input assistant.
Function block diagram
Step 4: From the input assistant, select Standard Function Blocks > Timer > TON(FB), and click OK. Repeat this step again to place the second function block of timer on-delay.
Input assistant
Function block diagram editor
Step 5: Declare the variable for both the timer on-delay. Right click in between the VAR and END_VAR on the declaration editor and select Auto Declare.
Auto declare
Step 6: Declare both the timer on-delay as timer1 and timer2 respectively, as shown as the diagram below.
Declare variable for timer on-delay
Step 7: Declare the input and output for the function block. On the declaration editor, right click in between the VAR_INPUT and END_VAR for the declaration of input, while VAR_INPUT and END_VAR for the declaration of output. Classify the input as VAR_INPUT and output as VAR_OUTPUT. Set the data type to be Boolean since true/false is only used in this function.
Declare variable for input
Declare variable for output
Declaration editor
Step 8: On the function block diagram, click on the text ??? with cursor and press F2 to obtain the input assistant. In the local variable, select and click OK to link the variable input and output and variables on the function block diagram. Next, set the PT to 1 millisecond with the format t#1ms as the time would begin counted in millisecond.
Input assistant for variable input, variable output and variables
Step 9: Set the second input of the AND block diagram as timer2 output, as shown in the diagram below. Then, right click on it and select negate to negate the input.
Input assistant for the second input
Complete function block diagram for blinker
Step 10: Create a program for ladder diagram.
New POU
Step 11: On the ladder diagram editor, right click and select Box with EN, this command is used to insert function block into a LD network. Next, click on the text ??? with cursor and press F2 to obtain the input assistant. From the input assistant, select User Defined Function Block > blinker (FB).
Input assistant with user defined function blocks
Step 12: Declare the input and output to be Boolean. First, right click in between the VAR and END_VAR and select Auto Declare. Next, obtain the input assistant and link both input and output to the function block on the ladder diagram editor, as shown in the picture below.
Step 13: Add a new object in visualizations. On the visualizations editor, draw a button for input and a circle for output, as shown in the diagram below.
Complete visualizations for blinker
Step 14: Double click on the button to obtain the regular element configuration dialog box. Select text and enter the text in the content field for the button. Next, select input and tick toggle variable and press F2 on the blank column to obtain the input assistant. From the input assistant, select the blinker input in MAIN PRG.
Step 15: Double click on the circle, select color and click inside and select green color for the alarm color. If the variable is true, the element will be displayed in its alarm color. Next, select variables change color and press F2 on the blank column to obtain the input assistant. From the input assistant, select the blinker output in MAIN PRG. Click OK.
Step 2: Save and name the POU project. Go to TwinCAT System Manager, right click on the PLC - Configuration and select Append PLC Project to open the TPY file for the PLC configuration. Next, click activate configuration to implement the PLC into the running system.
TwinCAT System Manager
Open the TPY file
Step 16: Go back to TwinCAT PLC Control. Build the project by clicking project and then build.
Step 17: Login the project by clicking online and then login. Next, click run to run the simulation.
Step 18: On the declaration editor, double click the word FALSE at the blinker input and press "F7. When it becomes TRUE, the blinker output will start blinking.
While running the simulation
Before click the button on the visualizations
After click the button, it starts to blink.
Activity 2 - Conversion
Step 1: This activity is to convert INTEGER to LREAL with the division of 10. In order to add a new POU, right click on the POU folder and select add object. In this case, structure text (ST) is used.
New POU
Step 2: On the structure text editor, type conversion := (INT_TO_LREAL (input)/10); , as shown in the picture below.
Step 3: Declare the variable input. Set the data type to be integer.
Declare variable input
Step 4: Create a program for ladder diagram.
New POU
Step 5: On the ladder diagram editor, right click and select Box with EN, this command is also used to insert a function into a LD network. Next, click on the text ??? with cursor and press F2. From the input assistant, select User Defined Function Block > conversion (FUN).
Input assistant for conversion (FUN)
Step 6: Declare the input and output to be Boolean. First, right click in between the VAR and END_VAR and select Auto Declare. Next, obtain the input assistant and link both input and output to the function on the ladder diagram editor, as shown in the picture below.
Declare variable for input
Declare variable for output
Complete ladder diagram for conversion
Step 7: On the visualizations editor, draw rectangles for integer and long real in order to display the values. In the visualization element configuration dialog box, select any primary colors in the color category for the inside area or for the frame of the element.
Step 8: Specify a text for each element in the text category by entering the text in the content field.
Complete visualizations for conversion
Step 9: Include %i and %s into the text field for integer and long real respectively.
Regular Element Configuration dialog box (text content)
Step 10: The value of the variable which is defined in Textdisplay in the variables category, it will be displayed in online mode in the visualization object. %s and %i will be replaced by the value. Press F2 on the Textdisplay to obtain the input assistant. From the input assistant, select the conversion input and output for integer and long real respectively.
Regular Element Configuration (text display)
Step 11: Build the project by clicking project and then build.
Step 12: Login the project by clicking online and then login. Next, click run to run the simulation.
Step 13: Double click on the word input_conv and set any value for it.
Write variable for conversion input
Step 14: Press F7 to convert the value after set up a value for conversion input.
While running the simulation
Visualizations
Activity 3: File Access
The log stores in an order actions that occur during an online session.
Step 1: Create a new POU, then, right click on the POU folder and select add object. In this case, structure text (ST) is used.
Step 2: Structure text code.
TON1(IN:=NOT TON1.Q , PT:=t#1s);
NT_GetTime1(NETID:='' ,START:=TON1.Q ,TMOUT:=t#2s ,TIMESTR=> );
(
(*IF Error1 THENbLog:=TRUE;Error1:=FALSE;Data_string := CONCAT(SYSTEMTIME_TO_STRING(NT_GetTime1.TIMESTR),' -->This is error 1 $N');END_IF
IF Error2 THENbLog:=TRUE;Error2:=FALSE;Data_string := CONCAT(SYSTEMTIME_TO_STRING(NT_GetTime1.TIMESTR),' -->This is error 2 $N');END_IF*)Data_string := CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(SYSTEMTIME_TO_STRING(NT_GetTime1.TIMESTR),' Data1 :'), INT_TO_STRING(Data1)),' Data2 : '),INT_TO_STRING(Data2)),'$N');Log_File1(FILE_PATH:=Path , LOG_DATA:=bLog , DATA_STRING:=Data_string );
(*IF NOT Log_File1.bBusy THENbLog:=FALSE;END_IF*)
Step 3: Declare the variables on the declaration editor.
Step 4: Create a folder and name it Logs in disk C. Next, create another folder and name it LOG1 in the Logs folder
Step 5: Enter the directory for the project log. The maximum number of online sessions to be stored can be entered in the menu item Project > Options > Log dialog. If this number is exceeded, the oldest record is deleted to make room for the newest one. Next, tick all the four particulars in the category: user actions, internal action, status change, and exception.
Step 6: Build the project by clicking project and then build.
Step 7: Login the project by clicking online and then login. Next, click run to run the simulation.
Step 8: Enter the data value by double clicking the word data1, data2, data3.
Step 9: Select the menu item Window and then click log to open the log, which the log activity can be checked here.
Step 10: The log file is also stored in an external file entitled LOG1.text that has been created in disk C, as shown in the picture below. Noted: the project log in an external file only occurs during an offline session.
Activity 4: Send, receive, analyze, and store a string in an array and log file (with date and time) via TCP/IP
Twincat TCP/IP enables the implementation of one or several TCP/IP servers and/or clients within the TwinCAT PLC. It allows the establishment/disconnection of communication as well as for the exchange of data (send and receive).
Step 1: Configuration of TCP/IP properties
For Windows XP, click start > Control Panel > select and double click Network and internet connections > select and double click Network Connections. Next, double-click the Local Area Connection icon, highlight Internet Protocol (TCP/IP) tab in the Local Area Connection Properties window that appears:
Then, click Properties, the TCP/IP Properties window will appear. Select Use the following IP address, if the routers LAN IP address is 192.168.1.1, type in IP address 192.168.0.x ( x is from 2 to 253). In this case, 192.168.0.15 is used for the IP address and subnet mask is 255.255.255.0. Click OK to save.
Step 2: Test the TCP/IP connection by using the ping command
Go to Start > Run and enter cmd to open the command window. Next, type ping 192.168.0.15 on the command window and press Enter. The picture below illustrates the result.
Step 3: In TwinCAT PLC Control, several function blocks are used in the main program: fbClientServerConnection, fbSocketSend, fbSocketReceive, and timer-on delay. In the library manager, Tcplp.lib and TcSocketHelper.lib are included.
The function block FB_ClientServerConnection can be used to establish or remove a client connection. A typical client application establishes the connection with the server via FB_ClientServerConnection function block. In the next step instance of the FB_SocketSend and FB_SocketReceive are used to exchange data with the server.
PROGRAM MAINVAR(*Fbs*)fbServerClientConnection: FB_ServerClientConnection;fbSocketSend: FB_SocketSend;fbSocketReceive: FB_SocketReceive;fbTON_RecvTimer: TON;(*Vars*)nServerPort: UDINT:=200;hServer : T_HSERVER;hSocket: T_HSOCKET;bEN_SERV_LISTEN: BOOL;bEN_SERV_CONN: BOOL;bEXE_SERV_RECV: BOOL;tRecv: TIME:=t#10ms;
(*Operation*)sDATA_Current: STRING(255);bSTATE_Machine: BOOL;bSTATE_Alarm: BOOL;iVALUE_SetValue: REAL;iVALUE_CurrValue: REAL;logFile: Log_File;
Path : STRING := 'C:\Logs\LOG1.txt'; (*CE:'Hard Disk:\LOG1.txt' |XP: 'C:\LOG1.tx*)Data_string : STRING(255);Log_data : BOOL;
reset_log : BOOL;set: SR;
string1: STRING;string2: STRING;string3: STRING;stringlength: INT;
mt: BOOL;midstring: STRING;pos1: INT;pos2: INT;datestring: STRING;datastring: STRING;
TON1: TON;NT_GetTime1: NT_GetTime;string_check_length: INT;X : INT;Y: INT;NREC_BYTE: UDINT; IP_Adress: STRING(255) :='192.168.0.15'; fbClientServerConnection: FB_ClientServerConnection;END_VAR
The code above shows the declaration part for the main program.
Declaration part for receiving a string
PROGRAM Receive_StringVARTON1: TON;NT_GetTime1: NT_GetTime;string1: STRING;string2: STRING;string3: STRING;stringlength: INT;mt: BOOL;midstring: STRING;pos1: INT;pos2: INT;datestring: STRING;datastring: STRING;logFile: Log_File; Path : STRING := 'C:\Logs\LOG1.txt'; (*CE:'Hard Disk:\LOG1.txt' |XP: 'C:\LOG1.tx*)Data_string : STRING(255);
Log_data : BOOL;string_check_length: INT;X : INT;Y: INT;Log_txt_array1: Log_txt_array;
END_VAR
Structure text code for receiving a string
string_check_length:=LEN(sDATA_RECV);TON1(IN:=NOT TON1.Q , PT:=t#1ms);NT_GetTime1(NETID:='' ,START:=TON1.Q ,TMOUT:=t#2s ,TIMESTR=> );
logFile(FILE_PATH:=Path , LOG_DATA:=Log_data , DATA_STRING:=Data_string , bBusy=> BUSY_LOG);
IF string_check_length > 0 THEN
Data_string := CONCAT(CONCAT(CONCAT(CONCAT(SYSTEMTIME_TO_STRING(NT_GetTime1.TIMESTR), ' '),'WMS -----> PLC '), sDATA_RECV),'$N');
Log_data := 1;X := X + 1;IF X > 1 THEN(* log data to arrray*)LOG_TXT_ARRAY [0] := sDATA_RECV;Log_txt_array1(Array_Name:= LOG_TXT_ARRAY);
sDATA_RECV :='';BUSY_LOG := 0;Log_data := 0;X :=0;END_IFEND_IF
Using the function block FB_SocketReceive, data from the remote client can be received via the TCP/IP connection server. The connection will have to be established via the function block FB_ClientServerConnection. The data can be received or sent within a TCP/IP network. During the process, a rising edge is generated at the bExecute input for every 1 millisecond. The nRecBytes output returns the number of the last successfully received data bytes.
Declaration part for sending a string
PROGRAM Analyse_stringVARx: INT;mid_string: STRING(255);Pos1: INT;Pos2: INT;Data_001: STRING (255);Data_002: STRING (255);Data_003: STRING (255);Data_004: STRING (255);Data_005: STRING (255);Data_006: STRING (255);Data_007: STRING (255);Data_008: STRING (255);Data_009: STRING(255);Data_010: STRING (255);Data_011: STRING (255);Data_012: STRING (255);Data_013: STRING (255);Data_014: STRING (255);Data_015: STRING (255);Data_016: STRING (255);Data_017: STRING (255);Data_018: STRING (255);Data_019: STRING (255);Data_020: STRING (255);
TON1: TON;NT_GetTime1: NT_GetTime;Send_string : STRING (255);
String_len: INT;Send_timing: INT;
Path : STRING := 'C:\Logs\LOG1.txt'; (*CE:'Hard Disk:\LOG1.txt' |XP: 'C:\LOG1.tx*)Data_string : STRING(255);Log_data2 : BOOL;logFile: Log_File;log_write: BOOL;
END_VAR
Structure code for sending the string
IF x = 0 THENx := 100;END_IF
String_len := LEN(Log_txt_array[x] );TON1(IN:=NOT TON1.Q , PT:=t#1s);NT_GetTime1(NETID:='' ,START:=TON1.Q ,TMOUT:=t#2s ,TIMESTR=> );
IF String_len = 0 THENx := x -1;ELSEmid_string := Log_txt_array[x];pos1 :=0;pos1 := FIND(mid_string,'');IF pos20 THENmid_string:= LEFT(mid_string,pos2-1);Data_001:=LEFT(mid_string,6);Data_002:=MID(mid_string, (LEN(mid_string)-6),7);
Send_string:= CONCAT(CONCAT(CONCAT(CONCAT(SYSTEMTIME_TO_STRING(NT_GetTime1.TIMESTR), ' '),'WMS