From ea21dbcc6787906defaf99bf779f99add6550dd0 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Fri, 5 Nov 2021 15:51:15 +0100 Subject: [PATCH] maybe fix ModBus holding reg error --- IOB-WIN-NEXT/IobGeneric.cs | 8 +++++- IOB-WIN-NEXT/IobModbusTCP.cs | 52 +++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/IOB-WIN-NEXT/IobGeneric.cs b/IOB-WIN-NEXT/IobGeneric.cs index 57f284f0..b8258933 100644 --- a/IOB-WIN-NEXT/IobGeneric.cs +++ b/IOB-WIN-NEXT/IobGeneric.cs @@ -106,6 +106,11 @@ namespace IOB_WIN_NEXT /// protected bool needRefresh = true; + /// + /// Generico numero di errori di rete read/write + /// + protected int networkReadWriteErrors = 0; + /// /// Form chiamante /// @@ -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); diff --git a/IOB-WIN-NEXT/IobModbusTCP.cs b/IOB-WIN-NEXT/IobModbusTCP.cs index c54f67e4..067cfdce 100644 --- a/IOB-WIN-NEXT/IobModbusTCP.cs +++ b/IOB-WIN-NEXT/IobModbusTCP.cs @@ -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 ...