maybe fix ModBus holding reg error

This commit is contained in:
Samuele Locatelli
2021-11-05 15:51:15 +01:00
parent 88c5cd4534
commit ea21dbcc67
2 changed files with 50 additions and 10 deletions
+7 -1
View File
@@ -106,6 +106,11 @@ namespace IOB_WIN_NEXT
/// </summary>
protected bool needRefresh = true;
/// <summary>
/// Generico numero di errori di rete read/write
/// </summary>
protected int networkReadWriteErrors = 0;
/// <summary>
/// Form chiamante
/// </summary>
@@ -2183,7 +2188,8 @@ namespace IOB_WIN_NEXT
}
catch (Exception exc)
{
lgError($"Errore in fase trySendValues{Environment.NewLine}{exc}");
networkReadWriteErrors++;
lgError($"Errore in fase trySendValues | networkReadWriteErrors: {networkReadWriteErrors}{Environment.NewLine}{exc}");
currDispData.semOut = Semaforo.SR;
}
raiseRefresh(currDispData);
+43 -9
View File
@@ -143,7 +143,14 @@ namespace IOB_WIN_NEXT
int lutAddress = 40000 + item.index;
if (HoldingRegisterLUT.ContainsKey(lutAddress))
{
listInt = HoldingRegisterLUT[lutAddress];
try
{
listInt = HoldingRegisterLUT[lutAddress];
}
catch (Exception exc)
{
lgError($"Errore in lettura da HoldingRegisterLUT per indirizzo {lutAddress} | item {item.memAddr}{Environment.NewLine}{exc}");
}
}
}
else
@@ -326,7 +333,14 @@ namespace IOB_WIN_NEXT
int lutAddress = 40000 + item.Value.index;
if (HoldingRegisterLUT.ContainsKey(lutAddress))
{
listInt = HoldingRegisterLUT[lutAddress];
try
{
listInt = HoldingRegisterLUT[lutAddress];
}
catch (Exception exc)
{
lgError($"Errore in lettura da HoldingRegisterLUT per indirizzo {lutAddress} | item {item.Key}{Environment.NewLine}{exc}");
}
}
}
else
@@ -412,23 +426,42 @@ namespace IOB_WIN_NEXT
stopwatch.Restart();
rawData = currPLC.ReadHoldingRegisters(startAddr - baseAddr, numReg);
stopwatch.Stop();
lgInfo($"Lettura in blocco HoldingRegisters| startAddr: {startAddr} | numReg {numReg} | {stopwatch.ElapsedMilliseconds}ms");
lgInfo($"Lettura in blocco HoldingRegisters| startAddr: {startAddr} | numReg {numReg} | rawData.lenght: {rawData.Length} | {stopwatch.ElapsedMilliseconds}ms");
// salvo in LUT la versione ESPLOSA 2 byte alla volta...
if (rawData.Length > 0)
{
for (int i = 0; i < rawData.Length / 2; i++)
{
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))
if (rawData.Length >= (i + 1) * 2)
{
HoldingRegisterLUT[currAddr] = thisSet;
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);
}
}
else
{
HoldingRegisterLUT.Add(currAddr, thisSet);
networkReadWriteErrors++;
lgError($"Impossibile copiare dati, array di origine troppo corto: rawData.lenght: {rawData.Length} | base addr richiesto i*2: {i * 2} | errori lettura: {networkReadWriteErrors}");
// se > 3 errori --> disconnetto
if (networkReadWriteErrors > 10)
{
networkReadWriteErrors = 0;
tryDisconnect();
}
else
{
// altrimenti pausa forzata
Thread.Sleep(200);
}
}
}
}
@@ -950,6 +983,7 @@ namespace IOB_WIN_NEXT
foreach (var item in memSetR)
{
readBlockHoldingReg(item.Key, item.Value);
Thread.Sleep(100);
}
}
// procedo ...