mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 11:11:23 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0a93df287d | ||
|
|
18384ff964 | ||
|
|
e4ddad685a |
@@ -76,9 +76,11 @@ namespace MewtocolNet {
|
||||
while (ContinousReaderRunning) {
|
||||
|
||||
//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) {
|
||||
|
||||
|
||||
@@ -101,6 +101,8 @@ namespace MewtocolNet {
|
||||
|
||||
internal List<Task> PriorityTasks { get; set; } = new List<Task>();
|
||||
|
||||
internal int SendExceptionsInRow = 0;
|
||||
|
||||
#region Initialization
|
||||
|
||||
/// <summary>
|
||||
@@ -193,6 +195,20 @@ namespace MewtocolNet {
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes all permanent polling
|
||||
/// </summary>
|
||||
public void Disconnect () {
|
||||
|
||||
if (!IsConnected)
|
||||
return;
|
||||
|
||||
OnMajorSocketException();
|
||||
|
||||
PriorityTasks.Clear();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attaches a poller to the interface that continously
|
||||
/// polls the registered data registers and writes the values to them
|
||||
@@ -384,6 +400,10 @@ namespace MewtocolNet {
|
||||
foundToUpdate.SetValue(collection, ((NRegister<int>)reg).Value);
|
||||
}
|
||||
|
||||
if (foundToUpdate.PropertyType == typeof(TimeSpan)) {
|
||||
foundToUpdate.SetValue(collection, ((NRegister<TimeSpan>)reg).Value);
|
||||
}
|
||||
|
||||
//setting back strings
|
||||
|
||||
if (foundToUpdate.PropertyType == typeof(string)) {
|
||||
@@ -526,7 +546,6 @@ namespace MewtocolNet {
|
||||
/// Calculates checksum and sends a command to the PLC then awaits results
|
||||
/// </summary>
|
||||
/// <param name="_msg">MEWTOCOL Formatted request string ex: %01#RT</param>
|
||||
/// <param name="_close">Auto close of frame [true]%01#RT01\r [false]%01#RT</param>
|
||||
/// <returns>Returns the result</returns>
|
||||
public async Task<CommandResult> SendCommandAsync (string _msg) {
|
||||
|
||||
@@ -543,7 +562,7 @@ namespace MewtocolNet {
|
||||
|
||||
var awaittask = SendSingleBlock(_msg);
|
||||
PriorityTasks.Add(awaittask);
|
||||
awaittask.Wait();
|
||||
await awaittask;
|
||||
|
||||
PriorityTasks.Remove(awaittask);
|
||||
response = awaittask.Result;
|
||||
@@ -600,9 +619,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 +632,44 @@ namespace MewtocolNet {
|
||||
ASCIIEncoding enc = new ASCIIEncoding();
|
||||
string characters = enc.GetString(message);
|
||||
}
|
||||
} catch (IOException) {
|
||||
Logger.Log($"Critical IO exception on send", LogLevel.Critical, this);
|
||||
return null;
|
||||
} catch (SocketException) {
|
||||
OnMajorSocketException();
|
||||
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) {
|
||||
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) {
|
||||
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();
|
||||
}
|
||||
|
||||
} catch (Exception) {
|
||||
} catch (SocketException) {
|
||||
|
||||
if (IsConnected) {
|
||||
CycleTimeMs = 0;
|
||||
IsConnected = false;
|
||||
Disconnected?.Invoke();
|
||||
}
|
||||
|
||||
KillPoller();
|
||||
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
|
||||
OnMajorSocketException();
|
||||
return null;
|
||||
|
||||
}
|
||||
@@ -645,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
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<PackageId>MewtocolNet</PackageId>
|
||||
<Version>0.4.0</Version>
|
||||
<Version>0.4.2</Version>
|
||||
<Authors>Felix Weiss</Authors>
|
||||
<Company>Womed</Company>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
|
||||
Reference in New Issue
Block a user