diff --git a/MewtocolNet/Mewtocol/DynamicInterface.cs b/MewtocolNet/Mewtocol/DynamicInterface.cs index 6732b30..380555f 100644 --- a/MewtocolNet/Mewtocol/DynamicInterface.cs +++ b/MewtocolNet/Mewtocol/DynamicInterface.cs @@ -76,9 +76,11 @@ namespace MewtocolNet { while (ContinousReaderRunning) { //do priority tasks first - if (PriorityTasks.Count > 0) { + if (PriorityTasks != null && PriorityTasks.Count > 0) { - await PriorityTasks.FirstOrDefault(x => !x.IsCompleted); + try { + await PriorityTasks?.FirstOrDefault(x => !x.IsCompleted); + } catch (NullReferenceException) { } } else if (getPLCinfoCycleCount > 25) { diff --git a/MewtocolNet/Mewtocol/MewtocolInterface.cs b/MewtocolNet/Mewtocol/MewtocolInterface.cs index b529f02..0853424 100644 --- a/MewtocolNet/Mewtocol/MewtocolInterface.cs +++ b/MewtocolNet/Mewtocol/MewtocolInterface.cs @@ -386,6 +386,10 @@ namespace MewtocolNet { foundToUpdate.SetValue(collection, ((NRegister)reg).Value); } + if (foundToUpdate.PropertyType == typeof(TimeSpan)) { + foundToUpdate.SetValue(collection, ((NRegister)reg).Value); + } + //setting back strings if (foundToUpdate.PropertyType == typeof(string)) { @@ -545,7 +549,7 @@ namespace MewtocolNet { var awaittask = SendSingleBlock(_msg); PriorityTasks.Add(awaittask); - awaittask.Wait(); + await awaittask; PriorityTasks.Remove(awaittask); response = awaittask.Result; @@ -616,13 +620,13 @@ namespace MewtocolNet { string characters = enc.GetString(message); } } catch (IOException) { + Logger.Log($"Critical IO exception on send", LogLevel.Critical, this); return null; } //await result StringBuilder response = new StringBuilder(); try { - byte[] responseBuffer = new byte[256]; do { int bytes = stream.Read(responseBuffer, 0, responseBuffer.Length); @@ -630,6 +634,7 @@ namespace MewtocolNet { } while (stream.DataAvailable); } catch (IOException) { + Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this); return null; } @@ -638,19 +643,21 @@ namespace MewtocolNet { if (Math.Abs(CycleTimeMs - curCycle) > 2) { CycleTimeMs = curCycle; } - Logger.Log($"IN MSG ({(int)sw.Elapsed.TotalMilliseconds}ms): {_blockString}", LogLevel.Critical, this); - SendExceptionsInRow = 0; + + Logger.Log($"IN MSG ({(int)sw.Elapsed.TotalMilliseconds}ms): {response}", LogLevel.Critical, this); return response.ToString(); } } catch (SocketException) { if (IsConnected) { + + Logger.Log("The PLC connection was closed", LogLevel.Error, this); CycleTimeMs = 0; IsConnected = false; Disconnected?.Invoke(); KillPoller(); - Logger.Log("The PLC connection was closed", LogLevel.Error, this); + } return null;