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
{
///
/// Ogetto plc impiegato...
///
protected Plc currPLC;
///
/// Oggetto cronometro x test vari...
///
protected Stopwatch sw = new Stopwatch();
///
/// parametri di connessione
///
protected connParam parametri;
///
/// oggetto logging
///
public static Logger lg;
public TestMainForm()
{
InitializeComponent();
myInit();
}
///
/// inizializzo
///
private void myInit()
{
lg = LogManager.GetCurrentClassLogger();
// inizializzo parametri...
parametri = new connParam()
{
ipAdrr = "127.0.0.1",
tipoCpu = CpuType.S7200,
slot = 0,
rack = 0
};
setParamPlc();
}
///
/// Imposto parametri PLC
///
private void setParamPlc()
{
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();
}
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();
// 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;
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
{
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";
// 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)
{
while (startAddr + memSize < 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);
}
}
currPLC.Close();
}
// }
//}
sw.Stop();
tslRTime.Text = string.Format("{0}", sw.Elapsed);
}
///
/// formatta un numero in forma binaria 0/1 a 32 bit (4 byte)
///
///
///
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();
}
///
/// Scrivo memoria tipo NUM
///
///
///
private void btnNumWrite_Click(object sender, EventArgs e)
{
}
///
/// Scrivo memoria tipo STRING
///
///
///
private void btnStrWrite_Click(object sender, EventArgs e)
{
}
}
}