Twincat Manual
Twincat Manual
Twincat Manual
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
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, by
using 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 by
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
These additional tasks are used by C++ code to talk to variables that are linked to hardware I/O
Route Settings
Current Routes The Remote Computers shown in this list are the same as in the Properties
of the TwinCAT icon.
Axes The software limits the total number of axes to 255, the real limit is the amount of CPU and
RAM 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.
A Visualization Editor
The Beckhoff Compilers specific to the Target Hardware (BC, BX, CX-ARM, X86)
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.
Variable
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
Declaration
VariableName : VariableType ;
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
IL Instruction List
LD Ladder Diagram
ST Structured Text
IL Instruction List
IL has a similar structure to assembly language and is comparable to the statement list
language provided by Siemens.
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 example has the same functionality as the above latching circuit
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
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 is an additional language provided within TwinCAT, yet it is not a part of the IEC 61131-3
Standard
Declaration
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
For example,
The variables to be passed into the function block are enable, time on, and time off values.
Activity 1 - Blinker
First start TwinCAT PLC Control and choose File > New, a dialog box appears to choose target system
type.
New POU
Step 2: Next, right click on the function block diagram editor and select box, the function block AND
would be placed automatically.
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.
Input assistant
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.
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.
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.
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).
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.
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.
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.
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.
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).
Visualizations
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.
(*
IF NOT Log_File1.bBusy THEN
bLog:=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 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.
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 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.
PROGRAM MAIN
VAR
(*Fbs*)
fbServerClientConnection
fbSocketSend
fbSocketReceive
fbTON_RecvTimer
(*Vars*)
nServerPort
hServer
hSocket
bEN_SERV_LISTEN
bEN_SERV_CONN
bEXE_SERV_RECV
tRecv: TIME:=t#10ms;
(*Operation*)
sDATA_Current
bSTATE_Machine
bSTATE_Alarm
iVALUE_SetValue
iVALUE_CurrValue
logFile: Log_File;
: FB_ServerClientConnection;
: FB_SocketSend;
: FB_SocketReceive;
: TON;
: UDINT:=200;
: T_HSERVER;
: T_HSOCKET;
: BOOL;
: BOOL;
: BOOL;
: STRING(255);
: BOOL;
: BOOL;
: REAL;
: REAL;
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.
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_IF
END_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_string
VAR
x: 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
END_IF
Log_txt_array[x] := '' ;
END_IF
IF bEXE_SERV_SEND = 1 THEN
Send_timing := 1 + Send_timing;
END_IF
IF send_timing > 1 THEN
bEXE_SERV_SEND := 0 ;
Log_data2 :=0;
Send_timing := 0;
END_IF
IF Log_data2 =1 AND BUSY_LOG=0 THEN
log_write := 1;
ELSE
log_write :=0;
END_IF
Path := 'C:\Logs\LOG1.txt';
logFile(FILE_PATH:=Path , LOG_DATA:=log_write , DATA_STRING:=Send_string , bBusy=>
BUSY_LOG2);
Using the function block FB_SocketSend, the data can be sent to a remote server via TCP/IP
connection server. The remote client connection will have to be established via the function
block FB_ClientServerConnection.
Step 4: Save the TwinCAT PLC Control file. Open TwinCAT System Manager, right-click on
the PLC-Configuration and append the PLC project. Next, click on the Activate Configuration
icon to run the simulation.
In the Multiway terminal, select TCP as Mode and Serve as Mode TCP. Next, set the IP
address of the Remote Node to be 192.168.0.15 plus the destination port 4364 and press
Connect. The connection state will change to connected. The connection is now open to allow
data to be sent and received.
Next, enter some data on the Multiway terminal and this data will then be received by the
TwinCAT PLC Control and placed in the PSPAD and log file.
The data received by the TwinCAT PLC Control, as shown as the picture below:
Step 7: On the other hand, the data is also stored in an external file entitled LOG1.text that has
been created in disk C,as shown in the picture below.
Activity 5: Transfer data using Omron Multiway on host computer to TwinCAT PLC
Control and PSPAD on client computer via TCP/IP connection between two computers
(host and client).
Step 1: Establish TCP/IP connection between two computers.
On the host computer, 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 internet Protocol (TCP/IP) properties. 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.
On the client computer, click start > Control Panel > select Network and Internet > select
Network and Sharing Center > select Change Adapter Settings > select and double-click the
Wireless Network Connection to obtain Wireless Network Connection status. Next, click on
the details to obtain the Network Connection Details, as shown in the picture below. In this
case, IPv4 Address is used.
Step 3: On the client computer, open TwinCAT System Manager, right-click on the PLCConfiguration and append the PLC project from activity 4 above. Next, click on the Activate
Configuration icon to run the simulation.
Open the PLC project in TwinCAT PLC Control, go to online > click login to load the
PLC program, after it get online, then go to online > Run. The bottom right will show if
made it to run. Next, go to visualization editor, click HMI and key in the clients IP address
on the box.
Step 4: Connect the host and client computers using Omron Multiway software. On the host
computer, open the Omron Multiway, insert clients IP address on the remote node and hosts
IP address on the local node. Make sure the mode TCP is set to be server and press connect.
The connection state will change to connected. The connection is now open to allow data to
be sent and received between host and client.
Next, enter some data on the Multiway terminal and this data will then be received by the
TwinCAT PLC Control and placed in the PSPAD, as shown in the picture below.
Step 5: Check the data that being sent to both TwinCAT PLC Control and PSPAD on the
client computer.