mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 03:01:24 +00:00
Fixed exception handling on receive
This commit is contained in:
@@ -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>
|
||||||
@@ -600,9 +602,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 +615,44 @@ namespace MewtocolNet {
|
|||||||
ASCIIEncoding enc = new ASCIIEncoding();
|
ASCIIEncoding enc = new ASCIIEncoding();
|
||||||
string characters = enc.GetString(message);
|
string characters = enc.GetString(message);
|
||||||
}
|
}
|
||||||
|
} catch (IOException) {
|
||||||
|
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) {
|
||||||
|
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): {_blockString}", LogLevel.Critical, this);
|
||||||
|
SendExceptionsInRow = 0;
|
||||||
return response.ToString();
|
return response.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception) {
|
} catch (SocketException) {
|
||||||
|
|
||||||
if (IsConnected) {
|
if (IsConnected) {
|
||||||
CycleTimeMs = 0;
|
CycleTimeMs = 0;
|
||||||
IsConnected = false;
|
IsConnected = false;
|
||||||
Disconnected?.Invoke();
|
Disconnected?.Invoke();
|
||||||
}
|
|
||||||
|
|
||||||
KillPoller();
|
KillPoller();
|
||||||
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
|
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user