public static void Main(string[] args) { //// As already demonstrated in the basic sample 'Notifications' it is possible to //// handle the state changes of a connection using the events provided by the static //// PlcNotifications class or using the events provided by the instance of a //// connection itself. //// Additionally taking the basic sample 'Exceptions' into account it is obvious that //// developers using the framework are able to fully control and monitor the creation //// of connections, the state of these connections and they can influence the status //// evaluation of every operation performed by the low level driver used within the //// framework. //// This sample does therefore demonstrate how a developer can get the most out of //// the features represented by the basic samples 'Notifications' and 'Exceptions'. // Assign an event handler to the ConnectionCreated event to get notified when ever // a new connection has been created within the framework. This handler does then // subscribe further events on the connection. PlcNotifications.ConnectionCreated += Program.HandleConnectionCreated; // Assign a callback method to evaluate the operational status in a custom way to // ensure that only in specific conditions failed operations will lead to an exception. PlcNotifications.EvaluateStatus = Program.EvaluateStatus; SimaticDevice device = new SimaticDevice("192.168.0.80", SimaticDeviceType.S7300_400); PlcDeviceConnection connection = device.CreateConnection(); Console.WriteLine(); Console.WriteLine("= Open ="); connection.Open(); //// An explicit call to Connect() is not required. But it will be done here to //// demonstrate the appropriate state changes which occur in case of the //// first attempt to acccess the PLC device. Console.WriteLine(); Console.WriteLine("= Connect ="); connection.Connect(); Program.ReadBoolean(connection); Program.ReadBooleanArray(connection); Console.WriteLine(); Console.WriteLine("= Close ="); connection.Close(); Console.ReadKey(); }
public static void Main(string[] args) { SimaticDevice device = new SimaticDevice("192.168.0.80", SimaticDeviceType.S7300_400); #region 1. Way: Global events. { //// In case there it is necessary of being notified when ever a new connection //// has been created or the state of a connection has been changed there does //// the PlcNotifications class provide different events that can be used for that //// requirement. //// Simply assign custom event handlers to the appropriate events like the //// following lines demonstrate. PlcNotifications.ConnectionCreated += Program.HandleAnyEvent; PlcNotifications.ConnectionOpening += Program.HandleAnyEvent; PlcNotifications.ConnectionOpened += Program.HandleAnyEvent; PlcNotifications.ConnectionConnecting += Program.HandleAnyEvent; PlcNotifications.ConnectionConnected += Program.HandleAnyEvent; PlcNotifications.ConnectionClosing += Program.HandleAnyEvent; PlcNotifications.ConnectionClosed += Program.HandleAnyEvent; PlcDeviceConnection connection = device.CreateConnection(); connection.Open(); //// An explicit call to Connect() is not required. But it will be done here to //// demonstrate the appropriate state changes which occur in case of the //// first attempt to acccess the PLC device. connection.Connect(); connection.Close(); //// In case there is no longer any need of being notified simply detach the //// associated custom event handlers like the following lines demonstrate. PlcNotifications.ConnectionCreated -= Program.HandleAnyEvent; PlcNotifications.ConnectionOpening -= Program.HandleAnyEvent; PlcNotifications.ConnectionOpened -= Program.HandleAnyEvent; PlcNotifications.ConnectionConnecting -= Program.HandleAnyEvent; PlcNotifications.ConnectionConnected -= Program.HandleAnyEvent; PlcNotifications.ConnectionClosing -= Program.HandleAnyEvent; PlcNotifications.ConnectionClosed -= Program.HandleAnyEvent; } #endregion Console.WriteLine(); #region 2. Way: Global state event. { //// Instead of attachning/detaching a custom event handler to specific events it //// is also possible to handle all state events using code like the following one. PlcNotifications.ConnectionStateChanged += Program.HandleStateEvent; PlcDeviceConnection connection = device.CreateConnection(); connection.Open(); //// An explicit call to Connect() is not required. But it will be done here to //// demonstrate the appropriate state changes which occur in case of the //// first attempt to acccess the PLC device. connection.Connect(); connection.Close(); //// In case there is no longer any need of being notified simply detach the //// associated custom event handler like the following line demonstrate. PlcNotifications.ConnectionStateChanged -= Program.HandleStateEvent; } #endregion Console.WriteLine(); #region 3. Way: Instance events. { //// If there is no need of being notified when ever any connection changes its //// state, it is also possible to restrict the event handlers to one or more //// specific connection instances. //// Simply assign custom event handlers to the appropriate events like the //// following lines demonstrate. PlcDeviceConnection connection = device.CreateConnection(); connection.Opening += Program.HandleAnyInstanceEvent; connection.Opened += Program.HandleAnyInstanceEvent; connection.Connecting += Program.HandleAnyInstanceEvent; connection.Connected += Program.HandleAnyInstanceEvent; connection.Closing += Program.HandleAnyInstanceEvent; connection.Closed += Program.HandleAnyInstanceEvent; connection.Open(); //// An explicit call to Connect() is not required. But it will be done here to //// demonstrate the appropriate state changes which occur in case of the //// first attempt to acccess the PLC device. connection.Connect(); connection.Close(); //// In case there is no longer any need of being notified simply detach the //// associated custom event handlers like the following lines demonstrate. connection.Opening -= Program.HandleAnyInstanceEvent; connection.Opened -= Program.HandleAnyInstanceEvent; connection.Connecting -= Program.HandleAnyInstanceEvent; connection.Connected -= Program.HandleAnyInstanceEvent; connection.Closing -= Program.HandleAnyInstanceEvent; connection.Closed -= Program.HandleAnyInstanceEvent; } #endregion Console.WriteLine(); #region 4. Way: Instance state event. { //// Instead of attachning/detaching a custom event handler to specific events it //// is also possible to handle all state events using code like the following one. PlcDeviceConnection connection = device.CreateConnection(); connection.StateChanged += Program.HandleInstanceStateEvent; connection.Open(); //// An explicit call to Connect() is not required. But it will be done here to //// demonstrate the appropriate state changes which occur in case of the //// first attempt to acccess the PLC device. connection.Connect(); connection.Close(); //// In case there is no longer any need of being notified simply detach the //// associated custom event handler like the following line demonstrate. connection.StateChanged -= Program.HandleInstanceStateEvent; } #endregion Console.ReadKey(); }