diff --git a/MewtocolNet/Mewtocol/MewtocolInterface.cs b/MewtocolNet/Mewtocol/MewtocolInterface.cs
index 0f63063..396afe5 100644
--- a/MewtocolNet/Mewtocol/MewtocolInterface.cs
+++ b/MewtocolNet/Mewtocol/MewtocolInterface.cs
@@ -11,33 +11,58 @@ using MewtocolNet.RegisterAttributes;
using MewtocolNet.Logging;
using System.Collections;
using System.Diagnostics;
+using System.ComponentModel;
namespace MewtocolNet {
-
+
///
/// The PLC com interface class
///
- public partial class MewtocolInterface {
+ public partial class MewtocolInterface : INotifyPropertyChanged {
///
/// Gets triggered when the PLC connection was established
///
public event Action Connected;
+ ///
+ /// Gets triggered when the PLC connection was closed or lost
+ ///
+ public event Action Disconnected;
+
///
/// Gets triggered when a registered data register changes its value
///
public event Action RegisterChanged;
+ ///
+ /// Gets triggered when a property of the interface changes
+ ///
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ private bool isConnected;
///
/// The current connection state of the interface
///
- public bool IsConnected { get; private set; }
+ public bool IsConnected {
+ get => isConnected;
+ private set {
+ isConnected = value;
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsConnected)));
+ }
+ }
+ private PLCInfo plcInfo;
///
/// Generic information about the connected PLC
///
- public PLCInfo PlcInfo { get; private set; }
+ public PLCInfo PlcInfo {
+ get => plcInfo;
+ private set {
+ plcInfo = value;
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PlcInfo)));
+ }
+ }
///
/// The registered data registers of the PLC
@@ -46,7 +71,7 @@ namespace MewtocolNet {
private string ip;
private int port;
- private int stationNumber;
+ private int stationNumber;
///
/// The current IP of the PLC connection
@@ -61,7 +86,20 @@ namespace MewtocolNet {
///
public int StationNumber => stationNumber;
- internal List PriorityTasks { get; set; } = new List();
+ private int cycleTimeMs;
+ ///
+ /// The duration of the last message cycle
+ ///
+ public int CycleTimeMs {
+ get { return cycleTimeMs; }
+ set {
+ cycleTimeMs = value;
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CycleTimeMs)));
+ }
+ }
+
+
+ internal List PriorityTasks { get; set; } = new List();
#region Initialization
@@ -72,10 +110,10 @@ namespace MewtocolNet {
/// Port of the PLC
/// Station Number of the PLC
public MewtocolInterface (string _ip, int _port = 9094, int _station = 1) {
-
+
ip = _ip;
port = _port;
- stationNumber = _station;
+ stationNumber = _station;
Connected += MewtocolInterface_Connected;
@@ -90,7 +128,8 @@ namespace MewtocolNet {
RegisterChanged += (o) => {
- string address = $"{o.GetRegisterString()}{o.MemoryAdress}".PadRight(5, (char)32); ;
+ string address = $"{o.GetRegisterString()}{o.MemoryAdress}".PadRight(5, (char)32);
+ ;
Logger.Log($"{address} " +
$"{(o.Name != null ? $"({o.Name}) " : "")}" +
@@ -144,6 +183,7 @@ namespace MewtocolNet {
if (OnFailed != null) {
OnFailed();
+ Disconnected?.Invoke();
Logger.Log("Initial connection failed", LogLevel.Info, this);
}
@@ -190,7 +230,7 @@ namespace MewtocolNet {
string propName = prop.Name;
foreach (var attr in attributes) {
- if(attr is RegisterAttribute cAttribute) {
+ if (attr is RegisterAttribute cAttribute) {
if (prop.PropertyType == typeof(bool) && cAttribute.AssignedBitIndex == -1) {
if (cAttribute.SpecialAddress == SpecialAddress.None) {
@@ -227,7 +267,7 @@ namespace MewtocolNet {
//read number as bit array
if (prop.PropertyType == typeof(BitArray)) {
- if(cAttribute.BitCount == BitCount.B16) {
+ if (cAttribute.BitCount == BitCount.B16) {
AddRegister(cAttribute.MemoryArea, _name: propName, _isBitwise: true);
} else {
AddRegister(cAttribute.MemoryArea, _name: propName, _isBitwise: true);
@@ -259,7 +299,7 @@ namespace MewtocolNet {
RegisterChanged += (reg) => {
var foundToUpdate = props.FirstOrDefault(x => x.Name == reg.Name);
-
+
if (foundToUpdate != null) {
var foundAttributes = foundToUpdate.GetCustomAttributes(true);
@@ -329,7 +369,7 @@ namespace MewtocolNet {
}
- } else if(foundToUpdate.PropertyType == typeof(BitArray)) {
+ } else if (foundToUpdate.PropertyType == typeof(BitArray)) {
//setting back bit registers
if (reg is NRegister shortReg) {
@@ -360,7 +400,7 @@ namespace MewtocolNet {
collection.OnInterfaceLinked(this);
Connected += (i) => {
- if(collection != null)
+ if (collection != null)
collection.OnInterfaceLinkedAndOnline(this);
};
@@ -473,7 +513,7 @@ namespace MewtocolNet {
string response = null;
- if(ContinousReaderRunning) {
+ if (ContinousReaderRunning) {
//if the poller is active then add all messages to a qeueue
@@ -481,7 +521,7 @@ namespace MewtocolNet {
PriorityTasks.Add(awaittask);
awaittask.Wait();
- PriorityTasks.Remove(awaittask);
+ PriorityTasks.Remove(awaittask);
response = awaittask.Result;
} else {
@@ -492,7 +532,7 @@ namespace MewtocolNet {
}
- if(response == null) {
+ if (response == null) {
return new CommandResult {
Success = false,
Error = "0000",
@@ -527,7 +567,7 @@ namespace MewtocolNet {
private async Task SendSingleBlock (string _blockString) {
- Stopwatch sw = Stopwatch.StartNew();
+ Stopwatch sw = Stopwatch.StartNew();
using (TcpClient client = new TcpClient() { ReceiveBufferSize = 64, NoDelay = true, ExclusiveAddressUse = true }) {
@@ -559,15 +599,19 @@ namespace MewtocolNet {
return response.ToString();
}
- } catch(Exception) {
+ } catch (Exception) {
+
+ if (IsConnected) {
+ IsConnected = false;
+ Disconnected?.Invoke();
+ }
- IsConnected = false;
KillPoller();
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
return null;
}
-
+
}
}
diff --git a/MewtocolNet/MewtocolNet.csproj b/MewtocolNet/MewtocolNet.csproj
index da98a99..394286b 100644
--- a/MewtocolNet/MewtocolNet.csproj
+++ b/MewtocolNet/MewtocolNet.csproj
@@ -2,7 +2,7 @@
netstandard2.0
MewtocolNet
- 0.3.2
+ 0.3.3
Felix Weiss
Womed
true