171 lines
5.5 KiB
C#
171 lines
5.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 logging
|
|
/// </summary>
|
|
public static Logger lg;
|
|
|
|
public TestMainForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
myInit();
|
|
}
|
|
/// <summary>
|
|
/// inizializzo
|
|
/// </summary>
|
|
private void myInit()
|
|
{
|
|
lg = LogManager.GetCurrentClassLogger();
|
|
}
|
|
|
|
private void eseguiLettura()
|
|
{
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
// inizializzo
|
|
CpuType tipoCpu = CpuType.S7300;
|
|
short slot = 0;
|
|
short rack = 0;
|
|
string titolo = "";
|
|
string contenuto = "";
|
|
txtOut.Text = "";
|
|
// leggo selezione...
|
|
try
|
|
{
|
|
short.TryParse(txtSlot.Text, out slot);
|
|
short.TryParse(txtRack.Text, out rack);
|
|
tipoCpu = (CpuType)Enum.Parse(typeof(CpuType), cbCpuType.SelectedItem.ToString());
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lg.Error(exc, "Errore in parse parametri");
|
|
}
|
|
if (txtIP.Text != "" && rack >= 0 && slot >= 0)
|
|
{
|
|
using (var plc = new Plc(tipoCpu, txtIP.Text, rack, slot))
|
|
{
|
|
// test ping!!!
|
|
Ping pingSender = new Ping();
|
|
IPAddress address = IPAddress.Loopback;
|
|
IPAddress.TryParse(txtIP.Text, 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
|
|
{
|
|
// provo ad aprire connessione
|
|
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}", address, Environment.NewLine);
|
|
contenuto += string.Format("CPU: {0}{1}", tipoCpu, Environment.NewLine);
|
|
contenuto += string.Format("RACK: {0}{1}", rack, Environment.NewLine);
|
|
contenuto += string.Format("SLOT: {0}{1}", slot, Environment.NewLine);
|
|
showOut(titolo, contenuto);
|
|
// decodifico memoria...
|
|
memAddress memoria = new memAddress(txtMemArea.Text);
|
|
int numByte = 1;
|
|
int.TryParse(txtMemSize.Text, out numByte);
|
|
Byte[] fullMem = 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 < fullMem.Length; i++)
|
|
{
|
|
byteVal = Convert.ToString(fullMem[i], 2).PadLeft(8, '0');
|
|
contenuto += string.Format("B{0:000}: {1} | {3}{4}", i, byteVal, fullMem[i], Environment.NewLine);
|
|
}
|
|
showOut(titolo, contenuto);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
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:yyyy.MM.dd HH:mm:ss}{2}", title, DateTime.Now, 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)
|
|
{
|
|
eseguiLettura();
|
|
}
|
|
}
|
|
|
|
}
|