Statusänderung vom TwinCAT-Router und der SPS erkennen

Download

Voraussetzungen

Sprache / IDE

Beispielprogram auspacken

C# / Visual Studio

Sample08.zip

Aufgabe

Erkennen von Statusänderungen des Routers und der SPS.

Beschreibung

Statusänderungen von ADS-Geräten können effektiv über Callback-Funktionen erkannt werden. Diese werden aufgerufen, wenn sich der Status eines ADS-Gerätes ändert.

Zur Erkennung von Statusänderungen des Routers enthält die Klasse TcAdsClient das Event TcAdsClient.AmsRouterNotification. Zur Erkennung von Statusänderungen der SPS kann eine ADS Notification auf den Status der SPS angemeldet werden. Sowohl für das Router-Event als auch für die SPS-Notification können Callback-Funktionen registriert werden, die bei den entsprechenden Statusänderungen aufgerufen werden.

Im folgenden Beispielprogramm werden die Statusänderungen des Routers und der SPS nach dem obigen Verfahren überwacht.

C# Programm

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using TwinCAT.Ads;

namespace TwinCATAds_Sample08
{
public partial class Form1 : Form
{
private TcAdsClient _tcClient = null;
private AdsStream _adsStream = null;
private BinaryReader _binRead = null;
private int _notificationHandle = 0;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
try
{
_tcClient = new TcAdsClient();


// Connect to local PLC - Runtime 1 - TwinCAT3 Port=851
_tcClient.Connect(851);

_adsStream = new AdsStream(2); /* stream for storing the ADS state of the PLC */
_binRead = new BinaryReader(_adsStream); /* reader for reading the state */

/* register callback function to detect state changes in the router */
_tcClient.AmsRouterNotification += new AmsRouterNotificationEventHandler(AmsRouterNotificationCallback);

/* register an ADS notification an the ADS status word of the PLC */
_notificationHandle = _tcClient.AddDeviceNotification(
(int)AdsReservedIndexGroups.DeviceData, /* index group of the device state*/
(int)AdsReservedIndexOffsets.DeviceDataAdsState, /*index offset of the device state */
_adsStream, /* stream to store the state */
AdsTransMode.OnChange, /* transfer mode: transmit ste on change */
0, /* transmit changes immediately */
0,
null);

/* register callback function to react on notifications */
_tcClient.AdsNotification += new AdsNotificationEventHandler(OnAdsNotification);
}
catch (AdsErrorException ex)
{
MessageBox.Show(ex.Message);
}
}

/* callback function called on state changes of the router */
void OnAdsNotification(object sender, AdsNotificationEventArgs e)
{
if (e.NotificationHandle == _notificationHandle)
{
AdsState plcState = (AdsState)_binRead.ReadInt16(); /* state was written to the stream */
_plcLabelValue.Text = plcState.ToString();
}
}

/* Ccallback function called on state changes of the PLC */
void AmsRouterNotificationCallback(object sender, AmsRouterNotificationEventArgs e)
{
_routerLabelValue.Text = e.State.ToString();
}

private void _exitButton_Click(object sender, EventArgs e)
{
this.Close();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
_tcClient.DeleteDeviceNotification(_notificationHandle);
_tcClient.Dispose();
}
catch(AdsErrorException ex)
{
MessageBox.Show(ex.Message);
}
}
}
}