From 287d2f58841c9733dacd376024a474b29f38f526 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Wed, 13 Nov 2019 12:46:52 +0100 Subject: [PATCH] Fix scrittura stringhe siemens metodo comune --- IOB-WIN/IobSiemens.cs | 81 ++++++++++++++++++++++++++++++-- IOB-WIN/IobSiemensComur.cs | 27 +++++++++-- IOB-WIN/IobSiemensLasco.cs | 8 ++-- IOB-WIN/IobSiemensPressoilCei.cs | 2 +- IOB-WIN/IobSiemensSaet.cs | 4 +- 5 files changed, 107 insertions(+), 15 deletions(-) diff --git a/IOB-WIN/IobSiemens.cs b/IOB-WIN/IobSiemens.cs index 02ab3a6a..cbcad3f1 100644 --- a/IOB-WIN/IobSiemens.cs +++ b/IOB-WIN/IobSiemens.cs @@ -32,6 +32,10 @@ namespace IOB_WIN /// protected int maxStrChar = 20; /// + /// indica se scrivere i primi byte x string siemens x indicare lung max e corrente + /// + protected bool writePre = true; + /// /// Oggetto PLC da ri-utilizzare... /// protected Plc currPLC; @@ -52,18 +56,18 @@ namespace IOB_WIN /// Salvo in memblock il valore stringa indicato con formattazione siemens /// /// Blocco memoria come byte[] dove scrivere - /// Valore stringa da scrivere + /// Nome del parametro da recuperare da prodData x scrivere /// Posizione inizio scrittura /// Lunghezza max stringa (se ci sono 2 byte iniziali verrĂ  ridotta di 2) /// Indica se scrivere i dati preliminari di lunghezza stringa max/occupata - public void saveStringOnMemBlock(ref byte[] MemBlock, string stringValue, int startPos, int totLen, bool writePre) + public void saveStringOnMemBlock(ref byte[] MemBlock, string stringKey, int startPos, int totLen) { - if (currProdData.ContainsKey(stringValue)) + if (currProdData.ContainsKey(stringKey)) { try { byte[] stringPar = new byte[2]; - string valore = currProdData[stringValue]; + string valore = currProdData[stringKey]; byte[] strByte = S7.Net.Types.String.ToByteArray(valore); int byteLen = strByte.Length <= totLen ? strByte.Length : totLen; int shiftStrByte = writePre ? 2 : 0; @@ -84,7 +88,7 @@ namespace IOB_WIN } catch (Exception exc) { - lgError($"Errore in gestione scrittura {stringValue}{Environment.NewLine}{exc}"); + lgError($"Errore in gestione scrittura {stringKey}{Environment.NewLine}{exc}"); } } } @@ -348,6 +352,7 @@ namespace IOB_WIN public IobSiemens(AdapterForm caller, IobConfiguration IOBConf) : base(caller, IOBConf) { memMap = new plcMemMap(); + writePre = true; if (IOBConf != null) { // gestione invio ritardato contapezzi @@ -958,6 +963,72 @@ namespace IOB_WIN return outVal; } /// + /// OVerride metodo x scrittura parametri su PLC + /// + /// + protected override void plcWriteParams(List updatedPar) + { + dataConf currMem = null; + int byteSize = 0; + byte[] MemBlock = new byte[1]; + string memAddrWrite = ""; + bool fatto = false; + if (updatedPar != null) + { + // controllo i parametri... ne gestisco 4... + foreach (var item in updatedPar) + { + try + { + memAddrWrite = ""; + // cerco in area memMapWrite... + if (memMap.mMapWrite.ContainsKey(item.uid)) + { + // recupero! + currMem = memMap.mMapWrite[item.uid]; + byteSize = currMem.size; + memAddrWrite = currMem.memAddr; + MemBlock = new byte[byteSize]; + // faccio preliminarmente upsertKey... + upsertKey(item.uid, currMem.value); + switch (currMem.tipoMem) + { + case plcDataType.Boolean: + break; + case plcDataType.Int: + saveIntOnMemBlock(ref MemBlock, item.uid, 0); + break; + case plcDataType.DInt: + saveDIntOnMemBlock(ref MemBlock, item.uid, 0); + break; + case plcDataType.Real: + saveRealOnMemBlock(ref MemBlock, item.uid, 0); + break; + case plcDataType.String: + saveStringOnMemBlock(ref MemBlock, item.uid, 0, currMem.value.Length); + break; + default: + break; + } + if (!string.IsNullOrEmpty(memAddrWrite)) + { + // scrivo su siemens + fatto = S7WriteBB(ref MemBlock, memAddrWrite); + } + } + else + { + lgInfo($"Errore uid non trovato in area write memory: {item.uid}, ci sono {memMap.mMapWrite.Count} in area write"); + } + } + catch (Exception exc) + { + lgError($"Eccezione in fase di plcWriteParams per item {item.uid} con valore {item.value}{Environment.NewLine}{exc}"); + } + } + } + } + /// /// Metodo dispose x il currPLC contenuto /// public void Dispose() diff --git a/IOB-WIN/IobSiemensComur.cs b/IOB-WIN/IobSiemensComur.cs index 19c153ce..84d1466b 100644 --- a/IOB-WIN/IobSiemensComur.cs +++ b/IOB-WIN/IobSiemensComur.cs @@ -109,7 +109,7 @@ namespace IOB_WIN MemBlock = new byte[byteSize]; if (currMem.tipoMem == plcDataType.String) { - saveStringOnMemBlock(ref MemBlock, item.Key, 0, byteSize, true); + saveStringOnMemBlock(ref MemBlock, item.Key, 0, byteSize); } else if (currMem.tipoMem == plcDataType.DInt) { @@ -138,14 +138,35 @@ namespace IOB_WIN MemBlock[0] = (byte)0; memAddrWrite = "DB150.DBB4"; break; + case taskType.setParameter: + // richiedo da URL i parametri WRITE da popolare + taskVal = processMemWriteRequests(); + // se restituiscce "" faccio altra prova... + if (string.IsNullOrEmpty(taskVal)) + { + // i parametri me li aspetto come stringa composta paramName|paramvalue + if (item.Value.Contains("|")) + { + string[] paramsJob = item.Value.Split('|'); + taskVal = $"REQUEST SET PARAMETERS: {paramsJob[0]} --> {paramsJob[1]}"; + } + else + { + taskVal = $"WRONG REQUEST FOR SET PARAMETERS: {item.Value} doesnt contain pipe for splitting key/value"; + } + } + break; default: taskVal = "SKIPPED | NO EXEC"; break; } // aggiungo task! taskDone.Add(item.Key, taskVal); - // scrivo comunque! - taskOk = S7WriteBB(ref MemBlock, memAddrWrite); + if (string.IsNullOrEmpty(memAddrWrite)) + { + // scrivo comunque! + taskOk = S7WriteBB(ref MemBlock, memAddrWrite); + } if (!taskOk) { lgError($"Errore in S7WriteBB durante executeTasks: {item.Key} | {item.Value}"); diff --git a/IOB-WIN/IobSiemensLasco.cs b/IOB-WIN/IobSiemensLasco.cs index c33f7355..0084feb0 100644 --- a/IOB-WIN/IobSiemensLasco.cs +++ b/IOB-WIN/IobSiemensLasco.cs @@ -192,7 +192,7 @@ namespace IOB_WIN MemBlock = new byte[byteSize]; if (currMem.tipoMem == plcDataType.String) { - saveStringOnMemBlock(ref MemBlock, item.Key, 0, byteSize, true); + saveStringOnMemBlock(ref MemBlock, item.Key, 0, byteSize); } else if (currMem.tipoMem == plcDataType.DInt) { @@ -283,9 +283,9 @@ namespace IOB_WIN taskDone.Add(item.Key, taskVal); } // controllo SE HO da scrivere articolo/commessa/programma - saveStringOnMemBlock(ref MemBlock, "setArt", 2, 22, true); - saveStringOnMemBlock(ref MemBlock, "setComm", 24, 22, true); - saveStringOnMemBlock(ref MemBlock, "setProg", 46, 22, true); + saveStringOnMemBlock(ref MemBlock, "setArt", 2, 22); + saveStringOnMemBlock(ref MemBlock, "setComm", 24, 22); + saveStringOnMemBlock(ref MemBlock, "setProg", 46, 22); // scrivo comunque! taskOk = S7WriteBB(ref MemBlock); diff --git a/IOB-WIN/IobSiemensPressoilCei.cs b/IOB-WIN/IobSiemensPressoilCei.cs index ffedfbfa..fb9e6127 100644 --- a/IOB-WIN/IobSiemensPressoilCei.cs +++ b/IOB-WIN/IobSiemensPressoilCei.cs @@ -81,7 +81,7 @@ namespace IOB_WIN MemBlock = new byte[byteSize]; if (currMem.tipoMem == plcDataType.String) { - saveStringOnMemBlock(ref MemBlock, item.Key, 0, byteSize, true); + saveStringOnMemBlock(ref MemBlock, item.Key, 0, byteSize); } else if (currMem.tipoMem == plcDataType.DInt) { diff --git a/IOB-WIN/IobSiemensSaet.cs b/IOB-WIN/IobSiemensSaet.cs index 00c863bc..81df01fb 100644 --- a/IOB-WIN/IobSiemensSaet.cs +++ b/IOB-WIN/IobSiemensSaet.cs @@ -143,14 +143,14 @@ namespace IOB_WIN case taskType.setArt: saveProdData(item); MemBlock = new byte[34]; - saveStringOnMemBlock(ref MemBlock, "setArt", 0, 32, true); + saveStringOnMemBlock(ref MemBlock, "setArt", 0, 32); memAddrWrite = "DB1275.DBB96"; taskVal = item.Value; break; case taskType.setComm: saveProdData(item); MemBlock = new byte[14]; - saveStringOnMemBlock(ref MemBlock, "setComm", 0, 12, true); + saveStringOnMemBlock(ref MemBlock, "setComm", 0, 12); memAddrWrite = "DB1275.DBB130"; taskVal = item.Value; break;