diff --git a/scr/FSI.BT.IR.Plc.TimeSync/SyncPlcTime.cs b/scr/FSI.BT.IR.Plc.TimeSync/SyncPlcTime.cs
index 7163db6..37290a1 100644
--- a/scr/FSI.BT.IR.Plc.TimeSync/SyncPlcTime.cs
+++ b/scr/FSI.BT.IR.Plc.TimeSync/SyncPlcTime.cs
@@ -7,8 +7,13 @@ namespace FSI.BT.IR.Plc.TimeSync
{
internal class SyncPlcTime : IPlc
{
- private Logger _log = LogManager.GetCurrentClassLogger();
- private const string DATE_TIME_FORMAT = "dd.MM.yyyy HH:mm:ss.fff";
+ #region Constants
+
+ private const string DATE_TIME_FORMAT = "dd.MM.yyyy HH:mm:ss.fff"; // Zeitformat
+
+ #endregion
+
+ private Logger _log = LogManager.GetCurrentClassLogger(); // Nlog
public SyncPlcTime(IPlc plc)
{
@@ -23,85 +28,137 @@ namespace FSI.BT.IR.Plc.TimeSync
Enable = plc.Enable;
}
+ ///
+ /// Name
+ ///
public string Name { get; set; }
+ ///
+ /// Beschreibung
+ ///
public string Description { get; set; }
+ ///
+ /// IP-Adresse
+ ///
public string Adress { get; set; }
+ ///
+ /// Rack-Nummer
+ ///
public int Rack { get; set; }
+ ///
+ /// Slot-Nummer
+ ///
public int Slot { get; set; }
+ ///
+ /// Update-Intervall, in der die Zeit überprüft werden soll.
+ ///
public int UpdateIntervall { get; set; }
+ ///
+ /// Zeitdifferenz, ab der die SPS-Zeit angepasst werden soll.
+ ///
public int TimeDifference { get; set; }
+ ///
+ /// Locale Zeit wird an die SPS gesendet - nicht UTC-Zeit
+ ///
public bool LocalTime { get; set; }
+ ///
+ /// Soll SPS-Zeit synchronisiert werden
+ ///
public bool Enable { get; set; }
+ ///
+ /// NTP-Server Adresse
+ ///
public string NtpServer { get; set; }
-
+
public async Task Snyc(CancellationToken cancellationToken) =>
await Task.Run(async () =>
{
+ var plc = new S7Client(); // SPS-Verbindung
+ var client = new NtpClient(NtpServer); // NTP-Client
+ NtpClock clock = client.Query(); // NTP-Client Uhrzeit
+ var plcDateTime = new DateTime(); // Uhrzeit SPS
+ DateTime ntpDateTime; // Uhrzeit NTP-Client
+
while (!cancellationToken.IsCancellationRequested)
{
- var plc = new S7Client();
-
- var connectionRslt = plc.ConnectTo(Adress, Rack, Slot);
-
- if (connectionRslt == 0)
+ try
{
- _log.Debug(Name + " Verbindung hergestellt.");
+ // Verbindung mit SPS-Aufbauen
+ var connectionRslt = plc.ConnectTo(Adress, Rack, Slot);
+
+ // Verbindungsstatus überprüfen
+ if (connectionRslt == 0) // Verbindung i.O.
+ {
+ _log.Debug(Name + " Verbindung hergestellt.");
+ }
+ else // Verbindung n.i.O.
+ {
+ _log.Error(Name + " Verbindung nicht hergestellt.");
+ _log.Error(Name + " Fehler: " + plc.ErrorText(connectionRslt));
+ await Task.Delay(UpdateIntervall, cancellationToken); // Warten bis zum nächsten Verbindungsversuch (Zeiten aus config.json)
+ continue;
+ }
}
- else
+ catch (Exception ex)
{
- _log.Error(Name + " Verbindung nicht hergestellt.");
- _log.Error(Name + " Fehler: " + plc.ErrorText(connectionRslt));
- await Task.Delay(UpdateIntervall, cancellationToken);
- continue;
+ _log.Error(Name + " " + ex.Message);
}
-
- var plcDateTime = new DateTime();
- plc.GetPlcDateTime(ref plcDateTime);
+
+ plc.GetPlcDateTime(ref plcDateTime); // Uhrzeit aus SPS auslesen
_log.Debug(Name + " SPS Zeit (aktuell): " + plcDateTime.ToString(DATE_TIME_FORMAT));
- var client = new NtpClient(NtpServer);
- NtpClock clock = client.Query();
-
- DateTime ntpDateTime;
-
- if (LocalTime)
+ if (LocalTime) // lokale Zeit/Ortszeit
{
- _log.Debug(Name + " UTC Zeit: " + clock.Now.ToString(DATE_TIME_FORMAT));
- ntpDateTime = clock.Now.DateTime;
+ _log.Debug(Name + " Ortszeit: " + clock.Now.ToString(DATE_TIME_FORMAT));
+ ntpDateTime = clock.Now.DateTime; // lokale Zeit/Ortszeit von NTP-Server
}
- else
+ else // UTC - Zeit
{
- _log.Debug(Name + " Ortszeit: " + clock.UtcNow.ToString(DATE_TIME_FORMAT));
- ntpDateTime = clock.UtcNow.DateTime;
+ _log.Debug(Name + " UTC-Zeit: " + clock.UtcNow.ToString(DATE_TIME_FORMAT));
+ ntpDateTime = clock.UtcNow.DateTime; // UTC-Zeit von NTP-Server
}
+ // Zeitdiffernz zwischen SPS-Zeit und Zeit von NTP-Server berechnen
var timeSpan = Math.Abs((plcDateTime - ntpDateTime).TotalMilliseconds);
- if (TimeDifference > 0 && timeSpan >= TimeDifference)
+
+ if (
+ TimeDifference > 0 // Zeitdifferenz aus Einstellungen > 0 ms
+ && timeSpan >= TimeDifference // Zeitdifferenz überprüfung
+ )
{
_log.Debug(Name + " Zeitdifferenz " + timeSpan + " ms überschritten");
+ var temp = plc.SetPlcDateTime(ntpDateTime); // Zeit an SPS senden
_log.Info(Name + " neue Zeit: " + ntpDateTime.ToString(DATE_TIME_FORMAT));
- var temp = plc.SetPlcDateTime(ntpDateTime);
}
else if (TimeDifference == 0)
{
+ var temp = plc.SetPlcDateTime(ntpDateTime); // Zeit an SPS senden
_log.Info(Name + " neue Zeit: " + ntpDateTime.ToString(DATE_TIME_FORMAT));
- var temp = plc.SetPlcDateTime(ntpDateTime);
}
- plc.Disconnect();
+ plc.Disconnect(); // Verbindung zur SPS trennen
_log.Debug(Name + " Verbindung getrennt.");
-
- await Task.Delay(UpdateIntervall, cancellationToken);
+
+ _log.Debug(Name + " Start Task-Wartezeit");
+
+ try
+ {
+ await Task.Delay(UpdateIntervall, cancellationToken); // Warten bis zum nächsten Verbindungsversuch (Zeiten aus config.json)
+ }
+ catch (Exception ex)
+ {
+ _log.Error(Name + " " + ex.Message);
+ }
+ _log.Debug(Name + " Ende Task-Wartezeit");
}
- }, cancellationToken);
+ }, cancellationToken);
}
}
diff --git a/scr/FSI.BT.IR.Plc.TimeSync/nlog.config b/scr/FSI.BT.IR.Plc.TimeSync/nlog.config
index 9951845..f0c9006 100644
--- a/scr/FSI.BT.IR.Plc.TimeSync/nlog.config
+++ b/scr/FSI.BT.IR.Plc.TimeSync/nlog.config
@@ -12,7 +12,7 @@