diff --git a/IOB-WIN-NEXT/IobModbusTCP.cs b/IOB-WIN-NEXT/IobModbusTCP.cs index 9817951e..841344aa 100644 --- a/IOB-WIN-NEXT/IobModbusTCP.cs +++ b/IOB-WIN-NEXT/IobModbusTCP.cs @@ -1481,191 +1481,198 @@ namespace IOB_WIN_NEXT // procedo x ogni valore configurato... foreach (var item in memItemList) { - listInt = new int[item.Value.size]; - // attesa 50 ms prima di procedere x evitare burst dati - if (!useLUT) + try { - Thread.Sleep(50); - } - string valString = ""; - double valore = 0; - double valoreScal = 0; - bool dataOk = false; - // in primis DEVO determinare di quale TIPO di valore ho bisogno... dalla PRIMA - // cifra di memAddr... - modBusAddrType memAddrType = getAddrType(item.Value.memAddr); - // in base al tipo leggo array... - switch (memAddrType) - { - case modBusAddrType.Coil: - listBool = readCoil(item.Value.index, item.Value.size); - valore = listBool[0] ? 1 : 0; - break; + listInt = new int[item.Value.size]; + // attesa 50 ms prima di procedere x evitare burst dati + if (!useLUT) + { + Thread.Sleep(50); + } + string valString = ""; + double valore = 0; + double valoreScal = 0; + bool dataOk = false; + // in primis DEVO determinare di quale TIPO di valore ho bisogno... dalla PRIMA + // cifra di memAddr... + modBusAddrType memAddrType = getAddrType(item.Value.memAddr); + // in base al tipo leggo array... + switch (memAddrType) + { + case modBusAddrType.Coil: + listBool = readCoil(item.Value.index, item.Value.size); + valore = listBool[0] ? 1 : 0; + break; - case modBusAddrType.DiscreteInput: - listBool = readDiscrInputs(item.Value.index, item.Value.size); - valore = listBool[0] ? 1 : 0; - break; + case modBusAddrType.DiscreteInput: + listBool = readDiscrInputs(item.Value.index, item.Value.size); + valore = listBool[0] ? 1 : 0; + break; - case modBusAddrType.InputRegister: - if (useLUT) - { - int lutAddress = 30000 + item.Value.index + deltaBase + indexLutCorr; - // se size = 1 --> devo cambiare modo recupero - if (item.Value.size == 1) + case modBusAddrType.InputRegister: + if (useLUT) { - //if (item.Value.index % 2 == 0) - if (lutAddress % 2 == 0) + int lutAddress = 30000 + item.Value.index + deltaBase + indexLutCorr; + // se size = 1 --> devo cambiare modo recupero + if (item.Value.size == 1) { - //Array.Copy(HoldingRegisterLUT[lutAddress], listInt, 1); - Array.Copy(InputRegisterLUT[lutAddress], 0, listInt, 0, 1); + //if (item.Value.index % 2 == 0) + if (lutAddress % 2 == 0) + { + //Array.Copy(HoldingRegisterLUT[lutAddress], listInt, 1); + Array.Copy(InputRegisterLUT[lutAddress], 0, listInt, 0, 1); + } + else + { + lutAddress--; + Array.Copy(InputRegisterLUT[lutAddress], 1, listInt, 0, 1); + } } else { - lutAddress--; - Array.Copy(InputRegisterLUT[lutAddress], 1, listInt, 0, 1); + if (InputRegisterLUT.ContainsKey(lutAddress)) + { + try + { + if (listInt.Length == 2) + { + listInt = InputRegisterLUT[lutAddress]; + } + else + { + // faccio ciclo x copiare 2 alla volta... + for (int i = 0; i < listInt.Length; i += 2) + { + Array.Copy(InputRegisterLUT[lutAddress + i], 0, listInt, i, 2); + } + } + } + catch (Exception exc) + { + lgError($"Errore in lettura da InputRegisterLUT per indirizzo {lutAddress} | item {item.Key}{Environment.NewLine}{exc}"); + } + } } } else { - if (InputRegisterLUT.ContainsKey(lutAddress)) + listInt = readInputReg(item.Value.index, item.Value.size); + if (item.Value.tipoMem == plcDataType.String) { - try + // valore string... + valString = ModbusClient.ConvertRegistersToString(listInt, 0, item.Value.size); + } + else + { + // valori numerici... + valore = convertFromReg(listInt, item.Value.size, item.Value.tipoMem); + } + } + + break; + + case modBusAddrType.HoldingRegister: + if (useLUT) + { + int lutAddress = 40000 + item.Value.index + deltaBase + indexLutCorr; + // se size = 1 --> devo cambiare modo recupero + if (item.Value.size == 1) + { + //if (item.Value.index % 2 == 0) + if (lutAddress % 2 == 0) { - if (listInt.Length == 2) + //Array.Copy(HoldingRegisterLUT[lutAddress], listInt, 1); + Array.Copy(HoldingRegisterLUT[lutAddress], 0, listInt, 0, 1); + } + else + { + lutAddress--; + Array.Copy(HoldingRegisterLUT[lutAddress], 1, listInt, 0, 1); + } + } + else + { + if (HoldingRegisterLUT.ContainsKey(lutAddress)) + { + try { - listInt = InputRegisterLUT[lutAddress]; - } - else - { - // faccio ciclo x copiare 2 alla volta... - for (int i = 0; i < listInt.Length; i += 2) + if (listInt.Length == 2) { - Array.Copy(InputRegisterLUT[lutAddress + i], 0, listInt, i, 2); + listInt = HoldingRegisterLUT[lutAddress]; + } + else + { + // faccio ciclo x copiare 2 alla volta... + for (int i = 0; i < listInt.Length; i += 2) + { + Array.Copy(HoldingRegisterLUT[lutAddress + i], 0, listInt, i, 2); + } } } - } - catch (Exception exc) - { - lgError($"Errore in lettura da InputRegisterLUT per indirizzo {lutAddress} | item {item.Key}{Environment.NewLine}{exc}"); + catch (Exception exc) + { + lgError($"Errore in lettura da HoldingRegisterLUT per indirizzo {lutAddress} | item {item.Key}{Environment.NewLine}{exc}"); + } } } } - } - else - { - listInt = readInputReg(item.Value.index, item.Value.size); + else + { + listInt = readHoldReg(item.Value.index - 1, item.Value.size); + } if (item.Value.tipoMem == plcDataType.String) { // valore string... valString = ModbusClient.ConvertRegistersToString(listInt, 0, item.Value.size); + valString = valString.Replace("\n", "").Replace("\r", "").Replace("\0", string.Empty).Trim(); } else { - // valori numerici... valore = convertFromReg(listInt, item.Value.size, item.Value.tipoMem); } - } + break; - break; - - case modBusAddrType.HoldingRegister: - if (useLUT) - { - int lutAddress = 40000 + item.Value.index + deltaBase + indexLutCorr; - // se size = 1 --> devo cambiare modo recupero - if (item.Value.size == 1) - { - //if (item.Value.index % 2 == 0) - if (lutAddress % 2 == 0) - { - //Array.Copy(HoldingRegisterLUT[lutAddress], listInt, 1); - Array.Copy(HoldingRegisterLUT[lutAddress], 0, listInt, 0, 1); - } - else - { - lutAddress--; - Array.Copy(HoldingRegisterLUT[lutAddress], 1, listInt, 0, 1); - } - } - else - { - if (HoldingRegisterLUT.ContainsKey(lutAddress)) - { - try - { - if (listInt.Length == 2) - { - listInt = HoldingRegisterLUT[lutAddress]; - } - else - { - // faccio ciclo x copiare 2 alla volta... - for (int i = 0; i < listInt.Length; i += 2) - { - Array.Copy(HoldingRegisterLUT[lutAddress + i], 0, listInt, i, 2); - } - } - } - catch (Exception exc) - { - lgError($"Errore in lettura da HoldingRegisterLUT per indirizzo {lutAddress} | item {item.Key}{Environment.NewLine}{exc}"); - } - } - } - } - else - { - listInt = readHoldReg(item.Value.index - 1, item.Value.size); - } - if (item.Value.tipoMem == plcDataType.String) - { - // valore string... - valString = ModbusClient.ConvertRegistersToString(listInt, 0, item.Value.size); - valString = valString.Replace("\n", "").Replace("\r", "").Replace("\0", string.Empty).Trim(); - } - else - { - valore = convertFromReg(listInt, item.Value.size, item.Value.tipoMem); - } - break; - - default: - break; - } - if (item.Value.tipoMem == plcDataType.String) - { - saveValueString(ref outVal, valString, item.Key); - lgDebug($"getDataDictionary: valore ricevuto | {item.Key} | val: {valString}"); - } - else - { - // verifica limite... con delta da impianto - dataOk = (valore >= (item.Value.minVal + deltaVal) && valore <= (item.Value.maxVal - deltaVal)); - if (dataOk || disDynDataRangeCheck) + default: + break; + } + if (item.Value.tipoMem == plcDataType.String) { - // verifica valori EPOCH x convertirli in datetime... - if (item.Value.unit == "EPOCH") - { - valString = TimeUtils.epochConvert(valore).ToString("yyyy-MM-dd HH:mm:ss"); - saveValueString(ref outVal, valString, item.Key); - lgDebug($"getDataDictionary: valore EPOCH ricevuto | {item.Key} | val: {valore} | valString: {valString}"); - } - else - { - // moltiplico x fattore conversione... - valoreScal = valore * item.Value.factor; - saveValue(ref outVal, valoreScal, item.Key); - lgDebug($"getDataDictionary: valore ricevuto | {item.Key} | val: {valore} | valoreScal: {valoreScal}"); - } + saveValueString(ref outVal, valString, item.Key); + lgDebug($"getDataDictionary: valore ricevuto | {item.Key} | val: {valString}"); } else { - lgError($"getDataDictionary: valore scartato x limiti min/max | {item.Key} | val: {valore} | valoreScal: {valoreScal} | min-max: {item.Value.minVal}-{item.Value.maxVal} | deltaVal: {deltaVal}"); - readErrorList.Add(item.Key, item.Value); - lgDebug($"--> rimesso in coda lettura | parametro: {item.Key} | index: {item.Value.index} | size: {item.Value.size}"); + // verifica limite... con delta da impianto + dataOk = (valore >= (item.Value.minVal + deltaVal) && valore <= (item.Value.maxVal - deltaVal)); + if (dataOk || disDynDataRangeCheck) + { + // verifica valori EPOCH x convertirli in datetime... + if (item.Value.unit == "EPOCH") + { + valString = TimeUtils.epochConvert(valore).ToString("yyyy-MM-dd HH:mm:ss"); + saveValueString(ref outVal, valString, item.Key); + lgDebug($"getDataDictionary: valore EPOCH ricevuto | {item.Key} | val: {valore} | valString: {valString}"); + } + else + { + // moltiplico x fattore conversione... + valoreScal = valore * item.Value.factor; + saveValue(ref outVal, valoreScal, item.Key); + lgDebug($"getDataDictionary: valore ricevuto | {item.Key} | val: {valore} | valoreScal: {valoreScal}"); + } + } + else + { + lgError($"getDataDictionary: valore scartato x limiti min/max | {item.Key} | val: {valore} | valoreScal: {valoreScal} | min-max: {item.Value.minVal}-{item.Value.maxVal} | deltaVal: {deltaVal}"); + readErrorList.Add(item.Key, item.Value); + lgDebug($"--> rimesso in coda lettura | parametro: {item.Key} | index: {item.Value.index} | size: {item.Value.size}"); + } } } + catch(Exception exc) + { + lgError($"Eccezione in lettura variabile | item: {item.Key} | mem: {item.Value.memAddr}{Environment.NewLine}{exc}"); + } } return readErrorList;