From 0a93df287dc50d9187528bb9096e241eed200e5e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felix=20Wei=C3=9F?=
<72068105+Sandoun@users.noreply.github.com>
Date: Mon, 4 Jul 2022 16:53:55 +0200
Subject: [PATCH] Fixed more exception handling and added the ability to close
a running interface
---
MewtocolNet/Mewtocol/MewtocolInterface.cs | 47 +++++++++++++++++------
MewtocolNet/MewtocolNet.csproj | 2 +-
2 files changed, 36 insertions(+), 13 deletions(-)
diff --git a/MewtocolNet/Mewtocol/MewtocolInterface.cs b/MewtocolNet/Mewtocol/MewtocolInterface.cs
index 0853424..026db0c 100644
--- a/MewtocolNet/Mewtocol/MewtocolInterface.cs
+++ b/MewtocolNet/Mewtocol/MewtocolInterface.cs
@@ -195,6 +195,20 @@ namespace MewtocolNet {
}
+ ///
+ /// Closes all permanent polling
+ ///
+ public void Disconnect () {
+
+ if (!IsConnected)
+ return;
+
+ OnMajorSocketException();
+
+ PriorityTasks.Clear();
+
+ }
+
///
/// Attaches a poller to the interface that continously
/// polls the registered data registers and writes the values to them
@@ -532,7 +546,6 @@ namespace MewtocolNet {
/// Calculates checksum and sends a command to the PLC then awaits results
///
/// MEWTOCOL Formatted request string ex: %01#RT
- /// Auto close of frame [true]%01#RT01\r [false]%01#RT
/// Returns the result
public async Task SendCommandAsync (string _msg) {
@@ -622,6 +635,9 @@ namespace MewtocolNet {
} catch (IOException) {
Logger.Log($"Critical IO exception on send", LogLevel.Critical, this);
return null;
+ } catch (SocketException) {
+ OnMajorSocketException();
+ return null;
}
//await result
@@ -636,8 +652,11 @@ namespace MewtocolNet {
} catch (IOException) {
Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this);
return null;
+ } catch (SocketException) {
+ OnMajorSocketException();
+ return null;
}
-
+
sw.Stop();
var curCycle = (int)sw.ElapsedMilliseconds;
if (Math.Abs(CycleTimeMs - curCycle) > 2) {
@@ -650,16 +669,7 @@ namespace MewtocolNet {
} catch (SocketException) {
- if (IsConnected) {
-
- Logger.Log("The PLC connection was closed", LogLevel.Error, this);
- CycleTimeMs = 0;
- IsConnected = false;
- Disconnected?.Invoke();
- KillPoller();
-
- }
-
+ OnMajorSocketException();
return null;
}
@@ -668,6 +678,19 @@ namespace MewtocolNet {
}
+ private void OnMajorSocketException () {
+
+ if (IsConnected) {
+
+ Logger.Log("The PLC connection was closed", LogLevel.Error, this);
+ CycleTimeMs = 0;
+ IsConnected = false;
+ Disconnected?.Invoke();
+ KillPoller();
+
+ }
+
+ }
#endregion
diff --git a/MewtocolNet/MewtocolNet.csproj b/MewtocolNet/MewtocolNet.csproj
index 5f55422..55f44ae 100644
--- a/MewtocolNet/MewtocolNet.csproj
+++ b/MewtocolNet/MewtocolNet.csproj
@@ -2,7 +2,7 @@
netstandard2.0
MewtocolNet
- 0.4.1
+ 0.4.2
Felix Weiss
Womed
true