59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
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 S7.Net;
|
|
|
|
namespace C__WPF_Test_Siemens
|
|
{
|
|
public partial class MainWindow : Window
|
|
{
|
|
private Plc _plc;
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
// Verbindung zur SPS aufbauen
|
|
_plc = new Plc(CpuType.S71500, "192.168.0.1", 0, 1); // IP, Rack, Slot
|
|
}
|
|
|
|
private void StartButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
_plc.Open();
|
|
|
|
if (_plc.IsConnected)
|
|
{
|
|
// Beispiel: schreibe die Ganzzahl 1 in Datenbaustein 1, DBW 0
|
|
_plc.Write("DB1.DBW0", (short)1);
|
|
|
|
MessageBox.Show("Wert wurde erfolgreich geschrieben!", "Erfolg", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Verbindung fehlgeschlagen!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Fehler: " + ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
_plc.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|