Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views45 pages

TC Quick Start 28

The document provides a quick start guide for Beckhoff's TwinCAT, detailing system requirements, installation procedures, and an overview of its components such as TwinCAT PLC Control and System Manager. It emphasizes the need for specific hardware and software to run TwinCAT effectively, including a minimum of 16 MB RAM and Windows NT/2000. Additionally, it outlines the programming capabilities of TwinCAT PLC, which supports multiple programming languages compliant with IEC 61131-3.

Uploaded by

fasp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views45 pages

TC Quick Start 28

The document provides a quick start guide for Beckhoff's TwinCAT, detailing system requirements, installation procedures, and an overview of its components such as TwinCAT PLC Control and System Manager. It emphasizes the need for specific hardware and software to run TwinCAT effectively, including a minimum of 16 MB RAM and Windows NT/2000. Additionally, it outlines the programming capabilities of TwinCAT PLC, which supports multiple programming languages compliant with IEC 61131-3.

Uploaded by

fasp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

®

Beckhoff TwinCAT
The Windows Control and Automation Technology

TwinCAT Quick Start

Last change: 11.12.2001


Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

Contents

TwinCAT Quick Start


1. Introduction to working with TwinCAT and System
requirements 4
2. Installation 5
Starting the installation program 5
End of installation 12
3. TwinCAT PLC Control 14
Overview 14
The PLC standard IEC 61131-3 15
4. TwinCAT System Manager 17
Overview 17
5. TwinCAT Scope View 18
Overview 18
6. Sample Program 19
Sample Maschine.pro 19
Following Program Flow 27
7. Conversion of the sample program 31
Declaration of the variables 31
Setting up the Bus Terminals 32
8. Visual Basic Link 41
Programming interface ADS OCX 41
Sample program 42

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

1. Introduction to working with TwinCAT and System


requirements
The aim of this introduction is to provide the reader with a swift overview of the possibilities offered by TwinCAT
without going into details. The explanations are supplemented by an example application that is added to step
by step in the individual chapters.
Refer to the individual instructions of the programs for detailed information.

System requirements
486 processor or higher
To operate TwinCAT, you need a PC with a 486 type processor or higher. The computer must also have a ma-
thematical coprocessor. The coprocessor will be integrated in a 486DX, DX2 or DX4 processor. In the case of
486SX or SX2 processors, the computer can be upgraded with an overdrive processor, which then contains a
coprocessor.

At least 16 MB RAM
The computer must contain at least 16 MB of RAM. Memory requirement increases if applications are large or if
additional software is run on the computer in parallel with TwinCAT

Windows NT/2000 operating system


TwinCAT only runs under the Windows NT/2000 operating system. Operation under Windows NT V3.x, Win-
dows 95 or the Windows V3.x 16-bit operating systems is not possible.

The Example program requirements


You need the following software and hardware to be able to run the sample program:
- TwinCAT version 2.0 or higher
- Microsoft Visual Basic version 4.0 or higher
- PC interface card for II/O-Lightbus (FC2001)
- Bus coupler for II/O-Lightbus (BK2000)
- 2 Bus terminal with each 2 digital outputs (KL2032)
- Bus end terminal (KL9010)
- Wiring material (fiber-optic cable, stranded wires, etc.)
- 24V power supply unit

Demokit:
The required Hardware for the sample program is contained in the Beckhoff Demokit Lightbus.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

2. Installation
Starting the installation program
Start the SETUP.EXE program on the CD. To do this, open the Explorer, change to the CD ROM and double
click the SETUP.EXE program.
The following dialog box opens. Click 'Next ' to continue.

Selecting the language


Select the national language in which you wish to install TwinCAT. To install it in English, for example, select
the entry 'English' and click 'OK' to confirm your input. Installation is completely menu-prompted.

Exit programs
The installation program recommends, to exit all Windows programs, before running this Setup program.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

License Agreement

Entering the User Information


You must enter the serial number in this dialog box. You will find the serial number in your TwinCAT purchase
agreement. Leave this box blank if you wish to install the demo version of TwinCAT.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

Select installation level


One of the following Installation levels must be selected:

Description of the product levels:


TwinCAT CP
Contains the necessary components for the Special functions (UPS, S-Keys, ...) of the Beckhoff Control Panels
TwinCAT IO
The (User Mode-) Program can directly access the IO Devices. This Level does not include the PLC.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

8
TwinCAT PLC
In TwinCAT PLC includes the IEC61131-3 Software Development kit.
TwinCAT NC PTP
Further to PLC this module contains the NC/CNC functionality to control the PTP-Axis .
TwinCAT NC I
Further to PLC this module contains the NC functionality interpolates drives in 3D.

Installation type
Select TwinCAT as 30-Day-Test-Version, Register an installation or a request for further Information. If you
have no Registration Number 'Install the 30 day Version' and register during the 30 day period.

installation type Restrictions


30 Day Version TwinCAT is usable for 30 Days without any restictions . During this time a license
number must be appled for otherwise after the 30 days has expired the program can
no longer be started.
Registration via Tele- When you have completed the installion you will be requested to enter the license
phone or Internet number. See below

Registration key
If you decide to register TwinCAT, you must now enter the Key. You must obtain this register key directly from
Beckhoff Industrie Elektronik. The Telephone number is in the dialog box. In order to compute the registration
number, the System ID must be given. The System ID is shown in the dialog box.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

Component selection
By Default, not all the components in TwinCAT are installed.

Component Description
TwinCAT IO Allows the direct access to IO via a DLL. Can be installed with TwinCAT PLC or Twin-
CAT NC PTP.
TwinCAT Scope Program for the graphical visualisation of the TwinCAT process variables.
View

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

10

TwinCAT Cam Ser- Fast Cam Server.


ver
TwinCAT EDS and The EDS (DeviceNet) and GSD (characteristic master device file, Profibus) makes all
GSD Files the settings available to the user for the configuration of his system.

Selecting the destination path and the program folder


You can select any directoy here as well as selecting the Program folder. Usually the default values would be
accepted.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

11

After the installation of TwinCAT


The installation of the TwinCAT Information System is automatically started. The TwinCAT Information System
contains the documentation of TwinCAT. Click 'OK' to start this installation.

End of the installation of the TwinCAT Information System


Click 'Finish' to finish the installation of the Information System. Afterwards also the installation is terminated by
TwinCAT.

Restart the computer


After the installation has been completed, the Computer must be restarted.

Now, the setup has completed the installation of TwinCAT.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

12

End of installation
New Program icons
After the installation the Windows NT/2000 Startup menu will contain a new folder containing five program sym-
bols and two further program folders.

StartUp

TwinCAT automatically starts all programs in the Startup folder if activated via the Auto-Boot Function. This is to
ensure that user programs that use process values from TwinCAT are started after TwinCAT.

TwinCAT System-Manager

With the help of this program, assign physical I/O adresses (Fieldbus) to the logical process variables (PLC
program). This assignment is called mapping. The realtime properties are defined here.

TwinCAT PLC Control

This is the software development kit for IEC61131-3. The PLCPrograms are written and tested here.

TwinCAT System Control

Apart from the visible programs, there are also hidden tasks and drivers that run in the background. The Twin-
CAT System Control administrates these programs.

TwinCAT Scope View

With the help of the TwinCAT Scope View process values can be displayed graphical in real time. The dynamic
Axis values can be thoroughly examined.

TwinCAT in Windows NT/2000


When the System is started the TwinCAT Real time server icon is displayed on the right of the Taskbar.

The color indicates the general operating state of the system. It can have one of the states 'started' (green),
'starting' (yellow)' and 'stopped' (red). If you click the icon, a pop up menu opens in which you can define further
system settings. Within the scope of these instructions, you can accept the default setting. The TwinCAT server
can be stopped and started in this menu.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

13

TwinCAT Information System

The TwinCAT Information System is a constantly growing reference source for TwinCAT products. It contains
technical information, manuals, example code, the TwinCAT Knowledge Base and much more. The hierarchical
arrangement of the documents makes it easy to find the required information.

Full version:
If you installed TwinCAT of the Beckhoff product CD, now the entire TwinCAT information system is installed on
your computer.

Base Version
If you loaded yourselves the installation from TwinCAT from the Internet, only a basic version of the TwinCAT
information system on your computer is installed. In order to receive a complete version (size 13 MB), you have
several possibilities:
You will find it

· on the Beckhoff Product CDs

· on our FTP-Server

· ftp://ftp.beckhoff.com/Software/TwinCAT/InfoSystem/1033/TcInfoSys.exe

· and on our Web-Server

· http://www.beckhoff.com

You can infer the statements for the installation also from the start page, which you can open with the start me-
nu, as represented above.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

14

3. TwinCAT PLC Control


Overview

What is TwinCAT PLC Control?


TwinCAT PLC Control is a complete development environment for your PLC. Use of the editors and debugging
functions is based upon the proven development program environments of advanced programming languages.

IEC 61131-3
TwinCAT PLC allows the PLC programmer to obtain easy access to the powerful language resources of IEC
61131-3. The following features were implemented during the course of TwinCAT PLC development:

Function blocks
TwinCAT PLC supports different programming languages: instruction list (IL), structured text (ST), sequential
function chart (SFC), function block diagram (FBD) and ladder diagram (LD).

Test without PLC


The integrated software PLC allows you to test the PLC program without external hardware.

Changes during operation


Programs are modified "online" in the PLC.

Reusability
Reuseability of existing PLC program blocks

Standardized interfaces
A link to other programs and computers, even through a network, is possible thanks to standardized open inter-
faces (DDE, OCX, DLL, etc). By way of OLE, for example, you can realize a data transfer to MS Excel without
the need for programming effort and you can process the data further in that application

Heterogeneous environment
Thanks to the use of system-independent and widespread network protocols, it is possible to integrate Twin-
CAT into a heterogeneous network environment. For example, an Oracle database running under UNIX can
exchange data with TwinCAT via TCP/IP and can process this data further in a PDA or PPS system or can
specify parameters within TwinCAT PLC in order to influence the production process.

High-level language libraries


Complex algorithms can be developed in C/C++ or Assembler, for example, in order to address them from
TwinCAT PLC. There are many third-party vendors of such libraries who deal with specific task areas.

SCADA systems
Some manufacturers of SCADA systems (Fix32, InTouch, Citect, Genesis, Wizcon, etc) offer direct driver sup-
port for linking to TwinCAT

Remote access
Central programming of distributed control systems through a network (including ISDN) is possible thanks to the
fact that the programming and run time environments are separate.

Intuitive development environment


Simulation according to the example of technically matured high-level language development environments
(e.g. visual C++), breakpoints, single step mode and tracing of variables etc. are possible using TwinCAT PLC,
as is the case with modern development environments.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

15

The PLC standard IEC 61131-3


Five different languages conforming to IEC 61131-3 are available for creating a PLC program using TwinCAT
PLC.

Instruction list (IL)


The instruction list is very similar to the STEP5 programming language. Every instruction begins in a new line
and contains one operator and one or several operands. An instruction may be preceded by a label, followed by
a colon. A comment must be the last element in one line.
Example:

label operator operand comment


Start: LD Basin_level (* Load level *)
GE 13 (* Limit reached? *)
JMPC Pump_on
R Pump_control (* Pump off *)
JMP End
Pump_on: S S Pump_control (* Pump on *):
End:

Structured text (ST)


In the case of this programming language, we also speak of a higher-level programming language because it is
not "machine-oriented" commands that are used. Instead, power command strings can be created by way of
abstract commands. From the area of the PC, Basic, PASCAL and C are comparable higher-level programming
languages.
Example:

operator operand comment


CASE Temperatur_furnace OF (* control heating output *)
60..99: Heating := 80; (* 80% *)
100..149: Heating := 60; (* 60% *)
150..199: Heating := 35; (* 35% *)
200..250: Heating := 10; (* 10% *)
ELSE: Alarm := TRUE; (* Set alarm *)
END_CASE;

Function block diagram (FBD)


The basic idea behind PLC programming with the function block diagram is that the program is structured in
function-oriented logical sequence cascades (networks). Within one network, the executed direction is always
from left to right. All input values must always have been generated before execution of a function block. Evalu-
ation of a network is not concluded until the output values of all elements have been calculated.
Example:

Ladder diagram (LD)


The representation of logical sequences in the form of the ladder diagram originates from the area of electro-
technical plant engineering. This mode of representation is particularly suitable for implementing relay switching
operations in PLC programs. Processing is limited to the Boolean signals 1 and 2.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

16

Sequential function chart (SFC)


The sequential function chart is expedient wherever sequencer program-ming is necessary. Complex tasks are
split into clearly arranged portions of program (steps). The sequence between these steps is then defined
graphically. The steps themselves are created in a different programming language (ST, IL,…) or can also be
represented in SFC again.
Example:

SFC programs essentially consist of steps, transitions and their links. Each step is assigned a set of com-
mands. These commands are executed when the step is active. A transition must be fulfilled to ensure that the
next step is executed. The steps and the transitions can be formulated in any chosen language.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

17

4. TwinCAT System Manager


Overview

What is TwinCAT System Manager?


The TwinCAT System Manager is the central tool for the configuration of the TwinCAT System.
The inputs and outputs of the participating software tasks and the physical inputs and outputs of the connected
field busses are managed by the TwinCAT System Manager. Additionally the online values of the active confi-
gurations can be regarded.
The logical inputs and outputs are assigned to the physical ones by logically linking variables of the software
tasks and variables of the field buses.

Configuration modules of the TwinCAT System Manager


In the following, the main components of the TwinCAT System Manager are listed. The existence of these
components depends on the level of the installed TwinCAT System.

Realtime Configuration
Realtime configurations and creation of user defined tasks.

PLC Configuration
Under this entry, all PLC projects that are running on the local system are listed (currently up to four projects).

Cam Configuration
Electronical Cam Server and its configuration.

I/O Configuration
Corresponding field bus interface cards are necessary to link the control to the process level. Which cards are
used must be defined under this entry.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

18

5. TwinCAT Scope View


Overview

What is Scope View?


TwinCAT Scope View is an analysis tool providing graphical display of the variables related to various PLC
tasks.
Curves can be drawn versus time, or you can choose to see XY displays.
Each Scope View can utilise several channels, the number being limited only by the memory size and compu-
ting capacity. For time displays, one variable is assigned to each channel.

Scope View analysis


Tools are available for analysis within Scope View.

Data backup
TwinCAT Scope View permits data to be saved in a number of data formats, such as in an Excel table.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

19

6. Sample Program
Sample Maschine.pro
Creation of applications using TwinCAT will be explained with reference to a sample program. This program
represents a machine tool for any chosen workpieces. After you have installed TwinCAT, you will find it in the
'\TwinCAT\Samples\First Steps' directory. It has the name 'Maschine.pro'.
Sketch:

Description
1) The conveyor belt is moved by 25 steps.
2.) The drill is moved down for 2 sec.
3.) The drill is moved up for 2 sec.
4.) Begin at step 1 again.

Start TwinCAT:
Before you are able to execute the program, you must activate the TwinCAT real time server.

To do this, click on the TwinCAT real time server icon and activate the "Start" command from the 'System' me-
nu. The color of the icon changes through yellow to green, which means that Twin-CAT's real time kernel is
active.

Starting TwinCAT PLC Control :


Start now the programming surface from the TwinCAT PLC. Then click with the mouse 'start' -> 'programs' ->

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

20

'TwinCAT system' -> 'TwinCAT PLC control'.

Open Projekt:

A PLC project is stored in a file on the hard disk or on a diskette that bears the name of the project. To open a
project, select the 'File' menu item and then the 'Open' command.

Select directory:

Switch to the directory specified above by clicking the symbol shown on the left in the dialog box. Double click
the 'Samples' entry. Then proceed in exactly the same way with the 'First steps' entry.

Select project:
Select the 'Maschine.pro' project by clicking the entry in the dialog box with the mouse and by then executing
the 'Open' command.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

21

Items of the PLC Control:


After opening Maschine.pro select the POU (programm object) MAIN and make a double click with the left
mouse button. The following dialog box opens:

In the upper blue beam the project name Maschine.pro is located.

Among them there´s the command menu


and the toolbar.

The lowest grey beam contains the status line.

The dialog window divides into three individual windows, it contains the object list, the variable declaration and
the program representation.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

22

Object list:

TwinCAT distinguishes three kinds of basic objects in a project:


· (Program) Blocks
· Data Types
· Resources

Variable declaration:

A PLC program stores its data in variables. Variables are comparable to flag words or data words. Before a
variable can be used, it must be declared, i.e. its affiliation to a specific data type (e.g. BYTE or REAL) must be
made known. Declaration also involves defining specific at-tributes such as battery buffering, initial values or
affiliation to physical addresses. If a variable is not needed in the input or output image, i.e. only within the PLC
program, the PLC programmer need not worry about the stor-age location of the data. This is taken care of by
TwinCAT. This avoids unintentional overlapping of flag words/data words as was possible in previous systems
(side effects). As in the case of variables, function blocks also have to be declared (instance). In the example,
one instance each (drive, down and up) of the three functions 'DriveType', 'DownType' and 'UpType' are crea-
ted. After instancing, the instances can be used and can be activated.

Program representation:

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

23

The actual PLC program is entered and represented in this area of TwinCAT PLC control.

Select destination system


TwinCAT provides up to 4 run time systems. Each of these run time systems is capable of executing PLC pro-
grams in IEC 61131-3 independently of the other run time systems. To use the 'Select run time system...' com-
mand in the 'Online' menu to define which run time system you wish to run your programs on.

After installation of TwinCAT, only one run time system is released and so only the first run time system (Run
Time 1) is displayed in this dialog. Select OK to confirm the selection.

Logging in:

You have now loaded the PLC program in TwinCAT PLC control and you are able to execute it. Make sure the
TwinCAT real time server is active. This is recognizable by the fact that the TwinCAT real time server icon is
displayed in green on the bottom right of the screen. Be-fore you start a PLC program, you must link TwinCAT
PLC control to the run time system, i.e. you must 'log in' with the control system. Execute the 'Log in' command
in the 'Online' menu. As there is still no PLC program in the run time system, you get the message: No program
on the controller! Rebuild All? Acknowledge the question with ' OK'.

The current status of the connection is displayed in the status line:

Starting PLC program:

You start the PLC program in the TwinCAT real time server by selecting the 'Start' command from the 'Online'
menu. The word "RUN" displayed in the status line darkens. You should also see that individual steps within the
sequential function chart are temporarily displayed in blue.
A step shown in blue is currently being executed, i.e. it is the active step.

Tracing the PLC program sequence:


When you activate the 'Global variables' window by clicking the Re-sources Tab at the bottom of the 'Object
List' window, and then double clicking the 'Global Variables' object, you will see all globally declared variables.
Global variables can be used jointly by all program objects (POU).

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

24

Besides the variables, the 'timerUp', 'timerDown' and 'devTimer' function blocks are also displayed there. A
lozenge is visible before the function names. When you double click the lozenge, a tree-like display opens in
which all variables of the function are displayed.

Changing the numeric representation


You can display the contents of the variables in various numeric systems. You have a choice between decimal,
hexadecimal and binary. If you wish to change the display, you must select the 'Options' command in the 'Pro-
ject' menu. The current setting is indicated by a tick mark before the corresponding entry.

Ending the program:

You have now loaded a PLC program into TwinCAT PLC control (IEC 61131-3 programming environment) and
you have executed it on the TwinCAT PLC server (run time system). Now end the PLC program. To do this,
select the 'Stop' command in the 'Online' menu.

Logging out:

In the next sections, we will add to the PLC program. To do this, you must log out of the TwinCAT PLC server.
To do this, execute the 'Log out' command in the 'Online' menu.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

25

Viewing program text:


This example was programmed in the various IEC 61131-3 programming languages. The main part of the pro-
gram was created in the sequential function chart (SFC). It contains the steps
· Init
· Engine
· Drill
And the transitions:
· TRUE
· End
· TimerUp.Q

Viewing transitions:
The 'TRUE' transition is constantly fulfilled because the 'TRUE' key word is a system-wide constant and is per-
manently fulfilled. The 'En-gine' step is executed unconditionally after the 'INIT' step. TimerUp.Q means that the
variable Q in the Up function must be TRUE (or also 1) for this transition to be fulfilled. 'End' is a transition that
contains further program text. When you double click the transition, a further window opens in which the cor-
responding program text is displayed.

In the 'End' transition, a comparison is made as to whether the 25 steps of the motor have already been rea-
ched. If this is the case, the program changes in the next cycle from the 'engine' step to the 'drill' step.

If a step or a transition contains further program text, this is indicated by a small black triangle.

Modifying the PLC program


Switch back to the 'MAIN' window. At this point, you should modify the PLC program in such a way that the
motor's cycle speed can be modified in two stages (fast/slow) by way of a variable. Open the 'Drive Type' func-
tion block in the object list by double clicking it. Move the input cursor to the first line and enter the following
text:

IF switch = TRUE THEN


When the Enter (Return) key is pressed, a dialog box appears which you must fill out as shown below.

When you select 'OK', the 'switch' variable is added to the variable list of MAIN. If this dialog box not opens,
your auto declare is deactivated. (You can activate it by selecting autodeclaration under 'Options' 'Editor' in the
menu 'Project'. ) Open 'Auto declare' in the menu 'Edit'.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

26

Then also enter the following program lines:

devSpeed := T#10ms;
ELSE
devSpeed := T#25ms;
END_IF
The window must then have the following contents:

If the 'switch' variable is set, the 'devSpeed' variable is set to 25 ms, or otherwise to 10 ms. The result of this is
that the pulse and pause duration of the clock pulse generator in the following program lines either amount to
25 ms or 10 ms.

Saving the program:

Save the program by selecting the 'Save ' command in the 'File' menu.

Compiling the program:


Before a program can be transferred to the TwinCAT PLC server, it must be compiled, i.e. it must be converted
from its textual or graphical representations to a form that is understandable to the control system. To do this,
select the 'Rebuild all' command from the 'Project' menu item.

Starting the program:


Log in with the control system and start the PLC program. You see that the 'switch' variable is set to 'FALSE'
when starting.

Modifying variable values:


You have the possibility of modifying values of variables while the PLC program is running. Open the 'Global
variables' window and double click the 'switch' entry. The display changes from FALSE to TRUE and the lette-
ring turns red. Up to this time, however, the value in the TwinCAT PLC server has not yet changed. To do this,
you must execute the 'Write values' command in the 'Online' menu item. The lettering turns black again and the
'devSpeed' variable changes to 10 ms.

Tracing the program sequence:


Follow the program flow with TwinCAT Scope View.

You will find more information about TwinCAT PLC Control in the documentation on TwinCAT PLC Control in
the TwinCAT Information System.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

27

Following Program Flow


TwinCAT Scope View is used for recording and analysis of the program.

Open TwinCAT Scope View:


Scope View can only be opened through the start menu.
Use the mouse to select 'Start' -> 'Programs' -> 'TwinCAT System' -> 'TwinCAT Scope View'.

The elements of TwinCAT Scope View:


The window of TwinCAT Scope View is similar to that of TwinCAT PLC Control. In the first line is the name of
the project, among them is the commend line and the toolbar.
The three large windows are empty. In the left window you can configure the Scope.

Start TwinCAT Scope View:


You must first add a Scope View, or in other words a project, in order to be able to start the example program
Maschine.pro. To do this, click with the right mouse button on Scope, choose 'Add Scope View' there, and con-
firm with 'OK'.

Insert channel:
In order to display the individual signals it is necessary to create the associated channels.
To do this, click with the right mouse button on 'Scope View 1', choose 'Add channel', and confirm with 'OK'.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

28

The following tabs then appear, with which the variables whose values are to be recorded are described.

A clear display is obtained by setting the user-defined sampling time to 10 ms.

Set up server port:


The next step is to set up the server port. Under Acquisition click on 'Change', enter the number, and confirm
with 'OK'. (The server port can be found from PLC Control, see under Selecting the target system.)

Assign channel:
Assign the signal .ENGINE to Channel 1 by clicking on 'Change' again, and confirm this action with 'OK'.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

29

Settings
The tab then shows the following settings:

Renaming a channel:
A slow double click on 'Channel1' allows it to be renamed to ENGINE.

Add more channels:


You can assign the other channels in the same way.
Channel2=.DEVICEUP
Channel3=.DEVICEDOWN
Channel4=.STEPS
The server port and sampling time remain the same for all the channels.

When you have created and renamed the four channels, save the Scope View via the 'File' menu, selecting
'Save as' and giving Maschine.scp as the name.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

30

In order to be able to distinguish the various curves from one another, each channel can be given a different
colour, form or axis. This is done with the aid of the Style or Display tabs.

Start recording:

Recording is started by selecting 'Start Scope' from the 'Scope' menu.

Maschine.pro
The sample program then looks like this:

You will find more information about TwinCAT Scope View in the documentation on TwinCAT Scope View in
the TwinCAT Information System.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

31

7. Conversion of the sample program


Declaration of the variables
In this chapter, you will connect the previous PLC program to the Beckhoff bus terminals. This happens with the
TwinCAT System Manager. By means of the System Manager, all input/output interface connections are mana-
ged and addressed and are assigned to the I/O data. Each I/O channel can be addressed by a logical name.
The TwinCAT System Manager manages several field buses on one common process image

Note:
It´s neccessary to have the required hardware. The Demokit for the Lightbus contains the PC interface card for
the II/O-Lightbus (FC2001), the bus Coupler BK 2000, Bus Terminals, 2 fibre optic and some documentation.
Without hardware go to the next chapter Visual Basic Link .

Variable declaration:
The storage locations (addresses) of variables are managed internally by the system. The programmer does
not need to bother about memory management. The PLC programs operate with symbolic variable names, thus
preventing the occurrence of side effects (overlaps) when using variables. To access the input/output level, it is
necessary for the programmer to be able to assign a fixed address to individual variables. This is achieved by
means of the key word 'AT', which must always be specified when declaring a variable. The key word 'AT' is
followed by several parameters which provide information about the data location (input/output or flag area) and
the width of the data (BIT, BYTE, WORD or DWORD). The variable declaration for the above example has the
following structure:

VAR_GLOBAL
engine AT %QX0.0: BOOL;
deviceUp AT %QX0.1: BOOL;
deviceDown AT %QX0.2: BOOL;
timerUp: TON;
timerDown: TON;
steps: BYTE;
count: UINT := 0;
devSpeed: TIME := t#10ms;
devTimer: TP;
switch: BOOL;
END_VAR

Where:

Data location Datawidth Meaning


% Start of I/O definition
I Input
Q Output
M Flag
X Bit (1bit)
B Byte (8 bits)
W Word (16 bits)
D Double word (32 Bit)
The digits after the data width specify the address of the variable. For bit variables, the address must be speci-
fied in the format x.y, or simply x for byte, word and double word. The input and outputsare in different Storage
areas , therefor may contain the same address.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

32

Setting up the Bus Terminals


Hardware Requirements:
- PC Interface card: FC2001
- Bus coupler: BK2000
- 3 digital outputs, 24V: 2x KL2032
- Bus end terminal: KL9010

Note:
This example is exactly appropriate for the contents of the Beckhoff Lightbus demokit (TC9910-B200-0100).
The hardware is however exchangeable. In this case, the configuration of the I/O devices has to be changed

Setting up the terminals


Set up the bus coupler and the bus terminals as shown in the drawing below:

Connect the bus coupler to the PC interface card and power the bus coupler with 24 V DC.

Hardware Documentation
Further information about the hardware connection contains the hardware documentation of the demokit.

Starting the TwinCAT real time server:


Now, start the TwinCAT real time server, unless you have already done so, so that the TwinCAT message rou-
ter is active.

Starting the TwinCAT System Manager


Once the system has been started, the color of the icon changes from red to green. Now start the TwinCAT
System Manager by selecting 'Start'-> 'Programs' -> 'TwinCAT System' -> 'TwinCAT System Manager'.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

33

The items of the TwinCAT System Manager:


In the first line is the name of the project, (here 'unbenannt'), amount there´s the command line (menu) and the
toolbar. In the last line you can see the status of the system, in this picture, the system is running (RTime). The
two windows in the middle contains the configuration of the system. In the following steps you´ll configure the
system.

The system configuration is shown as a tree structure on the left side of the System Manager. It consists of the
following four main points:

Configuration Meaning
Realtime Set the Real Time Parameters
PLC All PLC Projects that are required to be configured
Cam Add the cam server
I/O In order to link the controller to the process level, the system needs interfaces. This entry
offers a list of all interfaces.

TwinCAT Information System:


Further information you can find in the documentation TwinCAT System Manager.

PLC Configuration:
The individual PLC projects must be made known to the System Manager so that TwinCAT can access the
variables of the PLC programs. To do this, press the right mouse button while the mouse pointer is over 'PLC
configuration'

A context menu opens, in which you must select the 'Append IEC project…' entry.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

34

Switch to the '\TwinCAT\Samples\FirstSteps\' directory and select the 'maschine.pro' file.

A further point has been added under 'PLC configuration' which bears the name of the PLC project.

The + and the - symbols indicate whether the entry contains a further subpoints. By clicking these symbols, you
open or close the entries below them. The following structure appears if you open the tree as far as possible:

I/O Configuration:
Once the PLC project has been added to the PLC configuration, and thus all variables that are in the process
image are known, it is necessary to specify the I/O configuration. Select the 'I/O devices' entry with the right
mouse button. A context menu opens, in which you must select the 'Add device' entry.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

35

Selecting an I/O device:


The following window opens. Select the device type, in this case 'I/O lightbus FC200x, PCI '. You can choose
the device name freely.

On the right hand side, a dialog box now opens in which you can specify the configuration of the interface card.
One important setting under the 'FC2001' slider, for example, is the I/O address of the lightbus card. You can
use the specified entries if you have not made any changes to the default settings of the card.

Adding a bus coupler:


Open the context menu of the FC2001 card (device 1) and select the ‘Add box…’ command.

Selecting a bus coupler:


Select the field bus module type, in this case 'BK2000'. You can choose the field bus module name freely.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

36

Adding a bus terminal:


Open the context menu (press the right mouse button) of the BK2000 (box 1) and select the 'Add terminal…'
command.

Selecting a bus terminal:


Select the terminal that bears the designation KL2114. You can open and close the terminal selection with a
mouse click on the+ and - sign.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

37

You can choose the bus terminal name freely.


The sample program requires 3 digital outputs. To insert the second bus terminal, repeat the steps.
The Bus end Terminal KL9010 is inserted automatically by the System Manager.

End of configuration:
The configuration then has the following breakdown:

It goes without saying that you can rename the standard designations (device 1, box 1, terminal 1 etc.) To do
this, slowly double click the corresponding name and enter the new designation.

Assigning variables to the input/output channels:


Up to this point, the complete hardware needed for the above example program has been configured. Next, the
individual variables from the PLC project must be assigned to the individual input/output channels.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

38

To do this, mark the terminal you wish to configure. In the case of terminal 1 (four digital outputs), a dialog box
containing the 'General' and 'Variables' sliders opens on the right. Select the 'Variables' tab.

Obove you see a list of the two output channels, but they are still all free. To configure channel 1, select the
corresponding button ('channel 1…'). The following dialog box opens:

All output variables are now listed in this dialog box. Select the first variable (engine) and confirm your entry by
clicking 'OK'. Proceed analogously with the second output variable.
The first bus terminal is attached:

Proceed analogously with the other bus terminal.

Assigning variables:
Terminal 2 PLC variables Meaning
Channel1(=output 1) engine Stepper motor control
Channel2 (=output 2) device.Up Drill up control

Terminal 3 PLC variables Meaning


Channel1 (=output 3) device.Down Drill down control
Channel2 (=output 4) - free channel

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

39

Assignments of the bus terminals:

Saving the project:

You should save the configuration at this point to make sure you can access it later on. To do this, run the 'Sa-
ve as…' command from the 'File' menu.

Mapping Variables:

You have now configured the complete system for the above example pro-gram. You must now create the allo-
cation. To do this, go to the 'Create allocation' command in the 'Actions' menu. Under the 'Allocations' tree
entry, you now see 'Standard device 1(FC2001)'. Click this entry. The following window opens:

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

40

In the dialog box, you can define whether the data flow from A to B or from B to A is to be displayed. In this
case, Image A corresponds to the process image of the PLC variables, i.e. the input/output variables. Image B
corresponds to the process image of the I/O devices, in this case of the bus coupler BK2000. Each variable or
bus terminal is color highlighted in the process image. If you stop on one of these areas with the mouse, a small
display box appears in which the precise designation is shown.

Writing the configuration to the registry:

As the last step, you must save the configuration to Windows NT registry because the information stored there
is evaluated when you start Twin-CAT. Run the 'Save in registry…' command from the 'Actions' menu. If an
older configuration is already stored there, a safety prompt will appear, which you must confirm.

Restarting TwinCAT:
You must restart the system so that TwinCAT will accept the change.

The individual PLC variables are now output on the bus terminal KL 2032. The Bus Terminals indicate their
signal state by means of light emitting diodes.

You will find more information about TwinCAT System Manager in the documentation on TwinCAT System
Manager in the TwinCAT Information System.

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

41

8. Visual Basic Link


Programming interface ADS OCX
TwinCAT has several programming interfaces in order to integrate applica-tion-specific programs in the system.
Visual Basic from Microsoft is one programming language that is sup-ported. The strength of this programming
language is the fact that graphi-cal user interfaces are created and it is possible to link them to databases. Vi-
sual Basic has been widely spread for some years now and is constantly enhanced by Microsoft. Many third-
party vendors offer add-on modules for various areas. The generic term for such modules is OCX.Beckhoff
supplies an OCX for TwinCAT that bears the designation Ad-sOCX. AdsOCX provides methods for communica-
ting via the TwinCAT message router with other ADS devices (PLC, NC/CNC, ...).The included example pro-
gram shows how to access individual variables of the TwinCAT PLC server from Visual Basic.

Access methods:
AdsOCX contains various methods for reading the values out of other ADS devices. The decision as to which
method to use depends on the environment in which the program is to be run. You will find further notes and
detailed references to the individual functions in the special AdsOCX instructions. Only a brief overview of the
individual access methods will be given here:

Access Meaning
connect As soon as communication between a PLC variable and a Visual Basic variable is needed, a
connection between these two variables is established by activation of a method. During the
further course of the program, TwinCAT adapts the Visual Basic variable to the PLC variable.
This type of data exchange can also be used so as to activate an event function (event-
controlled data transfer) in the event of a change in a PLC variable in the Visual Basic pro-
gram.
synchronous After activation of a read/write method, execution of the Visual Basic program is interrupted
until the requested data has arrived. The Program can then continue working with the new
data.
asynchronous When using asynchronous access, execution of the Visual Basic program is not interrupted
and instead, execution of the next command is continued automatically. Once the requested
data arrives in AdsOCX, an event function is triggered in the Visual Basic program in which
the value is passed on as the parameter

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

42

Sample program
Starting TwinCAT and the PLC program:
TwinCAT and the PLC program must be active before you can start the Visual Basic program.

Starting the Visual Basic program:


Start the 'maschine.exe' program. (TwinCAT\ Samples\First Steps\)

In the left area, you see the two outputs, which are also output to the bus terminals. The variable that counts
the workpieces is shown on the bottom left. You can modify the cycle speed of the motor in the 'speed' box. The
'position' display corresponds to the number of cycles that are output to the output 1.

Programtext:
Option Explicit

DimdeviceUp As Boolean
Dim deviceDown As Boolean
Dim steps As Integer
Dim counter As Long
Dim hDeviceUp As Long
Dim hDeviceDown As Long
Dim hSteps As Long
Dim hSwitch As Long
Dim hCounter As Long
'-----------------------------------------------------
'Is activated first when the program is started
'-----------------------------------------------------
Private Sub Form_Load()
'Sprachabhängige Wörter aus Resource-Datei laden
'load language dependent words from the resource-file
lblMachine.Caption =

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

43

LoadResString(0 + GetLanguageId)
lplDeviceUp.Caption = LoadResString(1 +GetLanguageId)
lplDeviceDown.Caption = LoadResString(2 + GetLanguageId)
lblCountLabel.Caption = LoadResString(3 + GetLanguageId)
lplSteps.Caption =LoadResString(4 + GetLanguageId)
fraSpeed.Caption = LoadResString(5 +GetLanguageId)
optSpeedFast.Caption = LoadResString(6 + GetLanguageId)
optSpeedSlow.Caption = LoadResString(7 + GetLanguageId)
'Connect PLC variables with VB variables
Call AdsOcx1.AdsReadIntegerVarConnect(".steps", 2&, 4, 55, steps)
Call AdsOcx1.AdsReadBoolVarConnect(".deviceUp", 2&, 4, 55, deviceUp)
Call AdsOcx1.AdsReadBoolVarConnect(".deviceDown", 2&, 4, 55, deviceDown)
Call AdsOcx1.AdsReadLongVarConnect(".count", 4&, 4, 55, counter)
'Determine handle of the variables
Call AdsOcx1.AdsCreateVarHandle(".steps", hSteps)
Call AdsOcx1.AdsCreateVarHandle(".deviceUp", hDeviceUp)
Call AdsOcx1.AdsCreateVarHandle(".deviceDown", hDeviceDown)
Call AdsOcx1.AdsCreateVarHandle(".count", hCounter)
Call AdsOcx1.AdsCreateVarHandle(".switch", hSwitch)
End Sub

'------------------------------------------
'is activated when ending the program
'------------------------------------------
Private Sub Form_Unload(Cancel As Integer)
'Visual Basic-Variablen von SPS-Variablen trennen
'Separate Visual Basic variables from PLC variables
Call AdsOcx1.AdsReadIntegerDisconnect(steps)
Call AdsOcx1.AdsReadBoolDisconnect(deviceUp)
Call AdsOcx1.AdsReadBoolDisconnect(deviceDown)
Call AdsOcx1.AdsReadLongDisconnect(counter)
'Release handle of the variables
Call AdsOcx1.AdsDeleteVarHandle(hSteps)
Call AdsOcx1.AdsDeleteVarHandle(hDeviceUp)
Call AdsOcx1.AdsDeleteVarHandle(hDeviceDown)
Call AdsOcx1.AdsDeleteVarHandle(hCounter)
Call AdsOcx1.AdsDeleteVarHandle(hSwitch)
End Sub
'------------------------------------------------------
'is activated when the 'fast' field is marked
'------------------------------------------------------

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

44

Private Sub optSpeedFast_Click()


Dim switch As Boolean
'set PLC variable switch to TRUE
switch = True
Call AdsOcx1.AdsSyncWriteBoolVarReq(hSwitch, 2&, switch)
End Sub
'------------------------------------------------------
'is activated when the 'slow' field is marked
'------------------------------------------------------
Private Sub optSpeedSlow_Click()
Dim switch As Boolean
'set PLC variable switch to FALSE
switch = False
Call AdsOcx1.AdsSyncWriteBoolVarReq(hSwitch, 2&, switch)
End Sub
'------------------------------------------------
'is activated when a PLC variable changes
'------------------------------------------------
Private Sub AdsOcx1_AdsReadConnectUpdate(ByVal nIndexGroup As Long, ByVal nIndexOff-
set As Long)
Select Case nIndexOffset
Case hCounter:
'Display quantity in form
lblCount.Caption = counter
Case hDeviceUp
'Adapt colors of graphics according to the variables
DeviceUp_LED.ForeColor = IIf(deviceUp = True, vbRed, vbWhite)
Case hDeviceDown
'Adapt colors of graphics according to the variables
DeviceDown_LED.ForeColor = IIf(deviceDown = True, vbRed, vbWhite)
Case hSteps
'Display position of workpiece
prgSteps.Width = steps * 240
End Select
End Sub

Operating principle:
In Form1_Load() the language dependent text are loaded as resources.The Text will displayed according to the
preselected language in Windows NT. The Method ‚AdsReadVarConnect' connects the appropiate VB variables
to the PLC System. The parameter of the methods are:

Parameter Meaning
adsVarName Name of the PLC variable
cbLenght Length of the data in bytes

TWINCAT
Eiserstraße 5 / D-33415 Verl / Telefon 05246/963-0 / Telefax 05246/963-149

45

nRefreshType Type of data transfer


nCycleTime Refresh cycle in ms
pData Visual Basic variable in which the data is written
The AdsOcx1_AdsReadConnectUpdate() function is activated whenever the variable 'count', 'deviceUp', 'devi-
ceDown' or 'steps' has changed. In it, the objects are animated on the form with the variables of the PLC.
The optFast_Click() and optSlow_Click() functions are activated whenever the user clicks the 'fast' or 'slow'
marking box. In these functions, the 'switch' variable is set to TRUE or FALSE. The TwinCAT PLC server reads
out this variable and appropriately modifies the behavior of the flashing ('DriveType' function block in the PLC
program).

Parameter Meaning
hVar Handle of the PLC variable
cbLenght Length of the data in bytes
pData Visual Basic variable, which contains the data
When a form is closed, all variables that are no longer needed should be separately from the PLC variables.
This is done in the Form1_Unload() function.

TWINCAT

You might also like