2 Commits

Author SHA1 Message Date
Felix Weiß
18384ff964 Fixed not updating timespan auto propertys and thread blocking Wait() call for await 2022-07-01 15:12:24 +02:00
Felix Weiß
e4ddad685a Fixed exception handling on receive 2022-06-30 15:47:05 +02:00
3 changed files with 44 additions and 19 deletions

View File

@@ -76,9 +76,11 @@ namespace MewtocolNet {
while (ContinousReaderRunning) { while (ContinousReaderRunning) {
//do priority tasks first //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) { } else if (getPLCinfoCycleCount > 25) {

View File

@@ -101,6 +101,8 @@ namespace MewtocolNet {
internal List<Task> PriorityTasks { get; set; } = new List<Task>(); internal List<Task> PriorityTasks { get; set; } = new List<Task>();
internal int SendExceptionsInRow = 0;
#region Initialization #region Initialization
/// <summary> /// <summary>
@@ -384,6 +386,10 @@ namespace MewtocolNet {
foundToUpdate.SetValue(collection, ((NRegister<int>)reg).Value); foundToUpdate.SetValue(collection, ((NRegister<int>)reg).Value);
} }
if (foundToUpdate.PropertyType == typeof(TimeSpan)) {
foundToUpdate.SetValue(collection, ((NRegister<TimeSpan>)reg).Value);
}
//setting back strings //setting back strings
if (foundToUpdate.PropertyType == typeof(string)) { if (foundToUpdate.PropertyType == typeof(string)) {
@@ -543,7 +549,7 @@ namespace MewtocolNet {
var awaittask = SendSingleBlock(_msg); var awaittask = SendSingleBlock(_msg);
PriorityTasks.Add(awaittask); PriorityTasks.Add(awaittask);
awaittask.Wait(); await awaittask;
PriorityTasks.Remove(awaittask); PriorityTasks.Remove(awaittask);
response = awaittask.Result; response = awaittask.Result;
@@ -600,9 +606,12 @@ namespace MewtocolNet {
await client.ConnectAsync(ip, port); await client.ConnectAsync(ip, port);
using (NetworkStream stream = client.GetStream()) { using (NetworkStream stream = client.GetStream()) {
var message = _blockString.ToHexASCIIBytes(); var message = _blockString.ToHexASCIIBytes();
var messageAscii = BitConverter.ToString(message).Replace("-", " "); var messageAscii = BitConverter.ToString(message).Replace("-", " ");
//send request //send request
try {
using (var sendStream = new MemoryStream(message)) { using (var sendStream = new MemoryStream(message)) {
await sendStream.CopyToAsync(stream); await sendStream.CopyToAsync(stream);
Logger.Log($"OUT MSG: {_blockString}", LogLevel.Critical, this); Logger.Log($"OUT MSG: {_blockString}", LogLevel.Critical, this);
@@ -610,33 +619,47 @@ namespace MewtocolNet {
ASCIIEncoding enc = new ASCIIEncoding(); ASCIIEncoding enc = new ASCIIEncoding();
string characters = enc.GetString(message); string characters = enc.GetString(message);
} }
} catch (IOException) {
Logger.Log($"Critical IO exception on send", LogLevel.Critical, this);
return null;
}
//await result //await result
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
try {
byte[] responseBuffer = new byte[256]; byte[] responseBuffer = new byte[256];
do { do {
int bytes = stream.Read(responseBuffer, 0, responseBuffer.Length); int bytes = stream.Read(responseBuffer, 0, responseBuffer.Length);
response.Append(Encoding.UTF8.GetString(responseBuffer, 0, bytes)); response.Append(Encoding.UTF8.GetString(responseBuffer, 0, bytes));
} }
while (stream.DataAvailable); while (stream.DataAvailable);
} catch (IOException) {
Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this);
return null;
}
sw.Stop(); sw.Stop();
var curCycle = (int)sw.ElapsedMilliseconds; var curCycle = (int)sw.ElapsedMilliseconds;
if (Math.Abs(CycleTimeMs - curCycle) > 2) { if (Math.Abs(CycleTimeMs - curCycle) > 2) {
CycleTimeMs = curCycle; CycleTimeMs = curCycle;
} }
Logger.Log($"IN MSG ({(int)sw.Elapsed.TotalMilliseconds}ms): {_blockString}", LogLevel.Critical, this);
Logger.Log($"IN MSG ({(int)sw.Elapsed.TotalMilliseconds}ms): {response}", LogLevel.Critical, this);
return response.ToString(); return response.ToString();
} }
} catch (Exception) { } catch (SocketException) {
if (IsConnected) { if (IsConnected) {
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
CycleTimeMs = 0; CycleTimeMs = 0;
IsConnected = false; IsConnected = false;
Disconnected?.Invoke(); Disconnected?.Invoke();
KillPoller();
} }
KillPoller();
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
return null; return null;
} }

View File

@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<PackageId>MewtocolNet</PackageId> <PackageId>MewtocolNet</PackageId>
<Version>0.4.0</Version> <Version>0.4.1</Version>
<Authors>Felix Weiss</Authors> <Authors>Felix Weiss</Authors>
<Company>Womed</Company> <Company>Womed</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>