From e4ddad685a6626781799c9e01e9a1a316ba6688e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Wei=C3=9F?= <72068105+Sandoun@users.noreply.github.com> Date: Thu, 30 Jun 2022 15:47:05 +0200 Subject: [PATCH] Fixed exception handling on receive --- MewtocolNet/Mewtocol/MewtocolInterface.cs | 44 +++++++++++++++-------- MewtocolNet/MewtocolNet.csproj | 2 +- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/MewtocolNet/Mewtocol/MewtocolInterface.cs b/MewtocolNet/Mewtocol/MewtocolInterface.cs index aa4eb55..b529f02 100644 --- a/MewtocolNet/Mewtocol/MewtocolInterface.cs +++ b/MewtocolNet/Mewtocol/MewtocolInterface.cs @@ -101,6 +101,8 @@ namespace MewtocolNet { internal List PriorityTasks { get; set; } = new List(); + internal int SendExceptionsInRow = 0; + #region Initialization /// @@ -600,43 +602,57 @@ namespace MewtocolNet { await client.ConnectAsync(ip, port); using (NetworkStream stream = client.GetStream()) { + var message = _blockString.ToHexASCIIBytes(); var messageAscii = BitConverter.ToString(message).Replace("-", " "); + //send request - using (var sendStream = new MemoryStream(message)) { - await sendStream.CopyToAsync(stream); - Logger.Log($"OUT MSG: {_blockString}", LogLevel.Critical, this); - //log message sent - ASCIIEncoding enc = new ASCIIEncoding(); - string characters = enc.GetString(message); + try { + using (var sendStream = new MemoryStream(message)) { + await sendStream.CopyToAsync(stream); + Logger.Log($"OUT MSG: {_blockString}", LogLevel.Critical, this); + //log message sent + ASCIIEncoding enc = new ASCIIEncoding(); + string characters = enc.GetString(message); + } + } catch (IOException) { + return null; } + //await result StringBuilder response = new StringBuilder(); - byte[] responseBuffer = new byte[256]; - do { - int bytes = stream.Read(responseBuffer, 0, responseBuffer.Length); - response.Append(Encoding.UTF8.GetString(responseBuffer, 0, bytes)); + try { + + byte[] responseBuffer = new byte[256]; + do { + int bytes = stream.Read(responseBuffer, 0, responseBuffer.Length); + response.Append(Encoding.UTF8.GetString(responseBuffer, 0, bytes)); + } + while (stream.DataAvailable); + } catch (IOException) { + return null; } - while (stream.DataAvailable); + sw.Stop(); var curCycle = (int)sw.ElapsedMilliseconds; if (Math.Abs(CycleTimeMs - curCycle) > 2) { CycleTimeMs = curCycle; } Logger.Log($"IN MSG ({(int)sw.Elapsed.TotalMilliseconds}ms): {_blockString}", LogLevel.Critical, this); + SendExceptionsInRow = 0; return response.ToString(); } - } catch (Exception) { + } catch (SocketException) { if (IsConnected) { CycleTimeMs = 0; IsConnected = false; Disconnected?.Invoke(); + KillPoller(); + Logger.Log("The PLC connection was closed", LogLevel.Error, this); } - KillPoller(); - Logger.Log("The PLC connection was closed", LogLevel.Error, this); return null; } diff --git a/MewtocolNet/MewtocolNet.csproj b/MewtocolNet/MewtocolNet.csproj index 57b2871..5f55422 100644 --- a/MewtocolNet/MewtocolNet.csproj +++ b/MewtocolNet/MewtocolNet.csproj @@ -2,7 +2,7 @@ netstandard2.0 MewtocolNet - 0.4.0 + 0.4.1 Felix Weiss Womed true