From 412a4602619ab03009adaef5ce78fa8646f5beef Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Fri, 17 Jan 2020 15:29:34 +0100 Subject: [PATCH] Fixed InitialTranfer after connected --- Sharp7.Rx/Extensions/PlcExtensions.cs | 2 ++ Sharp7.Rx/Sharp7Plc.cs | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Sharp7.Rx/Extensions/PlcExtensions.cs b/Sharp7.Rx/Extensions/PlcExtensions.cs index 88259cf..619dd24 100644 --- a/Sharp7.Rx/Extensions/PlcExtensions.cs +++ b/Sharp7.Rx/Extensions/PlcExtensions.cs @@ -2,6 +2,7 @@ using System.Reactive; using System.Reactive.Disposables; using System.Reactive.Linq; +using System.Reactive.Threading.Tasks; using System.Threading.Tasks; using Sharp7.Rx.Enums; using Sharp7.Rx.Interfaces; @@ -23,6 +24,7 @@ namespace Sharp7.Rx.Extensions if (initialTransfer) { + await plc.ConnectionState.FirstAsync(state => state == ConnectionState.Connected).ToTask(); var initialValue = await ReadData(plc, readData); observer.OnNext(initialValue); } diff --git a/Sharp7.Rx/Sharp7Plc.cs b/Sharp7.Rx/Sharp7Plc.cs index 6bc4f38..abec30e 100644 --- a/Sharp7.Rx/Sharp7Plc.cs +++ b/Sharp7.Rx/Sharp7Plc.cs @@ -57,9 +57,6 @@ namespace Sharp7.Rx await s7Connector.InitializeAsync(); - RunNotifications(s7Connector, TimeSpan.FromMilliseconds(100)) - .AddDisposableTo(Disposables); - #pragma warning disable 4014 Task.Run(async () => { @@ -67,13 +64,16 @@ namespace Sharp7.Rx { await s7Connector.Connect(); } - catch (Exception) + catch (Exception e) { - + s7Connector.Logger.LogError(e, "Error while connecting to PLC"); } }); #pragma warning restore 4014 + RunNotifications(s7Connector, TimeSpan.FromMilliseconds(100)) + .AddDisposableTo(Disposables); + return true; }