TwinCAT PLC output
TwinCAT is a complete IDE for the development and testing of PLC controllers. Using the TwinCAT PLC target type, the generated PLC code can be written to the native file formats of the TwinCAT IDE.
TwinCAT system modes
The TwinCAT software IDE is built on Microsoft Visual Studio. TwinCAT also supplies a utility to monitor and control the TwinCAT System Service, via an icon in the system tray, part in the Windows taskbar. The icon shows the mode (or status) of the TwinCAT system:
-
Inactive Mode
-
Stop Mode
-
Config Mode
-
Run Mode
-
Exception Mode
In the Inactive Mode, the TwinCAT System Service is not running. Restart Windows or manually activate the TwinCAT System Service and related services.
The Stop Mode is of little use, so switch to the Config Mode instead. In the Config Mode, the system (the PLC) can be configured. In the Run Mode, the system (the PLC) can be used to control the hardware. If a runtime exception occurs in Run Mode, the Exception Mode becomes active.
Within Visual Studio, the mode is also shown as an icon in the bottom right corner of the IDE:
-
Stop Mode
-
Config Mode
-
Run Mode
-
/ Free Run Mode (alternating icons)
-
Exception Mode
If the system is only equipped with a soft PLC, and the TwinCAT environment is set up to target the local system, both icons should indicate a consistent status. This page assumes this situation.
Changing modes
To change to Config Mode or restart Config Mode from the system tray:
-
Right click the system tray icon and choose
.
To change to Config Mode or restart Config Mode from Visual Studio:
-
Click
. Alternatively, click the appropriate toolbar item (). -
A dialog pops up with the question Restart TwinCAT System in Config Mode. Click OK or press Enter.
-
A dialog pops up with the question Load I/O Devices. Click Yes or press Enter.
-
A dialog pops up with the question Activate Free Run. Click Yes or press Enter.
To change to Run Mode or restart Run Mode from the system tray:
-
Make sure the hardware is powered up.
-
Right click the system tray icon and choose
.
To change to Run Mode or restart Run Mode from Visual Studio:
-
Make sure the hardware is powered up.
-
Click
. Alternatively, click the appropriate toolbar item (). -
A dialog pops up with the question Restart TwinCAT System in Run Mode. Click OK or press Enter.
In general it is recommended to switch modes via Visual Studio instead of via the system tray icon.
Generated output
The CIF PLC code generator can generate code into an existing TwinCAT Visual Studio solution, with a TwinCAT XAE project that contains a TwinCAT Standard PLC project. The directory that contains the solution must have the same name (except for the file extension) as the solution file (.sln
file). The TwinCAT XAE project (both the directory and the .tsproj
file) in the solution should have the same name as well. The TwinCAT XAE project should contain a TwinCAT Standard PLC project. This project may be given any name, and this name can be configured using the PLC project name option (Generator category). However, in order to be able to import I/O mappings it is highly recommended to always use the default name suggested by TwinCAT (Untitled1
) for TwinCAT Default PLC projects.
The project should already contain a task. This task can be given any name, and this name can be configured using the PLC task name option (Generator category). By default, TwinCAT Default PLC projects have a task named PlcTask
. This name is the default for the PLC task name option as well. The cycle time and priority of the task are automatically updated by the PLC code generator, depending on the values provided for the options of the generator. TwinCAT code generation does not support disabling periodic task scheduling. The code generator also ensures that the MAIN
program is instantiated/scheduled on the task.
All POU
(functions, programs, etc), DUT
(data types), and GVL
(global variable lists) code files that are present in the TwinCAT Default PLC project are removed, from both the project and from disk, by the PLC code generator. The newly generated code files are (over)written, and the project is updated to include and compile only the newly generated files.
The PLC configuration name and PLC resource name options have no effect on the TwinCAT output.
Standard compliance
In general, the CIF PLC code generator follows the IEC 61131-3 PLC standard and does not use vendor-specific extensions. For the TwinCAT target however, the STRUCT
data type declarations have no ;
character after the STRUCT
data types, to work around a bug in the TwinCAT parser. In that case the output is not compliant with the IEC 61131-3 standard.
TwinCAT usage
Follow these steps to create a solution and project in TwinCAT, generate PLC code in it, and use the PLC code to control the hardware:
-
Start TwinCAT (Visual Studio) by right clicking on the TwinCAT system tray icon and choosing TwinCAT XAE (VS 2010).
-
Create a new TwinCAT XAE project:
-
Click
. -
In the New Project window, TwinCAT XAE Project (XML format) should already be selected. If not, first select TwinCAT Project on the left (from the list of Installed Templates), and then select TwinCAT XAE Project (XML format).
-
Choose a project name and enter it in the Name box at the bottom of the dialog.
-
Make sure the Solution name is exactly the same, and that the Create directory for solution option is enabled.
-
For Location, select the directory in which the directory for the solution should be created. This is usually the directory that contains the script and/or source files you will be using to generate the PLC code.
-
To figure out the directory that contains the ToolDef script, make sure that the Properties view is visible in Eclipse. Select the ToolDef script in the Project Explorer tab or Package Explorer tab, and look for the Location property.
-
-
Click OK to create the project.
-
-
Scan the available hardware:
-
Make sure the hardware is powered up.
-
Make sure the Config Mode is enabled, by switching to it if it is not the current mode.
-
In the project, under I/O, right click the Devices item and choose Scan. Alternatively, select the I/O item and click the appropriate toolbar item ().
-
A dialog pops up with the warning HINT: Not all types of devices can be found automatically. Click OK or press Enter.
-
A dialog pops up where you have to choose the I/O device to scan. Normally, if you have only one I/O device, it should already be selected. Click OK or press Enter.
-
A dialog pops up with the question Scan for boxes. Click Yes or press Enter.
-
A dialog pops up with the question Activate Free Run. Click Yes or press Enter.
-
-
Create a new TwinCAT PLC project:
-
Right click the PLC item in the project, and choose Add New Item….
-
In the Add New Item dialog, Standard PLC project should already be selected.
-
Keep the name
Untitled1
in the Name box at the bottom of the dialog. -
Also don’t change the Location of the project.
-
Click Add to add the PLC project.
-
-
Save the entire solution by clicking
. -
Generate PLC code. If using a script, wait for the execution of the script to finish (the status indicated as part of the title of the console should change from
running
toFINISHED
). -
Switch back to TwinCAT (Microsoft Visual Studio).
-
When a File Modification Detected window pops up for the project, click Reload to reload the updated project that was written by the CIF tooling.
-
Build the project by clicking
. -
Load the I/O mappings:
-
In the project, under I/O, right click Mappings, and choose Import Mapping Infos….
-
Select the mappings file for your hardware, and click Open.
-
If the import succeeds, you should see an Untitled1 Instance - … item under the Mappings item.
-
-
Activate the configuration:
-
Click
. Alternatively, click the appropriate toolbar item (). -
A dialog pops up with the question Activate Configuration (Old Configurations will be overwritten!). Click OK or press Enter.
-
A dialog pops up with the question Restart TwinCAT System in Run Mode. Click OK or press Enter.
-
-
Start the PLC:
-
Click
. Alternatively, click the appropriate toolbar item (). -
A dialog pops up with the question Application Port_851 does not exist on the device. Do you want to create it and proceed with download?. Click Yes or press Enter.
-
Click
. Alternatively, click the appropriate toolbar item ().
-
-
The PLC is now running, and you can use the system.
To stop the PLC:
-
Click
. Alternatively, click the appropriate toolbar item (). -
Click
. Alternatively, click the appropriate toolbar item (). This step is optional, and can usually be skipped.
To reset the PLC and start the 'simulation' from the beginning, with the same code and the same configuration:
-
Make sure the hardware is powered up.
-
Load the I/O mappings, if the Untitled1 Instance - … item under the Mappings item is no longer present:
-
In the project, under I/O, right click Mappings, and choose Import Mapping Infos….
-
Select the mappings file for your hardware, and click Open.
-
If the import succeeds, you should see an Untitled1 Instance - … item under the Mappings item.
-
-
Reactivate the configuration:
-
Click
. Alternatively, click the appropriate toolbar item (). -
A dialog pops up with the question Activate Configuration (Old Configurations will be overwritten!). Click OK or press Enter.
-
A dialog pops up with the question Restart TwinCAT System in Run Mode. Click OK or press Enter.
-
-
Start the PLC:
-
Click
. Alternatively, click the appropriate toolbar item (). -
A dialog pops up with the question Application Port_851 does not exist on the device. Do you want to create it and proceed with download?. Click Yes or press Enter.
-
Click
. Alternatively, click the appropriate toolbar item ().
-
-
The PLC is now running, and you can use the system.
Whenever you change a CIF source file, regenerate a supervisor, change a hardware mapping, etc, you need to regenerate the PLC code as well. Follow these steps:
-
Make sure the Config Mode is enabled, by switching to it if it is not the current mode.
-
Save the entire solution in TwinCAT by clicking
. -
Regenerate PLC code from Eclipse. If using a script, wait for the execution of the script to finish (the status indicated as part of the title of the console should change from
running
toFINISHED
). -
Switch back to TwinCAT (Microsoft Visual Studio).
-
For all File has been changed outside the environment. Reload the new file? questions, click Yes or press Enter. See the Frequently Asked Questions (FAQ) for instructions on how to disable some of these questions.
-
When a File Modification Detected window pops up for the project, click Reload to reload the updated project that was written by the CIF tooling.
-
Clean the project by clicking
. -
Rebuild the project by clicking
. -
Make sure the hardware is powered up.
-
Reload the I/O mappings:
-
In the project, under I/O, right click Mappings, and choose Import Mapping Infos….
-
Select the mappings file for your hardware, and click Open.
-
If the import succeeds, you should see an Untitled1 Instance - … item under the Mappings item.
-
-
Activate the configuration:
-
Click
. Alternatively, click the appropriate toolbar item (). -
A dialog pops up with the question Activate Configuration (Old Configurations will be overwritten!). Click OK or press Enter.
-
A dialog pops up with the question Restart TwinCAT System in Run Mode. Click OK or press Enter.
-
-
Start the PLC:
-
Click
. Alternatively, click the appropriate toolbar item (). -
A dialog pops up with the question Application Port_851 does not exist on the device. Do you want to create it and proceed with download?. Click Yes or press Enter.
-
Click
. Alternatively, click the appropriate toolbar item ().
-
-
The PLC is now running, and you can use the system.
Frequently Asked Questions (FAQ)
-
Question: Is there something that can be done about the large number of File has been changed outside the environment… prompts?
Answer: Yes. Click
to show the Options dialog. On the left, select . Then, on the right, enable the Detect when file is changed outside the environment and Auto-load changes, if saved options. -
Question: Why doesn’t importing the I/O mappings have any effect?
Answer: Make sure the TwinCAT Standard PLC project is named
Untitled1
, and try again. Also, make sure you’ve built the solution before you try to import the I/O mappings. -
Question: I get a dialog with the warning Device '…' needs a sync master (at least one variable linked to a task variable). What should I do?
Answer: You probably forgot to load an I/O mapping.
-
Question: Why does opening the MAIN program take so long in Run Mode?
Answer: As the generated code becomes larger, it can take a long time for TwinCAT to set up the environment for inline monitoring of the MAIN program. To disable inline monitoring, click
to show the Options dialog. On the left, select . Then, on the right, switch to the Monitoring tab. Disable the Enable inline monitoring option. -
Question: TwinCAT hangs for long periods of time, before crashing and automatically being restarted by Windows. What should I do?
Answer: If the generated code is too large, TwinCAT may not be able to handle it. In such cases, reduce the size of the generated code, by reducing the size of the CIF model for which you are generating the code.
-
Question: I get an error stating Reading file failed. […]. What should I do?
Answer: The generated code is too large for TwinCAT to handle. See also the answer to the previous question.