Troubleshooting TwinCAT

From SymbitronWiki
Revision as of 13:10, 9 January 2018 by Gijsvanoort (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page lists some common problems that can be experience when working with TwinCAT and their solution.

Adding a 'To File' block makes building the Simulink model impossible

In some (older) versions of TwinCAT (<=3.1.4016.12 at least), this may happen. When trying to build a larger model, you get the following error:

 Error: File: C:\TwinCAT\Functions\TE1400-TargetForMatlabSimulink\Common\tctModuleClass_cpp.tlc Line: 260 Column: 50
 Ambiguous reference to System - must use array index to refer to one of multiple scopes
 Main program:
 ==> [00] C:\TwinCAT\Functions\TE1400-TargetForMatlabSimulink\Common\tctModuleClass_cpp.tlc:<NONE>(260)
     [01] C:\TwinCAT\Functions\TE1400-TargetForMatlabSimulink\Common\tctModuleFiles.tlc:<NONE>(15)
 Error: File: C:\TwinCAT\Functions\TE1400-TargetForMatlabSimulink\Common\tctModuleClass_cpp.tlc Line: 260 Column: 50
 The left-hand side of a . operator must be a valid scope identifier
 Main program:
 ==> [00] C:\TwinCAT\Functions\TE1400-TargetForMatlabSimulink\Common\tctModuleClass_cpp.tlc:<NONE>(260)
     [01] C:\TwinCAT\Functions\TE1400-TargetForMatlabSimulink\Common\tctModuleFiles.tlc:<NONE>(15)

The problem is a bug in the file `C:\TwinCAT\Functions\TE1400-TargetForMatlabSimulink\Common\tctModuleClass_cpp.tlc`. It was fixed somewhere between versions v3.1.4016.12 and v3.1.4018.26. If you have the bug, you can fix it yourself as follows:

Locate the above file and find the part (around line 260):

   // FileWriter - Reset Count to Allow more than 100000000 lines
   %foreach blockidx = CompiledModel.System.NumBlocks
       %if (CompiledModel.System.Block[blockidx].Type == "ToFile")
           %assign FwIwork = CompiledModel.System.Block[blockidx].IWork
           %assign Fwgrpindx = CompiledModel.DWorks.DWork[FwIwork[0],FwIwork[1]].VarGroupIdx
           %assign varGroupRec = CompiledModel.VarGroups.VarGroup[Fwgrpindx[0]]
           %assign cgtIdx = varGroupRec.CGTypeIdx
           %assign memberRecord = CompiledModel.CGTypes.CGType[cgtIdx].Members.Member[Fwgrpindx[1]]
   m_DWork.%<memberRecord.Name>.Count = 1;
   %%//%<varGroupRec.Name>.%<memberRecord.Name>.Count = 1;

Fully replace this part by the following snippet (obtained from v3.1.4018.26):

   // FileWriter - Reset Count to Allow more than 100000000 lines
       %foreach sysIdx = CompiledModel.NumSystems
           %if (CompiledModel.NumSystems>1)
               %assign sys = CompiledModel.System[sysIdx]
               %assign sys = CompiledModel.System
           %foreach blockIdx = sys.NumBlocks
               %assign block = sys.Block[blockIdx]
               %if (block.Type == "ToFile")
                   %assign FwIwork = block.IWork
                   %assign Fwgrpindx = CompiledModel.DWorks.DWork[FwIwork[0],FwIwork[1]].VarGroupIdx
                   %assign varGroupRec = CompiledModel.VarGroups.VarGroup[Fwgrpindx[0]]
                   %assign cgtIdx = varGroupRec.CGTypeIdx
                   %assign memberRecord = CompiledModel.CGTypes.CGType[cgtIdx].Members.Member[Fwgrpindx[1]]
                   m_DWork.%<memberRecord.Name>.Count = 1;

Alternatively (and better), install a more recent version of TwinCAT.

More detailed information about this error can be found here.

Cannot open include file: '<filename>'


This problem seems to be solved in newer versions of TwinCAT (3.1.4018). They come with their own versions of <string> etc.

Original issue

I had a model in which I used an S-function, which in its turn included extra C++ files. One of the C++ files had the line

 #include <string>

which, for a C++ file, is perfectly ok; it is a standard library and exists by default. However, when building the model in Simulink, I got the error:

 <file.cpp>: fatal error C1083: Cannot open include file: 'string': No such file or directory [<modelname.vcxproj>] 

The reason appears that, as one of the compiler options to CL.exe, the /X option is given, which means Ignore the standard include directory.

It is not so simple to get rid of this compiler option. And, if you succeed, it remains the question whether it would help (I quickly tried by opening the <modelname.vcxproj> manually in Microsoft Visual Studio and building to verify that the same error occurs here, then removed the compiler option with the VS graphical interface and build again. Now the compiler started spitting out other errors; apparently it really doesn't want to compile with standard libraries... But maybe I did something wrong?).

Opening the project file, changing the compiler option and building manually seems like a viable option, but the project file is regenerated at each build (I think) in Simulink; therefore this is not practical. I tried to locate the template for the project file in the TwinCAT directory, but in the template file, there are no compiler options.

It seems that there are compiler options in the files C:\TwinCAT\3.1\sdk\VsProps\TwinCAT RT (x64) Release.props and alike. I haven't tried however whether these are the exact files used. But, if you insist in trying to get the code working with standard libraries, you can try to modify the contents of these files:

  • Find out what type of build you are doing. In Simulink, press Ctrl-E and navigate to Code Generation|Tc Build. Look at the combination of 'Publish Configuration' and 'Publish binaries for platform...'. Most probably, you are building two versions, being
    • TwinCAT RT (x86), Release
    • TwinCAT RT (x64), Release
  • Open the appropriate files, locate the <IgnoreStandardIncludePath> tag and set it to false
  • Optionally, remove the <project>_tct directory in order to be sure that the project file is re-generated
  • Rebuild your model in Simulink
  • Hope for the best! Remember, I haven't checked whether this works!

(note: I solved the problem by removing the offending s-function and working my way around it).

Logging and sample time mess-up

Logging signals can be done by:

  • add a ToFile block in Simulink and connect all signals you want to log to it. Choose 'Array' as output data type. Build your model
  • Load the new model into TwinCAT
  • Add a TcExtendedFileWriter object to the TcCOM Object list
  • Create a new (real time) Task under SYSTEM|Tasks and make the TcExtendedFileWriter use this task.
    Do not re-use the task for your Simulink model. Failure to do so may result in your model running twice as slow without any warning.
  • Activate the new configuration.

Assertion failed: nBytesNeeded<=esGetIncomingPktDataBufSize(ES)


When using TwinCAT in External mode, you may get the following window:


after which you need to restart Matlab/Simulink.


The problem is caused by Simulink's External Mode which wants to receive more data than can fit in the internal buffer. This can occur when Simulink wasn't able to receive data for a while, for example when it is busy opening a complex Parameter-edit window. Then, as soon as it is able to receive data again, it must receive more data than can fit in the buffer.


Increase the buffer size. Fortunately, this is easy, as it can be configured from with TwinCAT: Parameter (Init)|.InitIncomingPktBufferSize and Parameter (Init)|.InitOutgoingPktBufferSize. Usually, the incoming (not outgoing) packet size is the problem, but to be sure, both can be set to a (much?) larger value. For my test, I changed both from 10000 to 100000, which seemed to completely solve the problem for my specific model (with many displays, just made for testing). These settings are kept when you update your model, so you only need to do this once. The image below shows where you can find the setting.


Simulink External Mode, parameter changes don't work

You can use Simulink External Mode, which allows bidirectional communication between Simulink and TwinCAT. Real-time signal values are shown in your display boxes in Simulink and you can change parameters by double-clicking on Simulink blocks. If the latter does not work, check:

  • Are you connected? The status bar (bottom gray area in Simulink window) should say 'External' on the left and T=xxxx plus some 'progress bar' on the right. Also, any display blocks should display values which update frequently (10 times/second?)
  • Is the 'update parameters' function activated? Double click at your object in TcCOM objects (TwinCAT window), go to Parameter (Init). Then unfold the ExtModeParameters tree and check the .AllowParameterChange value. It should be TRUE.
  • Is Batch Download parameter tuning disabled? Go to your Simulink model, choose Code|External Mode Control Panel from the menu bar. The Batch download checkbox should be UNchecked. Now you know that this function exists, you can also benefit from it. If you want to update various values at exactly the same time, check the box, do all parameter changes, choose Download and uncheck the box again.

Cannot enter (and check values) inside masked subsystems in the TwinCAT|Block Diagram view

This is by design. There's a checkbox to change the behaviour. In Simulink, go to Configuration Parameters (Ctrl-E), then go to Code Generation|Tc Advanced'. Check the Resolve Masked Subsystems box and rebuild.

Init12\IO: Set State TComObj SAFEOP: Set Objects (4) to SAFEOP >> AdsWarning: 1823 (0x71f, ADS ERROR: device aborted the action)

Problem' Cannot activate configuration after cloning (git) TwinCAT solution from other computer (with XAE installed). ADS error 0x71f is thrown when activating solution.

Cause I am sure that 0x71f is a very broad ADS Error message (request denied). Therefore it will likely have many causes. I build the Simulink model in MATLAB R2016a, solution in MSVS2013 PRO on my laptop. The Beckhoff RT PC (to which I cloned the project) has MSVS2012 and MATLAB R2014b. Version-related issue then?

Solution In my case, basically the problem was fixed by starting a complete new project:

  • Remove the TcCOM object, build the Simulink model, 'add new item', select correct object from the list.
  • Remove the I/O devices, scan and select the hardware again.
  • NOTE: You have to link all the inputs and outputs (which is a pain). Perhaps there is a better solution?

System freezes/hangs when TwinCAT is put into Run mode

In our specific case, this was caused by a Windows Update to patch the 'spectre/meltdown' problem, beginning of January 2018. The solution is to temporarily disable the patch and wait for a new TwinCAT version. More information can be found in a PDF, which we got from Beckhoff Support. Unfortunately I cannot upload PDF's to the Wiki. Our suggestion is to contact Beckhoff support.