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 int SendExceptionsInRow = 0;
|
||||
|
||||
#region Initialization
|
||||
|
||||
/// <summary>
|
||||
@@ -600,9 +602,12 @@ namespace MewtocolNet {
|
||||
await client.ConnectAsync(ip, port);
|
||||
|
||||
using (NetworkStream stream = client.GetStream()) {
|
||||
|
||||
var message = _blockString.ToHexASCIIBytes();
|
||||
var messageAscii = BitConverter.ToString(message).Replace("-", " ");
|
||||
|
||||
//send request
|
||||
try {
|
||||
using (var sendStream = new MemoryStream(message)) {
|
||||
await sendStream.CopyToAsync(stream);
|
||||
Logger.Log($"OUT MSG: {_blockString}", LogLevel.Critical, this);
|
||||
@@ -610,33 +615,44 @@ namespace MewtocolNet {
|
||||
ASCIIEncoding enc = new ASCIIEncoding();
|
||||
string characters = enc.GetString(message);
|
||||
}
|
||||
} catch (IOException) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//await result
|
||||
StringBuilder response = new StringBuilder();
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<PackageId>MewtocolNet</PackageId>
|
||||
<Version>0.4.0</Version>
|
||||
<Version>0.4.1</Version>
|
||||
<Authors>Felix Weiss</Authors>
|
||||
<Company>Womed</Company>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
|
||||
Reference in New Issue
Block a user