1

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#?

Lau
  • 1,353
  • 7
  • 26

1 Answers1

2

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

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
HaD
  • 36
  • 3
  • 2
    Another way to receive notifications every x milliseconds is to use [AdsTransMode.Cyclic](https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_adsnetref/7313417355.html&id=). Then the PLC will send the value every given milliseconds even if the value hasn't changed and no toggle bit is needed. – Quirzo May 29 '20 at 05:13
  • @Quirzo is this method more ressource conserving in contrast to the SumCommand? – Lau Jun 15 '20 at 14:31
  • 1
    Do you mean with SumCommand a sum read command or what do you mean? If the cycle time is very low, it will use some resources from the router. But on the other hand, the notifications will be sent from PLC only, so no need for polling which takes resources. You can also use the cyclic setting when using sum command to add many notifications at once. – Quirzo Jun 16 '20 at 05:28
  • @Quirzo Sorrym yes I mean the SumRead. But using the SumRead in combination with the cycling setting sounds great. Thank you! – Lau Jun 16 '20 at 06:49
  • 1
    A tweak to this is to put all the information you want to read into a single structure, so you can read it all in a single command (or be notified on change). – Rodney Richardson Aug 04 '20 at 10:56