evopro-ag / Sharp7Reactive

Apache License 2.0
61 stars 14 forks source link

Question? Related to events from PLC #6

Closed sekulicb closed 4 months ago

sekulicb commented 5 months ago

Hi. One question....is there a way to get "event" from PLC, such that when we send a value to PLC, afters some time PLC triggers some sort of event so that we can act upon it and do some database operation?

We need reverse flow sort to speak. Not only to read from PLC, but to be notified when some value changes from 0 => 1 let say, and to get event and act on that event, and notify the UI for example. Is this scenario possible. Thanks for any advice, much appreciated.

Peter-B- commented 5 months ago

Hello @sekulicb,

this is a very common scenario. You can use .CreateNotification() to receive an IObservable of values:

using var plc = new Sharp7Plc("10.30.110.62", 0, 0);

await plc.InitializeConnection();

var subscription = 
    plc.CreateNotification<bool>($"DB2050.Bit1.3", Sharp7.Rx.Enums.TransmissionMode.OnChange)
    .Where(value => value == true)
    .Subscribe(value => {
        Console.WriteLine("DB2050.Bit1.3 was triggered");
    });

If you preferer procedural code, you can get a Task from the Observable:

while (true)
{
    await 
        plc.CreateNotification<bool>($"DB2050.Bit1.3", Sharp7.Rx.Enums.TransmissionMode.OnChange)
            .FirstAsync(value => value == true)
            .ToTask(cancellationToken);

    Console.WriteLine("Triggered");

    await
        plc.CreateNotification<bool>($"DB2050.Bit1.3", Sharp7.Rx.Enums.TransmissionMode.OnChange)
            .FirstAsync(value => value == false)
            .ToTask(cancellationToken);

    Console.WriteLine("Reset");
}

Please note a possible race condition here: The value in the PLC may change before the next subscription is created.

I hope this helps you to get stated Peter

sekulicb commented 5 months ago

Hi @Peter-B- I thought this library is tread safe (even if sharp7 is not)? Okay so we need to use lock or one of Concurrent Collections from .NET and that would solve the problem. Thanks

fbarresi commented 5 months ago

Hi!

wait, actually this library IS thread safe and you don't need to use any lock. The RFC protocol used in the library isn't by design so, the library provides a own scheduler in order to be thread safe.

What Peter meant was about the different cycle time between the plc and the software using sharp7.rx. If a variable get changed in the plc quicker than the min sample time of the library, you might miss events.

If you need to use a very short cycle time (like <<100ms) or the same time as the plc you can or manage critical changes:

Hope this clarification helped.

Best regards,

FB

sekulicb commented 5 months ago

Hi @fbarresi

I see. Thank you for the clarification. We need to discuss this with PLC team, since I run IT department and this is beyond our expertise as software developers. None the less, at least now I know a direction that we need to take. Much appreciated!