-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathMainWindow.xaml.cs
116 lines (98 loc) · 3.97 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DotNetSiemensPLCToolBoxLibrary.Communication;
using DotNetSiemensPLCToolBoxLibrary.DataTypes;
namespace ExampleWPFVisualization
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private DotNetSiemensPLCToolBoxLibrary.Communication.PLCConnection myConn = null;
public MainWindow()
{
InitializeComponent();
}
private void cmdConfig_Click(object sender, RoutedEventArgs e)
{
//Verbindung konfigurieren
DotNetSiemensPLCToolBoxLibrary.Communication.Configuration.ShowConfiguration("ExampleWPFVisualization", true);
}
private BackgroundWorker worker = null;
private void cmdConnect_Click(object sender, RoutedEventArgs e)
{
cmdConnect.IsEnabled = false;
cmdStop.IsEnabled = true;
//Alle Tags in eine Liste packen
List<PLCTag> Tags = new List<PLCTag>();
foreach (DictionaryEntry dictionaryEntry in this.Resources)
{
if (dictionaryEntry.Value is PLCTag)
Tags.Add((PLCTag) dictionaryEntry.Value);
}
try
{
//Verbindungskonfig laden und Verbinden
myConn = new PLCConnection("ExampleWPFVisualization");
myConn.Connect();
}
catch (Exception ex)
{
lblStatus.Content = ex.Message;
}
//Tags im Hintergrund laden
if (worker != null)
{
worker.Dispose();
}
//Uncomment this line, to use the VarTab Functions for Read
//PLCConnection.VarTabData vtab = myConn.ReadValuesWithVarTabFunctions(Tags, PLCReadTriggerVarTab.EndOfCycle);
worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = true;
worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
while (!worker.CancellationPending)
{
try
{
myConn.ReadValues(Tags);
//Uncomment this line, to use the Vartab Functions for read, and comment the line above!
//vtab.RequestData();
}
catch (Exception ex)
{
}
}
};
worker.RunWorkerAsync();
}
private void PLCTag_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
//Bei Änderung des Controlvalue eines Tags, den Tag in die SPS schreiben
if (myConn != null)
if (e.PropertyName == "Controlvalue")
myConn.WriteValue((PLCTag) sender);
}
private void cmdStop_Click(object sender, RoutedEventArgs e)
{
worker.CancelAsync();
cmdConnect.IsEnabled = true;
cmdStop.IsEnabled = false;
myConn.Disconnect();
}
}
}