Beckhoff / TF6000_ADS_DOTNET_V5_Samples

Sample code for the Version 6.X series of the TwinCAT ADS .NET Packages
https://infosys.beckhoff.com/content/1033/tc3_ads.net/9407515403.html?id=6770980177009971601
BSD Zero Clause License
37 stars 15 forks source link

PollValue order. #36

Closed Barteling closed 1 year ago

Barteling commented 1 year ago

If we use poll values at high rate and the PC which polls is under heavy load, we noticed the the events are not always fired at the pc in the order in which they were polled on the PLC.

Is it possible to improve on this?

We noticed this because we poll a timestamp from the PLC, for now we use this to reorder the read data but it would be better if we can trust the incoming event order as well.

RalfHeitmann commented 1 year ago

I am not fully aware of this scenario here. The 'events' you are talking about are coming all from the same 'Poll' call? In that case they all should be serialized. If they come from different 'Poll' calls they are executed in different async background threads which potentially could overtake each other (on the client side) - especially when the calling PC is under heavy load as you describe. At first sight, I think the only way to ensure the order is to synchronize your application, that the next ADS request is only sent after the previous ADS response is processed.

Barteling commented 1 year ago

It happen when I use sumread pollvalues and processing the first value I have long proccessing time, longer than my poll values interval.

I made some test runs and printed the output of the date time value which we also use read in the sum read command: This was my initial problem: 1

I noticed handling the values in the first run took almost 1 seconds so i optimized my code (in the invoke methods), which did fix the order:

afbeelding

However I still think that ideally the results should come in in order, and if one takes first long, this one might be off, but not all consecutive values which seem to come in randomly. i can simulate this problem with this code:

afbeelding

High load on the first value, all values after that are in random order.

RalfHeitmann commented 1 year ago

Seems to be the case that we have to much parallel processing here internally in PollValues resulting in Race-conditions in the Polling handler calls. In the just released version 6.0.216 https://www.nuget.org/packages/Beckhoff.TwinCAT.Ads.Reactive/6.0.216 the handlers will be serialized. The Poll Trigger will still be called asynchonously (meaning that the Ads Request are going out on trigger also if the handler is blocked), but the Polling handler receives the new Data serialized without mixing up the order. Hope that helps.

RalfHeitmann commented 1 year ago

Should be fixed with 6.0.216 and higher ...