From 66ff8065cde453b1dda65180aaf4f860875b3ada Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Mon, 18 Nov 2019 12:52:17 +0100 Subject: [PATCH] nuovi metodi conversione word/dword --- IOB-WIN/IobSiemens.cs | 163 +++++++++++++++++++++++++++++-- IOB-WIN/IobSiemensIngenia.cs | 2 +- IOB-WIN/IobSiemensPressoilCei.cs | 20 +++- 3 files changed, 174 insertions(+), 11 deletions(-) diff --git a/IOB-WIN/IobSiemens.cs b/IOB-WIN/IobSiemens.cs index 4bc8316f..fa659335 100644 --- a/IOB-WIN/IobSiemens.cs +++ b/IOB-WIN/IobSiemens.cs @@ -98,10 +98,10 @@ namespace IOB_WIN return answ; } /// - /// Converte direttamente un valore Int su un oggetto Word=byte[2] + /// Converte direttamente un valore Short su un oggetto byte[2] /// /// valore da scrivere - public byte[] wordToByte(string valore) + public byte[] intToByte(string valore) { byte[] answ = new byte[2]; try @@ -119,7 +119,49 @@ namespace IOB_WIN return answ; } /// - /// Converte direttamente un valore Int su un oggetto DWord=byte[2] + /// Converte direttamente un valore Int su un oggetto byte[4] + /// + /// valore da scrivere + public byte[] dintToByte(string valore) + { + byte[] answ = new byte[4]; + try + { + int valInt = 0; + int.TryParse(valore, out valInt); + byte[] strByte = S7.Net.Types.DInt.ToByteArray(valInt); + int byteLen = 4; + Buffer.BlockCopy(strByte, 0, answ, 0, byteLen); + } + catch (Exception exc) + { + lgError($"Errore in gestione scrittura DINT {valore} in byte{Environment.NewLine}{exc}"); + } + return answ; + } + /// + /// Converte direttamente un valore UInt16 su un oggetto byte[2] + /// + /// valore da scrivere + public byte[] wordToByte(string valore) + { + byte[] answ = new byte[2]; + try + { + ushort valInt = 0; + ushort.TryParse(valore, out valInt); + byte[] strByte = S7.Net.Types.Word.ToByteArray(valInt); + int byteLen = 2; + Buffer.BlockCopy(strByte, 0, answ, 0, byteLen); + } + catch (Exception exc) + { + lgError($"Errore in gestione scrittura INT {valore} in byte{Environment.NewLine}{exc}"); + } + return answ; + } + /// + /// Converte direttamente un valore UInt32 su un oggetto byte[4] /// /// valore da scrivere public byte[] dwordToByte(string valore) @@ -127,9 +169,9 @@ namespace IOB_WIN byte[] answ = new byte[4]; try { - short valInt = 0; - short.TryParse(valore, out valInt); - byte[] strByte = S7.Net.Types.DInt.ToByteArray(valInt); + ushort valInt = 0; + ushort.TryParse(valore, out valInt); + byte[] strByte = S7.Net.Types.DWord.ToByteArray(valInt); int byteLen = 4; Buffer.BlockCopy(strByte, 0, answ, 0, byteLen); } @@ -288,6 +330,94 @@ namespace IOB_WIN } } /// + /// Salvo in memblock il valore DInt indicato con formattazione siemens + /// + /// Blocco memoria come byte[] dove scrivere + /// Posizione inizio scrittura + /// Valore da scrivere + public void saveWordOnMemBlock(ref byte[] MemBlock, int startPos, string valore) + { + try + { + byte[] stringPar = new byte[4]; + int valInt = 0; + int.TryParse(valore, out valInt); + byte[] strByte = S7.Net.Types.DInt.ToByteArray(valInt); + int byteLen = 4; + Buffer.BlockCopy(strByte, 0, MemBlock, startPos, byteLen); + //var verifica = S7.Net.Types.String.FromByteArray(MemBlock); + } + catch (Exception exc) + { + lgError($"Errore in gestione scrittura DINT {valore} alla posizione {startPos} byte{Environment.NewLine}{exc}"); + } + } + /// + /// Salvo in memblock il valore DInt indicato con formattazione siemens + /// + /// Blocco memoria come byte[] dove scrivere + /// Valore da scrivere + /// Posizione inizio scrittura + public void saveWordOnMemBlock(ref byte[] MemBlock, string stringKey, int startPos) + { + if (currProdData.ContainsKey(stringKey)) + { + try + { + string valore = currProdData[stringKey]; + saveDIntOnMemBlock(ref MemBlock, startPos, valore); + } + catch (Exception exc) + { + lgError($"Errore in gestione scrittura DINT {stringKey}{Environment.NewLine}{exc}"); + } + } + } + /// + /// Salvo in memblock il valore DInt indicato con formattazione siemens + /// + /// Blocco memoria come byte[] dove scrivere + /// Posizione inizio scrittura + /// Valore da scrivere + public void saveDWordOnMemBlock(ref byte[] MemBlock, int startPos, string valore) + { + try + { + byte[] stringPar = new byte[4]; + int valInt = 0; + int.TryParse(valore, out valInt); + byte[] strByte = S7.Net.Types.DInt.ToByteArray(valInt); + int byteLen = 4; + Buffer.BlockCopy(strByte, 0, MemBlock, startPos, byteLen); + //var verifica = S7.Net.Types.String.FromByteArray(MemBlock); + } + catch (Exception exc) + { + lgError($"Errore in gestione scrittura DINT {valore} alla posizione {startPos} byte{Environment.NewLine}{exc}"); + } + } + /// + /// Salvo in memblock il valore DInt indicato con formattazione siemens + /// + /// Blocco memoria come byte[] dove scrivere + /// Valore da scrivere + /// Posizione inizio scrittura + public void saveDWordOnMemBlock(ref byte[] MemBlock, string stringKey, int startPos) + { + if (currProdData.ContainsKey(stringKey)) + { + try + { + string valore = currProdData[stringKey]; + saveDIntOnMemBlock(ref MemBlock, startPos, valore); + } + catch (Exception exc) + { + lgError($"Errore in gestione scrittura DINT {stringKey}{Environment.NewLine}{exc}"); + } + } + } + /// /// Salvo in memblock il valore stringa indicato con formattazione siemens /// /// Blocco memoria come byte[] dove scrivere @@ -1203,6 +1333,7 @@ namespace IOB_WIN { memAddrWrite = ""; int valInt = 0; + uint valUInt = 0; // cerco in area memMapWrite... if (memMap.mMapWrite.ContainsKey(item.uid)) { @@ -1230,6 +1361,14 @@ namespace IOB_WIN valInt = getScaledInt(currMem); saveDIntOnMemBlock(ref MemBlock, 0, valInt.ToString()); break; + case plcDataType.Word: + valUInt = getScaledUInt(currMem); + saveWordOnMemBlock(ref MemBlock, 0, valInt.ToString()); + break; + case plcDataType.DWord: + valUInt = getScaledUInt(currMem); + saveDWordOnMemBlock(ref MemBlock, 0, valInt.ToString()); + break; case plcDataType.Real: saveRealOnMemBlock(ref MemBlock, 0, currMem.value); break; @@ -1290,6 +1429,18 @@ namespace IOB_WIN return valInt; } + private static uint getScaledUInt(dataConf currMem) + { + uint valUInt; + // prima faccio eventuale fattore di scala... + uint.TryParse(currMem.value, out valUInt); + if (currMem.factor > 1) + { + valUInt = valUInt * (uint)currMem.factor; + } + + return valUInt; + } /// /// Metodo dispose x il currPLC contenuto diff --git a/IOB-WIN/IobSiemensIngenia.cs b/IOB-WIN/IobSiemensIngenia.cs index f01697a4..d815347e 100644 --- a/IOB-WIN/IobSiemensIngenia.cs +++ b/IOB-WIN/IobSiemensIngenia.cs @@ -144,7 +144,7 @@ namespace IOB_WIN case taskType.sendWatchDogMes2Plc: int valore = counterMes2Plc; saveProdData(item); - MemBlock = dwordToByte(valore.ToString()); + MemBlock = dintToByte(valore.ToString()); memAddrWrite = "DB700.DBB12"; taskVal = $"VALUE DB700.DBB12 --> {counterMes2Plc}"; break; diff --git a/IOB-WIN/IobSiemensPressoilCei.cs b/IOB-WIN/IobSiemensPressoilCei.cs index fb9e6127..611a8aaf 100644 --- a/IOB-WIN/IobSiemensPressoilCei.cs +++ b/IOB-WIN/IobSiemensPressoilCei.cs @@ -83,17 +83,29 @@ namespace IOB_WIN { saveStringOnMemBlock(ref MemBlock, item.Key, 0, byteSize); } + else if (currMem.tipoMem == plcDataType.Int) + { + short valDInt = 0; + short.TryParse(item.Value, out valDInt); + MemBlock = S7.Net.Types.Int.ToByteArray(valDInt); + } else if (currMem.tipoMem == plcDataType.DInt) { int valDInt = 0; int.TryParse(item.Value, out valDInt); MemBlock = S7.Net.Types.DInt.ToByteArray(valDInt); } - else if (currMem.tipoMem == plcDataType.Int) + else if (currMem.tipoMem == plcDataType.Word) { - short valDInt = 0; - short.TryParse(item.Value, out valDInt); - MemBlock = S7.Net.Types.Int.ToByteArray(valDInt); + ushort valDInt = 0; + ushort.TryParse(item.Value, out valDInt); + MemBlock = S7.Net.Types.Word.ToByteArray(valDInt); + } + else if (currMem.tipoMem == plcDataType.DWord) + { + uint valDInt = 0; + uint.TryParse(item.Value, out valDInt); + MemBlock = S7.Net.Types.DWord.ToByteArray(valDInt); } } taskVal = item.Value;