diff --git a/Test-S7.sln b/Test-S7.sln index 6a1ff6b4..751a4507 100644 --- a/Test-S7.sln +++ b/Test-S7.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.27004.2009 +VisualStudioVersion = 15.0.27130.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test-S7", "Test-S7\Test-S7.csproj", "{A0168CBE-9DA5-4E41-82FF-AFD39C982717}" EndProject diff --git a/Test-S7/App.config b/Test-S7/App.config index 731f6de6..5ebab194 100644 --- a/Test-S7/App.config +++ b/Test-S7/App.config @@ -3,4 +3,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Test-S7/DATA/CONF/MMapR.map b/Test-S7/DATA/CONF/MMapR.map new file mode 100644 index 00000000..6eec3133 --- /dev/null +++ b/Test-S7/DATA/CONF/MMapR.map @@ -0,0 +1,36 @@ +# Commenti con cancelletto, struttura un variabile per riga, tipo chiave|valore (occhio che il separatore è configurato da .cofig come "charSep"); spazi e tabulazioni dovrei trimmarli in acquisizione (qui inseriti per comodità di lettura) +# Segnali BIT per gestione MAPO-IOB-WIN "base" +0.0|IOB_POWER_ON |BIT +0.1|IOB_RUN |BIT +0.2|IOB_COUNT |BIT +0.3|IOB_ALARM |BIT +0.4|IOB_MANUAL |BIT +# segnali BIT x ACK +0.5|ACK_STR |BIT +0.6|ACK_ST_COM |BIT +0.7|ACK_END_COM |BIT +#bit x definizione ultimo pezzo (OK/SCARTO/RILAVORAZIONE) +1.0|LAST_PZ_OK |BIT +1.1|LAST_PZ_KO |BIT +1.2|LAST_PZ_RIL |BIT +# Vettori allarmi (banchi da 32) +002|ALARMS_001 |4BYTE +006|ALARMS_033 |4BYTE +010|ALARMS_065 |4BYTE +014|ALARMS_097 |4BYTE +# Altro valori byte numerici (0..255) +018|AUTO_POWER_OFF |BYTE +019|OVR_SPEED |BYTE +020|OVR_FEED |BYTE +021|CURR_MODE |BYTE +# valori word come UINT 16bit/Word +022|COUNT_TOT |WORD +024|RPM_PEZZO |WORD +026|RPM_MOLA |WORD +028|NUM_PZ_STOP |WORD +030|MIN_TEO_STOP |WORD +032|LOAD_PEZZO |WORD +034|LOAD_MOLA |WORD +036|TC_LAST_PZ |REAL +040|MIS_H1_LAST_PZ |REAL +044|MIS_H2_LAST_PZ |REAL \ No newline at end of file diff --git a/Test-S7/DATA/CONF/MMapW.map b/Test-S7/DATA/CONF/MMapW.map new file mode 100644 index 00000000..cdcdfcbb --- /dev/null +++ b/Test-S7/DATA/CONF/MMapW.map @@ -0,0 +1,27 @@ +# Commenti con cancelletto, struttura un variabile per riga, tipo chiave|valore (occhio che il separatore è configurato da .cofig come "charSep"); spazi e tabulazioni dovrei trimmarli in acquisizione (qui inseriti per comodità di lettura) +# Segnali BIT per gestione richeista lettura valori +0.0|SIG_STR |BIT +0.1|SIG_ST_COM |BIT +0.2|SIG_END_COM |BIT +# valori word come UINT 16bit/Word +002|NUM_PZ_LOTTO |WORD +# dati COMMESSA +006|L_MAX_COMM |BYTE +007|L_ACT_COMM |BYTE +008|COD_COMMESSA |20CHAR +# dati ARTICOLO +006|L_MAX_ART |BYTE +007|L_ACT_ART |BYTE +008|COD_ARTICOLO |20CHAR +# dati PROGRAMMA +006|L_MAX_PROG |BYTE +007|L_ACT_PROG |BYTE +008|COD_PROGRAMMA |20CHAR +# dati MACCHINA +006|L_MAX_MACC |BYTE +007|L_ACT_MACC |BYTE +008|COD_MACCHINA |20CHAR +# dati DIRECTORY +006|L_MAX_DIR |BYTE +007|L_ACT_DIR |BYTE +008|COD_DIRECTORY |20CHAR diff --git a/Test-S7/Test-S7.csproj b/Test-S7/Test-S7.csproj index 125706f6..3b71b7bf 100644 --- a/Test-S7/Test-S7.csproj +++ b/Test-S7/Test-S7.csproj @@ -39,6 +39,7 @@ ..\packages\S7netplus.0.1.7\lib\net45\S7.Net.dll + @@ -63,6 +64,7 @@ + TestMainForm.cs @@ -82,6 +84,12 @@ Always + + Always + + + Always + Designer diff --git a/Test-S7/TestMainForm.cs b/Test-S7/TestMainForm.cs index 2ea97b0f..ef45eb2d 100644 --- a/Test-S7/TestMainForm.cs +++ b/Test-S7/TestMainForm.cs @@ -17,6 +17,18 @@ namespace Test_S7 { public partial class TestMainForm : Form { + /// + /// Configurazione valori da LEGGERE dal PLC + /// + public otherData[] memMapR; + /// + /// Configurazione valori da SCRIVERE nel PLC + /// + public otherData[] memMapW; + /// + /// Byte dimensione buffer dati memoria (da file map) + /// + public int numByte = 0; /// /// Lungh massima stringhe /// @@ -113,10 +125,31 @@ namespace Test_S7 } needRefresh = false; } + // carico conf vettore memoria... + loadMemConf(); + // mostra output showOut(titolo, contenuto); } } /// + /// Caricamento conf memoria DB del SIEMENS + /// + private void loadMemConf() + { + // carico conf memoria + utils.loadConfFile(ref memMapR, filePath("MMapR"), 1, ref numByte); + utils.loadConfFile(ref memMapW, filePath("MMapW"), 1, ref numByte); + } + /// + /// Restituisce path completo file da chaive configurazione + /// + /// chaive conf x file richiesto + /// + protected string filePath(string keyFile) + { + return string.Format(@"{0}\{1}", utils.confDir, utils.CRS(keyFile)); + } + /// /// Esecuzione lettura! /// private void eseguiLetturaByte() diff --git a/Test-S7/utils.cs b/Test-S7/utils.cs new file mode 100644 index 00000000..b7a506e1 --- /dev/null +++ b/Test-S7/utils.cs @@ -0,0 +1,266 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Configuration; +using NLog; +using System.Windows.Forms; + +namespace Test_S7 +{ + public class utils + { + /// + /// wrapper di log + /// + public static Logger lg; + /// + /// folder archiviazione dati configurazione (DATA\CONF) + /// + public static string confDir + { + get + { + return string.Format(@"{0}\{1}", Application.StartupPath, CRS("dataConfPath")); + } + } + /// + /// legge conf in formato char + /// + /// + /// + public static char CRC(string key) + { + char answ = '-'; + try + { + answ = ConfigurationManager.AppSettings[key].ToCharArray()[0]; + } + catch + { } + return answ; + } + /// + /// legge conf in formato stringa + /// + /// + /// + public static string CRS(string key) + { + string answ = ""; + try + { + answ = ConfigurationManager.AppSettings[key].ToString(); + } + catch + { } + return answ; + } + /// + /// legge conf in formato INT + /// + /// + /// + public static Int32 CRI(string key) + { + int answ = 0; + try + { + answ = Convert.ToInt32(CRS(key)); + } + catch + { } + return answ; + } + /// + /// legge conf in formato BOOLean + /// + /// + /// + public static bool CRB(string key) + { + bool answ = false; + try + { + answ = Convert.ToBoolean(CRS(key)); + } + catch + { } + return answ; + } + /// + /// Decodifica file allarme + /// + /// + /// + /// tipo memoria (R/D/...) + /// indirizzo di partenza memoria + /// dimensione singolo slot in byte + /// + protected static otherData decodeOtherData(string linea, char separator, string memPre, int baseAddr, int memSize) + { + string[] valori = linea.Split(separator); + int shift = 0; + try + { + shift = Convert.ToInt32(valori[0]) - 1; + } + catch + { } + string memAddr = string.Format("{0}{1}", memPre, baseAddr + shift * memSize); + return new otherData(valori[0], memAddr, valori[1].Trim(), valori[2].Trim()); + } + /// + /// Decodifica file MAP (caso ESA/IOT) + /// + /// + /// + /// indirizzo Byte: indirizzo di partenza memoria + /// dimensione singolo slot in byte + /// indirizzo bit: numero riga x calcolo indice bit + /// + protected static otherData decodeBitData(string linea, char separator, int ByteNum, int memSize, int BitNum) + { + string[] valori = linea.Split(separator); + int shift = 0; + try + { + shift = Convert.ToInt32(valori[0]) - 1; + } + catch + { } + int resto = 0; + Math.DivRem(BitNum, 8, out resto); + string memAddr = string.Format("{0}.{1}", ByteNum + shift * memSize, resto); + return new otherData(valori[0], memAddr, valori[1].Trim(), valori[2].Trim()); + } + /// + /// Decodifica file MAP (caso FANUC/OSAI/...) + /// + /// + /// + /// tipo memoria (R/D/...) + /// indirizzo Byte: indirizzo di partenza memoria + /// dimensione singolo slot in byte + /// indirizzo bit: numero riga x calcolo indice bit + /// + protected static otherData decodeBitData(string linea, char separator, string memPre, int baseAddr, int memSize, int numRiga) + { + string[] valori = linea.Split(separator); + int shift = 0; + try + { + shift = (Convert.ToInt32(valori[0]) - 1) / (8 * memSize); + } + catch + { } + int resto = 0; + Math.DivRem(numRiga, 8 * memSize, out resto); + string memAddr = string.Format("{0}{1}.{2}", memPre, baseAddr + shift, resto); + return new otherData(valori[0], memAddr, valori[1].Trim(), valori[2].Trim()); + } + /// + /// Legge il file di conf di una MAP di informazioni da gestire con lettura set memoria + /// + /// nome vettore memoria + /// file origine + /// dimensione (in byte) della memoria + /// dimensione (in byte) della memoria + public static void loadConfFile(ref otherData[] vettoreConf, string nomeFile, int memSize, ref int numVett) + { + otherData lastData = new otherData(); + int totRighe = 0; + string linea; + totRighe = File.ReadLines(nomeFile).Count(); + // creo un vettore della dimensione corretta... conta anche commenti tanto poi riduco... + vettoreConf = new otherData[File.ReadLines(nomeFile).Count()]; + // carica da file... + StreamReader file = new StreamReader(nomeFile); + // leggo 1 linea alla volta... + int numRiga = 0; + int bitNum = 0; + int byteNum = 0; + while ((linea = file.ReadLine()) != null) + { + // SE non è un commento... + if (linea.Substring(0, 1) != "#") + { + // se finisce per BIT allora processo bit-a-bit... + if (linea.EndsWith("BOOL")) + { + try + { + string[] memIdx = linea.Split(utils.CRC("charSep"))[0].Split('.'); + // calcolo bit e byte number... + int.TryParse(memIdx[0], out byteNum); + if (memIdx.Length > 1) + { + int.TryParse(memIdx[1], out bitNum); + } + else + { + bitNum = 0; + } + } + catch + { + byteNum = 0; + bitNum = 0; + } + lastData = decodeBitData(linea, utils.CRC("charSep"), byteNum, 1, bitNum); + vettoreConf[numRiga] = lastData; + } + else + { + lastData = decodeOtherData(linea, utils.CRC("charSep"), "", 1, memSize); + vettoreConf[numRiga] = lastData; + } + numRiga++; + } + } + // salvo lunghezza file... + try + { + numVett = Convert.ToInt32(lastData.memAddr) + 1; + } + catch + { + numVett = numRiga + 1; + } + // chiudo file + file.Close(); + // ora trimmo vettore al solo numero VERO dei valori caricati... + Array.Resize(ref vettoreConf, numRiga); + + if (utils.CRB("verbose")) lg.Info(string.Format("Fine caricamento vettore di {0} variabili per file {1}", numRiga, nomeFile)); + } + } + + /// + /// Dato generico (per decodifica) + /// + public class otherData + { + public string codNum; + public string memAddr; + public string varName; + public string dataType; + public otherData() + { + codNum = ""; + memAddr = ""; + varName = ""; + dataType = ""; + } + public otherData(string _codNum, string _memAddr, string _varName, string _dataType) + { + codNum = _codNum; + memAddr = _memAddr; + varName = _varName; + dataType = _dataType; + } + } + +}