223 lines
6.9 KiB
C#
223 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using S7.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net;
|
|
using NLog;
|
|
using System.Diagnostics;
|
|
|
|
namespace Test_S7
|
|
{
|
|
public partial class TestMainForm : Form
|
|
{
|
|
/// <summary>
|
|
/// Oggetto cronometro x test vari...
|
|
/// </summary>
|
|
protected Stopwatch sw = new Stopwatch();
|
|
/// <summary>
|
|
/// parametri di connessione
|
|
/// </summary>
|
|
protected connParam parametri;
|
|
/// <summary>
|
|
/// titolo x log/debug
|
|
/// </summary>
|
|
protected string titolo = "";
|
|
/// <summary>
|
|
/// contenuto x log/debug
|
|
/// </summary>
|
|
protected string contenuto = "";
|
|
/// <summary>
|
|
/// oggetto logging
|
|
/// </summary>
|
|
public static Logger lg;
|
|
|
|
public TestMainForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
myInit();
|
|
}
|
|
/// <summary>
|
|
/// inizializzo
|
|
/// </summary>
|
|
private void myInit()
|
|
{
|
|
lg = LogManager.GetCurrentClassLogger();
|
|
// inizializzo parametri...
|
|
parametri = new connParam()
|
|
{
|
|
ipAdrr = "127.0.0.1",
|
|
tipoCpu = CpuType.S7200,
|
|
slot = 0,
|
|
rack = 0
|
|
};
|
|
setParamPlc();
|
|
}
|
|
/// <summary>
|
|
/// Imposto parametri PLC
|
|
/// </summary>
|
|
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");
|
|
}
|
|
}
|
|
|
|
private void eseguiLettura()
|
|
{
|
|
//Stopwatch sw = new Stopwatch();
|
|
sw.Restart();
|
|
// inizializzo
|
|
//if (parametri.ipAdrr != "" && parametri.rack >= 0 && parametri.slot >= 0)
|
|
//{
|
|
// test ping!!!
|
|
Ping pingSender = new Ping();
|
|
IPAddress address = IPAddress.Loopback;
|
|
IPAddress.TryParse(parametri.ipAdrr, out address);
|
|
PingReply reply = pingSender.Send(address, 100);
|
|
// se passa il ping faccio il resto...
|
|
if (reply.Status != IPStatus.Success)
|
|
{
|
|
titolo = "Errore ping";
|
|
contenuto = string.Format("Reply Status per {0}: {1}", address, reply.Status);
|
|
showOut(titolo, contenuto);
|
|
}
|
|
else
|
|
{
|
|
using (var plc = new Plc(parametri.tipoCpu, parametri.ipAdrr, parametri.rack, parametri.slot))
|
|
{
|
|
//plc.ClearLastError();
|
|
plc.Open();
|
|
if (!plc.IsAvailable)
|
|
{
|
|
|
|
titolo = "Errore Disponibilità";
|
|
contenuto = string.Format("{0} | {1}", plc.LastErrorCode, plc.LastErrorString);
|
|
plc.ClearLastError();
|
|
showOut(titolo, contenuto);
|
|
}
|
|
else
|
|
{
|
|
if (!plc.IsConnected)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
plc.Close();
|
|
}
|
|
}
|
|
sw.Stop();
|
|
tslRTime.Text = string.Format("{0}", sw.Elapsed);
|
|
}
|
|
/// <summary>
|
|
/// formatta un numero in forma binaria 0/1 a 32 bit (4 byte)
|
|
/// </summary>
|
|
/// <param name="valore"></param>
|
|
/// <returns></returns>
|
|
public static string binaryForm(int valore)
|
|
{
|
|
string answ = "";
|
|
try
|
|
{
|
|
answ = string.Format(new BinaryFormatter(), "{0:B}", valore);
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
protected void showOut(string title, string content)
|
|
{
|
|
string outText = "";
|
|
// a video
|
|
outText += string.Format("{0}--------------------------------------------------------------------------------------{0}", Environment.NewLine);
|
|
outText += string.Format("- {0}{1}", title, Environment.NewLine);
|
|
outText += string.Format("--------------------------------------------------------------------------------------{0}", Environment.NewLine);
|
|
outText += string.Format("{0}{1}", content, Environment.NewLine);
|
|
outText += string.Format("--------------------------------------------------------------------------------------{0}{0}", Environment.NewLine);
|
|
|
|
// aggiorno visualizzazione
|
|
txtOut.Text += outText;
|
|
// loggo!
|
|
lg.Info(outText);
|
|
}
|
|
|
|
private void btnRead_Click(object sender, EventArgs e)
|
|
{
|
|
setParamPlc();
|
|
eseguiLettura();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Scrivo memoria tipo NUM
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void btnNumWrite_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
/// <summary>
|
|
/// Scrivo memoria tipo STRING
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void btnStrWrite_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
}
|