TwinCAT & virtualization

Most people who have developed software have at some point or another used virtualization technology. Software development for PLCs in a virtual environment is often overlooked, since PLC development is so close to the hardware. Nevertheless, there are still advantages. Working for several projects with various requirements, but where a Beckhoff PLC/TwinCAT was the common delimiter, made me ask myself “How much use of virtualization can I do for TwinCAT software development?”

There can be many reasons for wanting to use a virtual machine while doing TwinCAT development, a few of them being:

  • Doing a temporary test of a certain functionality without messing up the main PC
  • Have different toolchains for development
  • Developing software for different operating systems
  • General prototyping

During a project a requirement came up in where I needed to develop some software that was supposed to run on a Linux machine, but communicating to a TwinCAT runtime.  What I needed was to run a client on a Linux machine, talking to a TwinCAT3 PLC through the ADS protocol. At this point I had no access to any physical PLC, which meant I needed to run the TwinCAT code on my Windows 10 developer machine. I used VirtualBox, which is a free open-source hypervisor to virtualize x86 computers. For Linux, I had a virtual instance of it in VirtualBox. The problem is, if you try to run a virtual machine and at the same time try to put the TwinCAT3 runtime in “RUN”-mode, you get a warning:

TwinCAT & VirtualBox warning

TwinCAT might fail if I press Continue? Let’s try anyway…

…and my computer crashed. Something needed to be done. The above suggests two solutions:

  • Don’t run any VirtualBox machines when TwinCAT is in run mode at the host machine
  • Active a solution using only isolated cores

I didn’t like the first suggestion, which is just another way of saying “just give up”. The other suggestion was however good. This warning message suggests that the problem occurs when you’re trying to run a TwinCAT task on a CPU-core that is also used on the virtual machine. If we could make sure to remove one (or more) CPU-cores from the host operating system, thus allowing only TwinCAT to have access to it. If the host OS did not have access to the CPU core(s), then neither would any virtual machines running in the host have it and all should be fine. Theory is one thing, time to do some experiments.

My development machine is running a Intel Core i7-8550U CPU, and according to the datasheet it has four physical CPU cores (eight running with Hyper-Threading). Doing a “Read from Target” in TwinCAT3 on my machine confirmed this:

“Read from Target” on developer machine

Now the trick here is that with the current configuration, all these logical CPUs are accessible for both TwinCAT and Windows. Because they are accesible to both Windows and TwinCAT they need to be shared (through some magic done in TwinCAT). What we need to make sure is that TwinCAT has 100% access to at least one CPU-core. Now I should mention that TwinCAT generally is not too fond of having hyperthreading enabled, so I would suggest to disable it (possible in the BIOS/UEFI of most computers), though I for this example will not do so. If hyperthreading would be disabled the above would show just four rows, one for each physical core. Change the above settings so that we have 1 isolated core and 7 cores for Windows like this:

Isolated core in TwinCAT

After doing this, TwinCAT is going to ask you to reboot the machine. After rebooting you might need to run the batch-file C:\TwinCAT\3.1\System\win8settick.bat in a command prompt (as administrator), and reboot again. After the reboot is done, you can see that Windows only “sees” seven of the eight logical CPUs:

Seven cores (sorry for the Swedish language)

Now, enabling configuration in TwinCAT3 and starting a Linux machine (specifically CentOS 7.5), it works!

TwinCAT & Virtual Linux

So apparently it is possible to run TwinCAT on a hyperthreading CPU. This is great stuff! Even better, thanks to the bridged networking functionality in VirtualBox, I can have the Linux machine do network communications with the TwinCAT runtime on the Windows host. For instance, it’s possible to compile libraries for Linux by using Beckhoffs ADS protocol. Now TwinCAT (The WINdows Control and Automation Technology) got a little bit flavoured with the sweet taste of Linux.

All of this is amazing, but I want more. Is it possible to run a virtual Windows machine running TwinCAT3? Probably not, as TwinCAT most likely needs direct access to the hardware and not through any abstraction layers provided by a hypervisor as VirtualBox. But for experiment, I created a Windows 10 x64 Pro machine and installed the latest TwinCAT 3.1.4022.20 XAE (development environment + runtime). I allocated 4 logical CPUs to the Win10-machine in VirtualBox, and verified that this was what TwinCAT was seeing as well.

Now I tried to start the program with TwinCAT using one of the CPUs that are shared with Windows:

TwinCAT3 not working in VirtualBox

This was expected. But TwinCAT says there is a solution. TwinCAT suggests to activate it on an isolated core. We could again isolate a core as I showed you above, but because we are software engineers we want to have at least two ways to do one thing, just in case we forget one of them. Another way to remove a logical CPU from Windows so that TwinCAT can have it all for itself is by using a built-in way in Windows. Press WindowsKey+R, enter msconfig and click OK. This will give you the System configuration tool. Go to Boot, and click Advanced options. Tick Number of processors and select one less than the virtual machine has. So in this example the virtual machine has four, select three.

System configuration in Windows 10

Do a reboot. Again, verify both in the task manager and doing a read from target in TwinCAT3. This is exactly how I want it to look. Just as with the previous example, we have an isolated core just for the TwinCAT3 program in the virtual machine.

Three windows cores + one isolated core

And finally, write a quick little structured-text program that only increments a number, activate configuration and put the system in RUN:

TwinCAT running in VirtualBox

it works!

Some final thoughts. Even though this worked, I’m still expecting some issues. I haven’t done any deep analysis, but the real-time properties (determinism/jitter) were far from optimal. The jitter for instance topped at a whopping 2000µs+ (i.e. more than 2ms). Even though my program only consisted of a simple counter running all by itself without any fieldbuses or external communication,  making it useless for critical tasks (especially involving fast fieldbuses as EtherCAT). Though this is of course less than optimal, for most of my purposes it doesn’t matter! If I want high-determinstic/low jitter performance I’d run this on a dedicated PLC optimized for this very purpose. But being able to virtualize a TwinCAT runtime gives me a possibility to do certain tests that would require a large amount of physical PLCs. This is a huge benefit with running PC-based control in generic and TwinCAT in specific. The possibilites to run many instances of a real-time controller (even only for doing certain tests), mixed with standard user-space programs are huge, and is definitely something I will elaborate further on. Beckhoff should without any doubt emphasize this possibility more!

Also, I have not tested what combinations of hardware and versions of TwinCAT/Windows this works with. This might or might not work with your specific configuration of the hardware and software. For reference I used:

  • Lenovo ThinkPad X1 carbon (gen6) with Intel Core i7-8550U CPU, BIOS 1.22
  • TwinCAT 3.1.4022.20
  • VirtualBox 5.2.12
  • Windows 10 Pro version 1803 (in both host and virtual machine)
  • CentOS 7.5.1804

As a grand finale I started a TwinCAT3 runtime/program on my (real) Windows host, and simultaneously I started another TwinCAT3 runtime/program as a virtual machine, and simultaneously with this I started the CentOS linux machine. Now I had two TwinCAT machines (on the same physical machine, one real and one virtual) each with their own separate AMS NetID. On the CentOS linux machine I downloaded the ADS-client sources from Beckhoff GitHub, compiled them and wrote a small little C++ test-program that retrieved a PLC-variable from the virtual ADS-host, and the result being:

Windows and Linux VMs communicating through ADS

This is PC-based control at its best!

  • Share on:

47 Comments, RSS

  1. Yannick Asselin

    Very interesting. As a side not, here’s an email I have received from Beckhoff regarding an issue with version 4022.20:

    Dear TwinCAT 3 users,

    as we have seen, you have downloaded TwinCAT 3.1 Build 4022.20. We need to inform you, that we have recognized problems
    (affecting even other software installations), which could occur during the uninstalling of this build.

    This concerns all TwinCAT installations (Full and XAR). We are currently building a new version.
    The current problem will be solved by simply installing this new version.

    Please do not hesitate to contact our worldwide support in case of any questions.

    We apologize for the inconvenience.

    TwinCAT Product Management Team

  2. Steven

    first, thank’s for your great articles about TwinCAT… !

    one question, i planed to buy a new notebook (actual T470s, reason is the
    ethernet Port)
    i read you use an X1 Carbon 6th. gen. with no real eth port,
    (i read with an Adapter) Is this port working well with EtherCAT ?

    thanks a lot, steven

    • Jakob Sagatowski

      Steven,
      thanks for your feedback!

      Actually, the 6th gen X1 carbon DOES have an Ethernet adapter NIC (Intel I219-V) integrated with an Ethernet port. However, it does not have an RJ45-port but a proprietary one, so you need to connect a dongle to the computer that converts the proprietary connector on the X1 to an RJ45. This dongle only converts the physical port of the X1, the NIC is however integrated in the computer.

      To know whether your computer/NIC is capable of running EtherCAT you can find it out in two ways.
      Alternative 1: Find out EXACTLY which NIC the T470s is using. This can be found in the device manager, right click on the NIC, select details and “Hardware Ids”. The information that you need is this string that includes VEN/DEV/SUBSYS (it’s not enought to just say Intel I219-V as there are variants of this). Send this information to your local Beckhoff support and ask if the EtherCAT-driver supports that NIC.

      Alternative 2: If you have a T470s available, install TwinCAT XAE on it, start visual studio and go to the toolbar -> “TwinCAT” -> “Show Realtime Ethernet Compatible Devices…”. If you NIC is listed under “Compatible devices” it will work with EtherCAT, otherwise not. If it’s compatible, you can install the EtherCAT-driver for it. Remember that you however most likely will not get the same real-time performance as from a Beckhoff PLC, thought I honestly have never compared the EtherCAT-performance between a say CX5140 PLC with my laptop.

      Good luck!

      • Olivier Lapierre

        From my experience (experiments), a laptop, with Ethercat I/O connected to the laptop, has a jitter incompatible with a 100µs task period. But at 1ms, it’s OK.

        Thanks for this very useful blog.

      • Joël

        Hello,

        Thank you for this explanations, it’s very interesting.
        I’m trying to work with my engineering computer with VMs (VMware workstation16) and with this explanations I can work with TwinCat3 in local in the VM.
        I wouldn’t like to install the TwinCAT on the Host Computer, but I would like to work online on a machine with Beckhoff computer, put a software program, scan Ethercat bus…
        If I try to install the Twincat driver on a Ethernet Adapter in NAT in my VM, the adapter isn’t show anymore in the connections. Did you have the same effect?
        It is necessary to install TC31_ADS on the Host computer and have a bridged Adapter?
        Target is to have a Beckhoff VM with all the Beckhoff software installed to work only with the VM…
        If someone have a trick, I take it…

        Thank you

        Regards

        Joël

  3. Alex Wallace

    This is really great!

    Did you try running an ethercat simulator in either the VM or the host TC? I wonder if you could do that or if ethercat would complain too much.

    • Jakob Sagatowski

      Hi Alex!

      I’ve personally not tried to run an EtherCAT simulator inside a virtual machine, but there are people that on the Linkedin TwinCAT-group have reported that it works (although with poor realtime performance). As a quick test I created a virtual machine, installed latest TwinCAT (3.1.4022.22) on it, selected the NIC on the VM to be “Intel Pro/1000 MT Desktop (82540EM)”, and TwinCAT says it is capable of running the EtherCAT-driver! So it’s definitely worth a try!

  4. Wolfgang

    Very nice article!

    I had a problem with virtualization from TwinCAT 4020. Back in TwinCAT 4018 we did it with VMWare without any problems. We had a 32 Bit Windows 7 as PlC and even the corresponding Server was an VMWare. In this way we could simulate our real machine. The only sideeffect was that the PLC sometimes crashed when we started the process where many simulated axis were moving, because there were timing violations.

    Thank You for this great article!

  5. Thank you for this post. I always have the need to develop and test offline without a physical PLC but always ran into the roadblock of running the runtime in a virtual environment (VMWare). This solution worked well and will definitely save me some time.

  6. Alec

    Hi Jakob
    Thanks for your post.
    MS have snuck in an extra feature in newer builds of Windows 10: Windows Hypervisor Platform (the W of Windows keeping it a mile from the H of Hyper in the list of features)
    This also needs to be disabled to keep TwinCAT from complaining about Hyper-V.
    Please could you update your superb article to include this info?
    Cheers
    Alec

  7. Kevin vandermaarel

    Back in July I started using this idea and it worked great to start. I did notice however that i am getting frequent BSOD with a 3B stop error due in part the hypervisor (HAL). Anyone else experiencing this issue?

  8. welsey

    thanks for your post, but i cannot find the page for setting core used in twincat, can you tell me where it is? thanks a lot.

  9. welsey

    thamks!
    oh, i cannot still find it, is it need install some plugins? could you give me a screenshot and then i can confirm it.

  10. Thijs

    Hi Jakob,

    Thanks for the great articles.

    I have been playing around with Virtual Box and recently with Docker. Do you think it would be possible to run Twincat XAR in a docker environment? This would be great for testing purposes.

    Kind regards,
    Thijs

    • Jakob Sagatowski

      Hi! To be honest, no idea. I haven’t tried Docker yet – but surely it’s a subject for a future post!

      • doki

        Hi Jakob,

        Not sure if you’ve explored the topic since, but TwinCAT XAR in a docker container would be amazing to use it with CI/CD tools, to automate our build tests in a controlled environment.

        Could you please direct me to some articles that discuss this topic if you know any?

        cheers

        • Hi Doki! I’ve not tested the XAR in Docker. It’s been on my to-do list for a while, but hopefully other bloggers/content creators can test it! If you try it out, maybe you can share your knowledge here or some other place?

  11. apr

    The main problem is the x64 guest operating system.

    In x64 OS TwinCAT 3 runtime tries to run as a virtualized x86 runtime. So you need proper nested virtualization support from your virtualization software. My host is osx, the virtualization software is Parallels.

    So I have enabled nested virtualization, and I got no more warning when activating my project(!), but the vm just hangs and after some minutes crash with BSOD. The isolated core trick works, but it is an overkill to reserve a core just testing a plc project offline 🙁 The latency time is 200µs for a big project, but IO is disabled in my test.

    I also worked with x86 win10 guests, and TC3 is happy to run without issues, no isolated core required! In this case the latency time is 20000µs for me, but I can test without issues (no IO) However I have some issues with a virtualized x86 win10 and 5k retina resolution, so I switched to x64 and found this helpful article…, thanks 🙂

  12. mahmoud

    I have this probelm when i activate configuration how can isolve it : Intel CPU is required to run TwinCAT on windows core under 64bit

      • Geo

        I have done this on a Ryzen 4800H and it works the same as Intel.
        I have TwinCat 3.1 & VMware Workstation installed & was faulting at “Activate Configuration” so I did the same, isolated 2 or the 16 virtual cores and is running, fault went away.

  13. Markus Wurm

    Hello,
    Thanks for this great tutorial.
    It helped me to quickly simulate multiple PLCs in office when to few real PLCs are available because they are all used by collegues.
    Of course I am aware that this can’t be a real world test, but for this pourpuse it is a great alternative to stacking even more Beckhoff CXes into our testing cabinet.

    Just one question: Normaly the warning pop up appears, when activating a configuration on cores wich already are running virtualization. But if I acidentely check the “Don’t show this again” checkbox, I will just crash. Does anyone know, if I can “reactivate” the pop up window again? Because I often have to change the real time configuration between machines and forgetting this when activating on the wrong machine result in crash obviously.
    Does anyone know of a setting anywhere, where this checkbox can be unchecked again?

  14. peter

    Is it possible to connect to & control real physical EtherCAT slaves(e.g. a servo drive) from TwinCAT installed in a virtual PC?

    I have TwinCAT 3.1.4023 installed in a VMWare VM (guest & host Win7 Pro x64).
    The PLC part works (increments a variable).

    The network adapter of the VM is set as bridged, and TwinCAT sees it as a Realtime Ethernet compatible device, with a binding to TwinCAT Ethernet protocol. The EtherCAT master is set to use this adapter.
    So everything seems OK, but when I do a scan – it does not find my slave. If I add the slave manually, there is no communication with it. LAN cable goes directly from laptop LAN port to driver’s EtherCAT IN port.
    Is it possible to do this at all? Any specific settings in TC or VM?
    (if I load the same project to a physical Beckhoff PLC – CX-43…. – it works fine).

    Thanks

  15. Davy

    A similar note to the one from Alec on 2018-10-23,

    After upgrading WSL to WSL 2 and enabling ‘Virtual Machine Platform’, I was unable to put twincat on my laptop in Run mode. The warning message is 0x1024 when activating the configuration and if you try to go in run mode anyway you get a “‘TCRTIME’ (200): start of real-time avoided by “HyperV””
    and
    “0x1024, RTIME: incompatible software detected)”
    The isolated core didn’t fix the problem for me.
    Here is a link to some more details about WSL 2 https://docs.microsoft.com/en-us/windows/wsl/wsl2-faq

  16. Jure

    Have you managed to get this setup to work? I have WSL2 installed on my Windows 10 laptop because Ubuntu subsystem but now I can’t get TwinCat to run mode. I get the TCRTIME 200 error.

    • Rich

      From your link:
      “The newest version of WSL uses Hyper-V architecture to enable its virtualization.”

      Despite having Hyper-V disabled, WSL2 seems to use parts of it anyway. I had to downgrade all WSL machines to WSL1 to get TwinCAT running. List WSL machines in powershell with ‘wsl -l -v’
      Some more steps may be necessary: https://github.com/MicrosoftDocs/WSL/issues/590

  17. Joost Breed

    I got this project with one project and not with another. In the end I noticed that the task was running on a Shared core, so all I had to do was moving the RT-core to an Isolated core. Then I did not get the errors anymore.

  18. CGrom

    Great post, that helped me understand incompatibility with the Hyper-V on Win11.
    Now, TC runs on a VM, however, I need to check whether there is no issue with the NEtwork adapter as Ecat Master.
    Cheers!

    • CGrom

      Even though, I was able to activate and debug a simple code in the RUNtime environment, the EtherCAT Master integration over the physical adapter apparently does not work if using HyperV in win11.
      I bridged the LAN adapter as external and then share it to the VM, however, TC3 shows it as incompatible device, namely Microsoft Hyper-V Network adapter. If it is though tried to use it, it just breaks the whole VM.
      At first time I did not want to use the Hyper-V but VirtualBox e.g. it has also compatibility issues with Win11.

  19. Francisco

    Hi everybody !

    Thank you Jakob Sagatowski for the tutorials, they are very good.

    I am a question/issue about TwinCAT and Virtual Machines. It is possible to conenct via ADS to TwinCAT inside a VM ? This is, if it possible to connect in the Windows host to a virtual machine runing TwinCAT3 localy

  20. Sander

    Hi Jakob,

    Love your content.

    I use TwinCAT inside a HyperV virtual machine, but it looks like it is not possible to ‘discover’ devices from inside the VM. I always have to add PLCs (targets) directly via IP address. Did you have the same issue with virtualbox, if so, were you able to resolve this?

  21. Amjad AbouShama

    Hi Jakob,

    I am getting ADS Error 4132 on my TwinCAT when I try to go online. any idea to fix it

    Thanks.

  22. Björn

    Hi,

    I’m on a new job with a new clean computer. What would you recommend? To install TC3 localy or install VM ware player and install TC3 on a virutal win10 om that one instead to keep the computer clean? I will work with TIA-portal also and some of my new colleagues recommend that I use a virtual machine för TIA. Mostly because TIA is almost impossible to uninstall.

    • Wiktor

      Björn, I always use VM (on Virtualbox) for engineering software (XAE) because of :
      -easy backup complete OS+XAE
      -easy switch to other computer. Desktop in office and laptop or tablet on field.
      I recommend this solution.

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.