diff --git a/IOB-WIN-NEXT/IobModbusTCP.cs b/IOB-WIN-NEXT/IobModbusTCP.cs
index 261e912a..c54f67e4 100644
--- a/IOB-WIN-NEXT/IobModbusTCP.cs
+++ b/IOB-WIN-NEXT/IobModbusTCP.cs
@@ -264,6 +264,18 @@ namespace IOB_WIN_NEXT
return valUInt;
}
+ ///
+ /// se si disconnette --> processo tentativo riconnessione..
+ ///
+ ///
+ private void CurrPLC_ConnectedChanged(object sender)
+ {
+ if (!currPLC.Connected)
+ {
+ tryDisconnect();
+ }
+ }
+
///
/// Recupero dati da singole letture
///
@@ -384,44 +396,52 @@ namespace IOB_WIN_NEXT
/// NUm registri da leggere (max 120)
private void readBlockHoldingReg(int startAddr, int numReg)
{
- // fix massima lunghezza pacchetto
- if (numReg > 120)
+ if (currPLC.Connected)
{
- lgError($"Attenzione richiesta lettura blocco troppo grande: numReg {numReg} --> 120");
- numReg = 120;
- }
- // si lavora rispetto all'indirizzo base 40001
- int baseAddr = 40001;
- int[] rawData = new int[2];
- try
- {
- stopwatch.Restart();
- rawData = currPLC.ReadHoldingRegisters(startAddr - baseAddr, numReg);
- stopwatch.Stop();
- lgInfo($"Lettura in blocco HoldingRegisters| startAddr: {startAddr} | numReg {numReg} | {stopwatch.ElapsedMilliseconds}ms");
- // salvo in LUT la versione ESPLOSA 2 byte alla volta...
- if (rawData.Length > 0)
+ // fix massima lunghezza pacchetto
+ if (numReg > 120)
{
- for (int i = 0; i < rawData.Length / 2; i++)
+ lgError($"Attenzione richiesta lettura blocco troppo grande: numReg {numReg} --> 120");
+ numReg = 120;
+ }
+ // si lavora rispetto all'indirizzo base 40001
+ int baseAddr = 40001;
+ int[] rawData = new int[2];
+ try
+ {
+ stopwatch.Restart();
+ rawData = currPLC.ReadHoldingRegisters(startAddr - baseAddr, numReg);
+ stopwatch.Stop();
+ lgInfo($"Lettura in blocco HoldingRegisters| startAddr: {startAddr} | numReg {numReg} | {stopwatch.ElapsedMilliseconds}ms");
+ // salvo in LUT la versione ESPLOSA 2 byte alla volta...
+ if (rawData.Length > 0)
{
- int[] thisSet = new int[2];
- Array.Copy(rawData, i * 2, thisSet, 0, 2);
- // salvo nel registro...
- int currAddr = startAddr + i * 2;
- if (HoldingRegisterLUT.ContainsKey(currAddr))
+ for (int i = 0; i < rawData.Length / 2; i++)
{
- HoldingRegisterLUT[currAddr] = thisSet;
- }
- else
- {
- HoldingRegisterLUT.Add(currAddr, thisSet);
+ int[] thisSet = new int[2];
+ Array.Copy(rawData, i * 2, thisSet, 0, 2);
+ // salvo nel registro...
+ int currAddr = startAddr + i * 2;
+ if (HoldingRegisterLUT.ContainsKey(currAddr))
+ {
+ HoldingRegisterLUT[currAddr] = thisSet;
+ }
+ else
+ {
+ HoldingRegisterLUT.Add(currAddr, thisSet);
+ }
}
}
}
+ catch (Exception exc)
+ {
+ lgError($"Eccezione in readBlockHoldingReg{Environment.NewLine}{exc}");
+ }
}
- catch (Exception exc)
+ else
{
- lgError($"Eccezione in readBlockHoldingReg{Environment.NewLine}{exc}");
+ lgError("Attenzione, plc disconnesso...");
+ tryDisconnect();
}
}
@@ -671,6 +691,8 @@ namespace IOB_WIN_NEXT
//Ip-Address and Port of Modbus-TCP-Server
currPLC = new ModbusClient(parametri.ipAdrr, parametri.port);
+ currPLC.ConnectedChanged += CurrPLC_ConnectedChanged;
+
// disconnetto e connetto...
if (isVerboseLog)
{
@@ -1208,14 +1230,17 @@ namespace IOB_WIN_NEXT
lgInfo("Richiesta disconnessione adapter ModBusTCP!");
try
{
- currPLC.Disconnect();
- connectionOk = false;
+ if (currPLC.Connected)
+ {
+ currPLC.Disconnect();
+ }
lgInfo("Effettuata disconnessione adapter ModBusTCP!");
}
catch (Exception exc)
{
lgFatal($"Errore nella disconnessione dall'adapter ModBusTCP{Environment.NewLine}{exc}");
}
+ connectionOk = false;
}
///
diff --git a/IOB-WIN-NEXT/IobModbusTCPHam.cs b/IOB-WIN-NEXT/IobModbusTCPHam.cs
index 98462265..cfaac7e6 100644
--- a/IOB-WIN-NEXT/IobModbusTCPHam.cs
+++ b/IOB-WIN-NEXT/IobModbusTCPHam.cs
@@ -138,117 +138,5 @@ namespace IOB_WIN_NEXT
}
#endregion Protected Methods
-
- #region Public Methods
-
- ///
- /// Override connessione
- ///
- public override void tryConnect()
- {
- bool doLog = (verboseLog || periodicLog);
- lgInfo("ModBus TCP HAM: tryConnect step 01");
- if (!connectionOk)
- {
- // SE è necessario refresh...
- if (needRefresh)
- {
- lgInfo("ModBus TCP HAM: tryConnect step 02");
-
- // reimporto parametri PLC se necessario...
- setParamPlc();
- }
- lgInfo("ModBus TCP HAM: tryConnect step 03");
-
- // controllo che il ping sia stato tentato almeno pingTestSec fa...
- if (DateTime.Now.Subtract(lastPING).TotalSeconds > utils.CRI("pingTestSec"))
- {
- if (doLog)
- {
- lgInfo("ModBus TCP HAM: ConnKO - tryConnect");
- }
- lgInfo("ModBus TCP HAM: tryConnect step 04");
-
- // in primis salvo data ping...
- lastPING = DateTime.Now;
- // se passa il ping faccio il resto...
- if (testPingMachine == IPStatus.Success)
- {
- string szStatusConnection = "ND";
- try
- {
- // ora provo connessione...
- parentForm.commPlcActive = true;
- currPLC.Connect();
- szStatusConnection = "OPEN";
- parentForm.commPlcActive = false;
- connectionOk = currPLC.Connected;
- lgInfo($"StatusConnection: {szStatusConnection}");
- // refresh stato allarmi!!!
- if (connectionOk)
- {
- if (adpRunning)
- {
- lgInfo($"Connessione OK: {connectionOk} | adpRunning: {adpRunning}");
- }
- }
- else
- {
- lgError("Impossibile procedere, connessione mancante...");
- }
- }
- catch (Exception exc)
- {
- lgFatal($"Errore in TryConnect adapter ModBusTCP | szStatusConnection {szStatusConnection}{Environment.NewLine}{exc}");
- connectionOk = false;
- needRefresh = true;
- }
- }
- else
- {
- // loggo no risposta ping ...
- connectionOk = false;
- if (doLog)
- {
- lgInfo($"Attenzione: ModBusTCP controllo PING fallito per IP {cIobConf.cncIpAddr}");
- }
- }
- }
- }
- // se non è ancora connesso faccio procesisng memoria caso disconnesso...
- if (!connectionOk)
- {
- // processo semafori ed invio...
- processMemoryDiscon();
- }
- }
-
- ///
- /// Override disconnessione
- ///
- public override void tryDisconnect()
- {
- if (connectionOk)
- {
- string szStatusConnection = "";
- try
- {
- currPLC.Disconnect();
- connectionOk = false;
- lgInfo(szStatusConnection);
- lgInfo("Effettuata disconnessione adapter ModBusTCP!");
- }
- catch (Exception exc)
- {
- lgFatal(exc, "Errore nella disconnessione dall'adapter ModBusTCP");
- }
- }
- else
- {
- lgError("IMPOSSIBILE effettuare disconnessione ModBusTCP: Connessione non disponibile...");
- }
- }
-
- #endregion Public Methods
}
}
\ No newline at end of file