327 lines
9.5 KiB
C#
327 lines
9.5 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 PLC da ri-utilizzare...
|
|
/// </summary>
|
|
protected Plc currPLC;
|
|
/// <summary>
|
|
/// indica se serva refresh parametri e quindi PLC...
|
|
/// </summary>
|
|
bool needRefresh = true;
|
|
/// <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 = "";
|
|
// SE è necessario refresh...
|
|
if (needRefresh)
|
|
{
|
|
lg.Info("Refreshing connection...");
|
|
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 (pre connect)";
|
|
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);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lg.Error(exc, "Errore in parse parametri");
|
|
}
|
|
// ora tento avvio PLC... SE PING OK...
|
|
if (testPing() == IPStatus.Success)
|
|
{
|
|
try
|
|
{
|
|
currPLC = new Plc(parametri.tipoCpu, parametri.ipAdrr, parametri.rack, parametri.slot);
|
|
currPLC.Open();
|
|
if (currPLC.IsConnected) titolo = "CONNESSIONE AVVENUTA";
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lg.Error(exc, "Errore in INIT PLC");
|
|
}
|
|
needRefresh = false;
|
|
}
|
|
showOut(titolo, contenuto);
|
|
}
|
|
}
|
|
|
|
private void eseguiLettura()
|
|
{
|
|
sw.Restart();
|
|
IPStatus pingStatus = testPing();
|
|
// se passa il ping faccio il resto...
|
|
if (pingStatus != IPStatus.Success)
|
|
{
|
|
titolo = "Errore ping";
|
|
contenuto = string.Format("Reply Status per {0}: {1}", parametri.ipAdrr, pingStatus);
|
|
showOut(titolo, contenuto);
|
|
}
|
|
else
|
|
{
|
|
#if false
|
|
//using (var plc = new Plc(parametri.tipoCpu, parametri.ipAdrr, parametri.rack, parametri.slot))
|
|
//using (var plc = currPLC)
|
|
//{
|
|
var plc = currPLC;
|
|
if (!plc.IsConnected) 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";
|
|
|
|
|
|
// decodifico memoria...
|
|
memAddress memoria = new memAddress(txtMemArea.Text);
|
|
int numByte = 1;
|
|
int.TryParse(txtMemSize.Text, out numByte);
|
|
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);
|
|
}
|
|
#endif
|
|
|
|
|
|
if (!currPLC.IsConnected) currPLC.Open();
|
|
if (!currPLC.IsAvailable)
|
|
{
|
|
|
|
titolo = "Errore Disponibilità";
|
|
contenuto = string.Format("{0} | {1}", currPLC.LastErrorCode, currPLC.LastErrorString);
|
|
currPLC.ClearLastError();
|
|
showOut(titolo, contenuto);
|
|
}
|
|
else
|
|
{
|
|
if (!currPLC.IsConnected)
|
|
{
|
|
titolo = "Errore connessione";
|
|
contenuto = string.Format("{0} | {1}", currPLC.LastErrorCode, currPLC.LastErrorString);
|
|
currPLC.ClearLastError();
|
|
showOut(titolo, contenuto);
|
|
tslConn.Text = "NO Connection";
|
|
}
|
|
else
|
|
{
|
|
tslConn.Text = "Connection OK";
|
|
|
|
|
|
// decodifico memoria...
|
|
memAddress memoria = new memAddress(txtMemArea.Text);
|
|
int numByte = 1;
|
|
int.TryParse(txtMemSize.Text, out numByte);
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
sw.Stop();
|
|
tslRTime.Text = string.Format("{0}", sw.Elapsed);
|
|
}
|
|
/// <summary>
|
|
/// test ping all'indirizzo impostato nei parametri
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private IPStatus testPing()
|
|
{
|
|
IPStatus answ = IPStatus.Unknown; ;
|
|
IPAddress address;
|
|
PingReply reply;
|
|
Ping pingSender = new Ping();
|
|
address = IPAddress.Loopback;
|
|
IPAddress.TryParse(parametri.ipAdrr, out address);
|
|
reply = pingSender.Send(address, 100);
|
|
answ = reply.Status;
|
|
return answ;
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
|
|
}
|
|
|
|
private void txtIP_TextChanged(object sender, EventArgs e)
|
|
{
|
|
needRefresh = true;
|
|
}
|
|
|
|
private void txtRack_TextChanged(object sender, EventArgs e)
|
|
{
|
|
needRefresh = true;
|
|
}
|
|
|
|
private void cbCpuType_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
needRefresh = true;
|
|
}
|
|
|
|
private void txtSlot_TextChanged(object sender, EventArgs e)
|
|
{
|
|
needRefresh = true;
|
|
}
|
|
|
|
private void txtMemArea_TextChanged(object sender, EventArgs e)
|
|
{
|
|
needRefresh = true;
|
|
}
|
|
|
|
private void txtMemSize_TextChanged(object sender, EventArgs e)
|
|
{
|
|
needRefresh = true;
|
|
}
|
|
}
|
|
|
|
}
|