Read variables in Twincat 3 continously with the C# API

1.9k views Asked by At

I want to read a lot of variables continuously (faster than 10 Hz) via the ADS client of C#. However, in the documentation the examples only read the variables one time.

I found the Twincat-OCX module that has this functionality, but it is used for Twincat 2.

What is the recommended way for Twincat3 while using c#?

1

There are 1 answers

5
HaD On BEST ANSWER

There are different ways how to do that. The way that works best for me is to set the Notification Handle to a Toggle Bit in the PLC (port 851 TC3), maybe something like this:

// tic for ADS notification, imod used for cycle adjustment

diCounter := diCounter + 1;

IF (diCounter MOD imod = 0)
THEN
    bTic := NOT bTic;
END_IF

toggle bit in PLC

The notification handle is a good realtime tic for getting data into C#. This works fine down to a 10ms cycle if TwinCAT runs on a isolated core. On Beckhoff Controllers this communication into a user mode application is stable down to 1ms. At least that is my experience regarding deterministic real time hardware.

Now the notification is cyclically arriving in C#, with this signal an ADS sum Command can be used to get multiple Variables in one Request. This is also highly recommended in order to keep the communication overhead low. TC3 ADS SUM Commands .NET example:

https://infosys.beckhoff.de/english.php?content=../content/1033/tc3_adssamples_net/185258507.html&id=8424732030635156090