From a2b6d848629d142694e91b2b70497b7950f8149d Mon Sep 17 00:00:00 2001 From: Peter Butzhammer Date: Tue, 30 Jul 2024 12:13:50 +0200 Subject: [PATCH] Wait for connection before setting up notification --- Sharp7.Rx/Sharp7Plc.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Sharp7.Rx/Sharp7Plc.cs b/Sharp7.Rx/Sharp7Plc.cs index 3bb5ac6..03bda1f 100644 --- a/Sharp7.Rx/Sharp7Plc.cs +++ b/Sharp7.Rx/Sharp7Plc.cs @@ -105,9 +105,13 @@ public class Sharp7Plc : IPlc disposableContainer.AddDisposableTo(disp); var observable = - // Read variable with GetValue first. - // This will propagate any errors due to reading from invalid addresses. - Observable.FromAsync(() => GetValue(variableName)) + ConnectionState + // Wait for connection to be established + .FirstAsync(c => c == Enums.ConnectionState.Connected) + // Read variable with GetValue first. + // This will propagate any errors due to reading from invalid addresses. + .SelectMany(_ => GetValue(variableName)) + // Output results from read loop .Concat( disposableContainer.Observable .Select(bytes => ValueConverter.ReadFromBuffer(bytes, address))