From 2dbce4bae2269cdc8f26cce444564b0c66c53b97 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Wed, 29 Nov 2017 18:36:42 +0100 Subject: [PATCH] Ok nuova versione lettura... --- Test-S7/NLog.config | 19 +++-- Test-S7/TestMainForm.Designer.cs | 18 ++--- Test-S7/TestMainForm.cs | 115 +++++++++++++++---------------- 3 files changed, 72 insertions(+), 80 deletions(-) diff --git a/Test-S7/NLog.config b/Test-S7/NLog.config index a55caaa4..ba967607 100644 --- a/Test-S7/NLog.config +++ b/Test-S7/NLog.config @@ -5,22 +5,19 @@ autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> - - - - + + - diff --git a/Test-S7/TestMainForm.Designer.cs b/Test-S7/TestMainForm.Designer.cs index 2cbcea44..afeeb793 100644 --- a/Test-S7/TestMainForm.Designer.cs +++ b/Test-S7/TestMainForm.Designer.cs @@ -157,7 +157,7 @@ this.groupBox1.Controls.Add(this.txtMemSize); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.label2); - this.groupBox1.Location = new System.Drawing.Point(466, 12); + this.groupBox1.Location = new System.Drawing.Point(468, 12); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(386, 105); this.groupBox1.TabIndex = 8; @@ -193,9 +193,9 @@ | System.Windows.Forms.AnchorStyles.Right))); this.groupBox2.AutoSize = true; this.groupBox2.Controls.Add(this.txtOut); - this.groupBox2.Location = new System.Drawing.Point(12, 185); + this.groupBox2.Location = new System.Drawing.Point(12, 189); this.groupBox2.Name = "groupBox2"; - this.groupBox2.Size = new System.Drawing.Size(843, 624); + this.groupBox2.Size = new System.Drawing.Size(842, 402); this.groupBox2.TabIndex = 9; this.groupBox2.TabStop = false; this.groupBox2.Text = "Risultati"; @@ -209,7 +209,7 @@ this.txtOut.Multiline = true; this.txtOut.Name = "txtOut"; this.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; - this.txtOut.Size = new System.Drawing.Size(837, 603); + this.txtOut.Size = new System.Drawing.Size(836, 381); this.txtOut.TabIndex = 0; this.txtOut.Text = "..."; // @@ -295,9 +295,9 @@ this.toolStripStatusLabel1, this.toolStripProgressBar1, this.tslRTime}); - this.statusStrip1.Location = new System.Drawing.Point(0, 812); + this.statusStrip1.Location = new System.Drawing.Point(0, 599); this.statusStrip1.Name = "statusStrip1"; - this.statusStrip1.Size = new System.Drawing.Size(867, 25); + this.statusStrip1.Size = new System.Drawing.Size(869, 25); this.statusStrip1.TabIndex = 11; this.statusStrip1.Text = "statusStrip1"; // @@ -326,6 +326,8 @@ // // groupBox4 // + this.groupBox4.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); this.groupBox4.Controls.Add(this.btnStrWrite); this.groupBox4.Controls.Add(this.btnNumWrite); this.groupBox4.Controls.Add(this.txtWriteVal2); @@ -338,7 +340,7 @@ this.groupBox4.Controls.Add(this.label7); this.groupBox4.Location = new System.Drawing.Point(12, 117); this.groupBox4.Name = "groupBox4"; - this.groupBox4.Size = new System.Drawing.Size(840, 71); + this.groupBox4.Size = new System.Drawing.Size(842, 71); this.groupBox4.TabIndex = 12; this.groupBox4.TabStop = false; this.groupBox4.Text = "Memoria: WRITE param"; @@ -439,7 +441,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(867, 837); + this.ClientSize = new System.Drawing.Size(869, 624); this.Controls.Add(this.groupBox4); this.Controls.Add(this.statusStrip1); this.Controls.Add(this.groupBox3); diff --git a/Test-S7/TestMainForm.cs b/Test-S7/TestMainForm.cs index 71d995a6..6b62a468 100644 --- a/Test-S7/TestMainForm.cs +++ b/Test-S7/TestMainForm.cs @@ -17,10 +17,6 @@ namespace Test_S7 { public partial class TestMainForm : Form { - /// - /// Ogetto plc impiegato... - /// - protected Plc currPLC; /// /// Oggetto cronometro x test vari... /// @@ -29,7 +25,14 @@ namespace Test_S7 /// parametri di connessione /// protected connParam parametri; - + /// + /// titolo x log/debug + /// + protected string titolo = ""; + /// + /// contenuto x log/debug + /// + protected string contenuto = ""; /// /// oggetto logging /// @@ -62,36 +65,33 @@ namespace Test_S7 /// private void setParamPlc() { + txtOut.Text = ""; try { short.TryParse(txtSlot.Text, out parametri.slot); short.TryParse(txtRack.Text, out parametri.rack); parametri.tipoCpu = (CpuType)Enum.Parse(typeof(CpuType), cbCpuType.SelectedItem.ToString()); parametri.ipAdrr = txtIP.Text.Trim(); + titolo = "PARAM PLC"; + contenuto = string.Format("IP: {0}{1}", parametri.ipAdrr, Environment.NewLine); + contenuto += string.Format("CPU: {0}{1}", parametri.tipoCpu, Environment.NewLine); + contenuto += string.Format("RACK: {0}{1}", parametri.rack, Environment.NewLine); + contenuto += string.Format("SLOT: {0}", parametri.slot, Environment.NewLine); + showOut(titolo, contenuto); } catch (Exception exc) { lg.Error(exc, "Errore in parse parametri"); } - if (parametri.ipAdrr != "" && parametri.rack >= 0 && parametri.slot >= 0) - { - // inizializzo il PLC... - currPLC = new Plc(parametri.tipoCpu, parametri.ipAdrr, parametri.rack, parametri.slot); - } } private void eseguiLettura() { //Stopwatch sw = new Stopwatch(); - sw.Start(); + sw.Restart(); // inizializzo - string titolo = ""; - string contenuto = ""; - txtOut.Text = ""; //if (parametri.ipAdrr != "" && parametri.rack >= 0 && parametri.slot >= 0) //{ - // using (var plc = new Plc(tipoCpu, txtIP.Text, rack, slot)) - // { // test ping!!! Ping pingSender = new Ping(); IPAddress address = IPAddress.Loopback; @@ -106,64 +106,57 @@ namespace Test_S7 } else { - currPLC.Open(); - if (!currPLC.IsAvailable) + using (var plc = new Plc(parametri.tipoCpu, parametri.ipAdrr, parametri.rack, parametri.slot)) { - titolo = "Errore Disponibilità"; - contenuto = string.Format("{0} | {1}", currPLC.LastErrorCode, currPLC.LastErrorString); - currPLC.ClearLastError(); - showOut(titolo, contenuto); - } - else - { - if (!currPLC.IsConnected) + //plc.ClearLastError(); + plc.Open(); + if (!plc.IsAvailable) { - titolo = "Errore connessione"; - contenuto = string.Format("{0} | {1}", currPLC.LastErrorCode, currPLC.LastErrorString); - currPLC.ClearLastError(); + + titolo = "Errore Disponibilità"; + contenuto = string.Format("{0} | {1}", plc.LastErrorCode, plc.LastErrorString); + plc.ClearLastError(); showOut(titolo, contenuto); - tslConn.Text = "NO Connection"; } else { - tslConn.Text = "Connection OK"; - // inizio riportando dati connessione... - titolo = "CONNESSIONE AVVENUTA"; - contenuto = string.Format("IP: {0}{1}", address, Environment.NewLine); - contenuto += string.Format("CPU: {0}{1}", parametri.tipoCpu, Environment.NewLine); - contenuto += string.Format("RACK: {0}{1}", parametri.rack, Environment.NewLine); - contenuto += string.Format("SLOT: {0}{1}", parametri.slot, Environment.NewLine); - showOut(titolo, contenuto); - // decodifico memoria... - memAddress memoria = new memAddress(txtMemArea.Text); - int numByte = 1; - int.TryParse(txtMemSize.Text, out numByte); - // se sono + di 200 byte leggo 200 alla volta... - int startAddr = 0; - int memSize = 0; - if (numByte > 200) + if (!plc.IsConnected) { - while (startAddr + memSize < numByte) + titolo = "Errore connessione"; + contenuto = string.Format("{0} | {1}", plc.LastErrorCode, plc.LastErrorString); + plc.ClearLastError(); + showOut(titolo, contenuto); + tslConn.Text = "NO Connection"; + } + else + { + tslConn.Text = "Connection OK"; + // inizio riportando dati connessione... + titolo = "CONNESSIONE AVVENUTA"; + contenuto = string.Format("IP: {0}{1}", parametri.ipAdrr, Environment.NewLine); + contenuto += string.Format("CPU: {0}{1}", parametri.tipoCpu, Environment.NewLine); + contenuto += string.Format("RACK: {0}{1}", parametri.rack, Environment.NewLine); + contenuto += string.Format("SLOT: {0}", parametri.slot, Environment.NewLine); + showOut(titolo, contenuto); + // decodifico memoria... + memAddress memoria = new memAddress(txtMemArea.Text); + int numByte = 1; + int.TryParse(txtMemSize.Text, out numByte); + Byte[] memByteRead = plc.ReadBytes(DataType.DataBlock, memoria.DbNum, memoria.indiceMem, numByte); + titolo = string.Format("READ BLOCK MEM: {0} --> {1} byte", txtMemArea.Text, numByte); + contenuto = ""; + string byteVal = ""; + for (int i = 0; i < memByteRead.Length; i++) { - + byteVal = Convert.ToString(memByteRead[i], 2).PadLeft(8, '0'); + contenuto += string.Format("B{0:000}: {1} | {2}{3}", i, byteVal, memByteRead[i], Environment.NewLine); } + showOut(titolo, contenuto); } - Byte[] memByteRead = currPLC.ReadBytes(DataType.DataBlock, memoria.DbNum, memoria.indiceMem, numByte); - titolo = string.Format("READ BLOCK MEM: {0} --> {1} byte", txtMemArea.Text, numByte); - contenuto = ""; - string byteVal = ""; - for (int i = 0; i < memByteRead.Length; i++) - { - byteVal = Convert.ToString(memByteRead[i], 2).PadLeft(8, '0'); - contenuto += string.Format("B{0:000}: {1} | {2}{3}", i, byteVal, memByteRead[i], Environment.NewLine); - } - showOut(titolo, contenuto); } + plc.Close(); } - currPLC.Close(); } - // } - //} sw.Stop(); tslRTime.Text = string.Format("{0}", sw.Elapsed); }