ok nuovi parametri e gestione lettura + variazione
This commit is contained in:
+207
-53
@@ -1,5 +1,6 @@
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using NLog.LayoutRenderers;
|
||||
using S7.Net;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -18,11 +19,12 @@ namespace MHT_Siemens
|
||||
{
|
||||
public partial class SiemensProxy : Form
|
||||
{
|
||||
#region oggetti di base
|
||||
|
||||
/// <summary>
|
||||
/// configurazioen principale proxy
|
||||
/// </summary>
|
||||
protected dataProxy currDataProxy;
|
||||
|
||||
/// <summary>
|
||||
/// Oggetto PLC da ri-utilizzare...
|
||||
/// </summary>
|
||||
@@ -51,6 +53,17 @@ namespace MHT_Siemens
|
||||
/// file delle conf attivo
|
||||
/// </summary>
|
||||
protected string setupFile = "setup.json";
|
||||
/// <summary>
|
||||
/// Ultimi valori registrati da file
|
||||
/// </summary>
|
||||
protected Dictionary<string, string> lastValues;
|
||||
/// <summary>
|
||||
/// valori correntemente letti dal file
|
||||
/// </summary>
|
||||
protected Dictionary<string, string> currValues;
|
||||
#endregion
|
||||
|
||||
#region inizializazione
|
||||
|
||||
public SiemensProxy()
|
||||
{
|
||||
@@ -62,6 +75,8 @@ namespace MHT_Siemens
|
||||
{
|
||||
lg = LogManager.GetCurrentClassLogger();
|
||||
tsslApp.Text = $"{utils.CRS("appName")}";
|
||||
lastValues = new Dictionary<string, string>();
|
||||
currValues = new Dictionary<string, string>();
|
||||
loadMemConf();
|
||||
loadPlc();
|
||||
startUiTimer();
|
||||
@@ -74,14 +89,6 @@ namespace MHT_Siemens
|
||||
uiTimer.Tick += UiTimer_Tick;
|
||||
uiTimer.Start();
|
||||
}
|
||||
private void UiTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
toolStripProgressBar1.ProgressBar.Value++;
|
||||
if (toolStripProgressBar1.ProgressBar.Value >= toolStripProgressBar1.ProgressBar.Maximum)
|
||||
{
|
||||
toolStripProgressBar1.ProgressBar.Value = 0;
|
||||
}
|
||||
}
|
||||
private void startSampleTimer()
|
||||
{
|
||||
int sampleTimerMs = utils.CRI("sampleTimerMs");
|
||||
@@ -89,19 +96,6 @@ namespace MHT_Siemens
|
||||
sampleTimer.Interval = sampleTimerMs;
|
||||
sampleTimer.Tick += SampleTimer_Tick;
|
||||
sampleTimer.Start();
|
||||
}
|
||||
private void SampleTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
// rileggo il file
|
||||
|
||||
// verifico valore...
|
||||
|
||||
// loggo!
|
||||
lg.Info("New Value read...");
|
||||
|
||||
// invio a PLA
|
||||
|
||||
|
||||
}
|
||||
private void startCheckTimer()
|
||||
{
|
||||
@@ -110,21 +104,6 @@ namespace MHT_Siemens
|
||||
checkTimer.Tick += CheckTimer_Tick;
|
||||
checkTimer.Start();
|
||||
}
|
||||
private void CheckTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
// loggo!
|
||||
lg.Info("Program Alive control...");
|
||||
//verifico PLC
|
||||
if (currPLC.IsConnected)
|
||||
{
|
||||
lg.Info("PLC Connected!");
|
||||
}
|
||||
else
|
||||
{
|
||||
lg.Info("Connection error!");
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// init PLC
|
||||
/// </summary>
|
||||
@@ -156,22 +135,6 @@ namespace MHT_Siemens
|
||||
}
|
||||
}
|
||||
/// <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>
|
||||
/// Init conf memoria
|
||||
/// </summary>
|
||||
protected void loadMemConf()
|
||||
@@ -189,6 +152,7 @@ namespace MHT_Siemens
|
||||
currDataProxy = JsonConvert.DeserializeObject<dataProxy>(jsonData);
|
||||
lg.Info("Completed data deserialization");
|
||||
}
|
||||
reader.Close();
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -212,6 +176,8 @@ namespace MHT_Siemens
|
||||
currDataProxy = new dataProxy()
|
||||
{
|
||||
csvFilePath = @"c:\zz\prova1.csv",
|
||||
csvHasHeader = true,
|
||||
csvSeparator = ';',
|
||||
confPLC = new connParam()
|
||||
{
|
||||
ipAdrr = "192.168.0.102",
|
||||
@@ -237,5 +203,193 @@ namespace MHT_Siemens
|
||||
txtSlot.Text = $"{currDataProxy.confPLC.slot}";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region metodi ricorrenti
|
||||
|
||||
|
||||
private void UiTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
toolStripProgressBar1.ProgressBar.Value++;
|
||||
if (toolStripProgressBar1.ProgressBar.Value >= toolStripProgressBar1.ProgressBar.Maximum)
|
||||
{
|
||||
toolStripProgressBar1.ProgressBar.Value = 0;
|
||||
}
|
||||
}
|
||||
private void CheckTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
// loggo!
|
||||
lg.Info("Program Alive control...");
|
||||
//verifico PLC
|
||||
if (currPLC.IsConnected)
|
||||
{
|
||||
lg.Info("PLC Connected!");
|
||||
}
|
||||
else
|
||||
{
|
||||
lg.Info("Connection error!");
|
||||
}
|
||||
|
||||
}
|
||||
private void SampleTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
// rileggo il file
|
||||
reloadFromFile();
|
||||
|
||||
// verifico valore confrontando con i precedenti...
|
||||
if (dataChanged())
|
||||
{
|
||||
// loggo!
|
||||
lg.Info("New Value found");
|
||||
|
||||
// invio a PLC
|
||||
saveToPLC();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region helper methods
|
||||
|
||||
/// <summary>
|
||||
/// salva i dati sul PLC
|
||||
/// </summary>
|
||||
private void saveToPLC()
|
||||
{
|
||||
// invio TUTTI i dati al PLC secondo configurazione...
|
||||
|
||||
// loggo invio al PLC!
|
||||
lg.Info("New Value sent to PLC");
|
||||
}
|
||||
/// <summary>
|
||||
/// Effettua comparazioen dati vecchi/nuovi
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private bool dataChanged()
|
||||
{
|
||||
bool answ = false;
|
||||
// se i 2 vettori sono diversi
|
||||
if (currValues.Count != lastValues.Count)
|
||||
{
|
||||
// salvo ed esco subito
|
||||
lastValues = currValues;
|
||||
return true;
|
||||
}
|
||||
// verifico ogni singolo valore...
|
||||
foreach (var item in currValues)
|
||||
{
|
||||
// cerco...
|
||||
if (lastValues.ContainsKey(item.Key))
|
||||
{
|
||||
// verifico se diversi --> trovato cambio!
|
||||
answ = currValues[item.Key] != lastValues[item.Key];
|
||||
}
|
||||
else
|
||||
{
|
||||
answ = true;
|
||||
}
|
||||
// se ho cambiamenti --> esco!
|
||||
if (answ)
|
||||
{
|
||||
// salvo ed esco subito
|
||||
lastValues = currValues;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return answ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Effettua rilettura da file
|
||||
/// </summary>
|
||||
protected void reloadFromFile()
|
||||
{
|
||||
if (File.Exists(currDataProxy.csvFilePath))
|
||||
{
|
||||
// leggo...
|
||||
using (StreamReader sr = new StreamReader(currDataProxy.csvFilePath))
|
||||
{
|
||||
string currentLine;
|
||||
//se ha header salto la prima riga...
|
||||
if (currDataProxy.csvHasHeader)
|
||||
{
|
||||
if ((currentLine = sr.ReadLine()) == null)
|
||||
{
|
||||
// vuoto: loggo ed esco!
|
||||
lg.Error("Errore intestazione vuota!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// ora leggo la riga successiva... SE !=null
|
||||
|
||||
if ((currentLine = sr.ReadLine()) != null)
|
||||
{
|
||||
// splitto riga e cerco valore...
|
||||
var valori = currentLine.Split(currDataProxy.csvSeparator);
|
||||
// cerco parametri!
|
||||
try
|
||||
{
|
||||
foreach (var item in currDataProxy.parametersList)
|
||||
{
|
||||
// cerco!
|
||||
if (valori.Count() >= item.Value.Index)
|
||||
{
|
||||
upsertValue(item.Key, valori[item.Value.Index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
else
|
||||
{
|
||||
// vuoto: loggo ed esco!
|
||||
lg.Error("Errore riga vuota!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lg.Error($"Errore! file non trovato: {currDataProxy.csvFilePath}");
|
||||
}
|
||||
}
|
||||
|
||||
protected void upsertValue(string key, string value)
|
||||
{
|
||||
// cerco se ci sia... aggiorno!
|
||||
if (currValues.ContainsKey(key))
|
||||
{
|
||||
currValues[key] = value;
|
||||
}
|
||||
// altrimenti aggiungo
|
||||
else
|
||||
{
|
||||
currValues.Add(key, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
{
|
||||
"csvFilePath": "c:\\zz\\prova1.csv",
|
||||
"csvHasHeader": true,
|
||||
"csvSeparator": ";",
|
||||
"confPLC": {
|
||||
"ipAdrr": "192.168.0.102",
|
||||
"tipoCpu": "S71500",
|
||||
@@ -9,7 +11,7 @@
|
||||
"parametersList": {
|
||||
"DB701.DBD142": {
|
||||
"Column": "Valore assoluto",
|
||||
"Index": 8,
|
||||
"Index": 7,
|
||||
"MemConf": "DB701.DBD142",
|
||||
"DataType": "real"
|
||||
}
|
||||
|
||||
@@ -9,16 +9,24 @@ namespace MHT_Siemens
|
||||
/// <summary>
|
||||
/// classe costruzione dataproxy
|
||||
/// </summary>
|
||||
public class dataProxy
|
||||
public class dataProxy
|
||||
{
|
||||
/// <summary>
|
||||
/// Nome file da osservare
|
||||
/// </summary>
|
||||
public string csvFilePath { get; set; } = "";
|
||||
/// <summary>
|
||||
/// Indica se il CSV abbia intestazione
|
||||
/// </summary>
|
||||
public bool csvHasHeader { get; set; } = true;
|
||||
/// <summary>
|
||||
/// Indica se il CSV abbia intestazione
|
||||
/// </summary>
|
||||
public char csvSeparator { get; set; } = ';';
|
||||
/// <summary>
|
||||
/// Configurazione PLC
|
||||
/// </summary>
|
||||
public connParam confPLC { get; set; }
|
||||
public connParam confPLC { get; set; }
|
||||
/// <summary>
|
||||
/// Elenco parametri
|
||||
/// </summary>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
{
|
||||
"csvFilePath": "c:\\zz\\prova1.csv",
|
||||
"csvHasHeader": true,
|
||||
"csvSeparator": ";",
|
||||
"confPLC": {
|
||||
"ipAdrr": "192.168.0.102",
|
||||
"tipoCpu": "S71500",
|
||||
@@ -9,7 +11,7 @@
|
||||
"parametersList": {
|
||||
"DB701.DBD142": {
|
||||
"Column": "Valore assoluto",
|
||||
"Index": 8,
|
||||
"Index": 7,
|
||||
"MemConf": "DB701.DBD142",
|
||||
"DataType": "real"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user