CIF 3

TwinCAT PLC output

TwinCAT is a complete IDE for the development and testing of PLC controllers. Using the TwinCAT PLC output type, the generated PLC code can be written to the native file formats of the TwinCAT IDE. This allows for more tight integration and smoother development cycles than using the PLCopen XML output type.

We currently only provide official support for version 3.1 of TwinCAT.

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:

  • img-mode-inactive Inactive Mode
  • img-mode-stop Stop Mode
  • img-mode-config Config Mode
  • img-mode-run Run Mode
  • img-mode-ex 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:

  • img-mode-stop Stop Mode
  • img-mode-config Config Mode
  • img-mode-run Run Mode
  • img-mode-run / img-mode-free-run Free Run Mode (alternating icons)
  • img-mode-ex 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 System ‣ Config.

To change to Config Mode or restart Config Mode from Visual Studio:

  • Click TwinCAT ‣ Restart TwinCAT (Config Mode). Alternatively, click the appropriate toolbar item (img-restart-config).
  • 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 System ‣ Start/Restart.

To change to Run Mode or restart Run Mode from Visual Studio:

  • Make sure the hardware is powered up.
  • Click TwinCAT ‣ Restart TwinCAT System. Alternatively, click the appropriate toolbar item (img-restart-run).
  • 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 3 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. 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.

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 File ‣ New ‣ Project....
    • 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. For the 4K420 course, make sure to use the name used by the script you will be using to generate the PLC code, e.g. #_plc as directory name, with # replaced by the name of your station. For instance, for the input station, the project should be named input_station_plc.
    • 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 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 or Package Explorer, 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 (img-scan-devices).
    • 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 File ‣ Save All.
  • Generate PLC code from Eclipse, by executing the appropriate script. For the 4K420 course, use the 3_gen_plc_code.tooldef2 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 to FINISHED).
  • 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 Build ‣ Rebuild Solution.
  • 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 TwinCAT ‣ Activate Configuration. Alternatively, click the appropriate toolbar item (img-activate-cfg).
    • 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 PLC ‣ Login. Alternatively, click the appropriate toolbar item (img-login).
    • 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 PLC ‣ Start. Alternatively, click the appropriate toolbar item (img-start).
  • The PLC is now running, and you can use the system.

To stop the PLC:

  • Click PLC ‣ Stop. Alternatively, click the appropriate toolbar item (img-stop).
  • Click PLC ‣ Logout. Alternatively, click the appropriate toolbar item (img-logout). 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 TwinCAT ‣ Activate Configuration. Alternatively, click the appropriate toolbar item (img-activate-cfg).
    • 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 PLC ‣ Login. Alternatively, click the appropriate toolbar item (img-login).
    • 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 PLC ‣ Start. Alternatively, click the appropriate toolbar item (img-start).
  • 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 File ‣ Save All.
  • Regenerate PLC code from Eclipse, by executing the appropriate script. For the 4K420 course, use the 3_gen_plc_code.tooldef2 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 to FINISHED).
  • 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 Build ‣ Clean Solution.
  • Rebuild the project by clicking Build ‣ Rebuild Solution.
  • 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 TwinCAT ‣ Activate Configuration. Alternatively, click the appropriate toolbar item (img-activate-cfg).
    • 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 PLC ‣ Login. Alternatively, click the appropriate toolbar item (img-login).
    • 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 PLC ‣ Start. Alternatively, click the appropriate toolbar item (img-start).
  • 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 Tools ‣ Options... to show the Options dialog. On the left, select Environment ‣ Documents. 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 Tools ‣ Options... to show the Options dialog. On the left, select TwinCAT ‣ PLC Environment ‣ Text Editor. 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. If you are following the supervisory controller development toolchain, read the Using multiple plants/requirements files section.
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.