.NET Based OPC UA Client/Server SDK
3.2.1.522
|
The Program example is a basic implementation of an OPC UA Program that counts a value down from 1000 to 0. Please read the short introduction to Programs if you’re not familiar with the concept.
The Demo Server address space contains the type CountdownStateMachineType having the child Value. This custom ProgramType is a subtype of the ProgramStateMachineType. An instance of this type can be found in the folder Demo → 016_StateMachines → Program. The sample code can be found in the file DemoNodeManager.StateMachines.cs.
For internal processing of the ProgramStateMachine we create an enumeration containing the possible states.
The generated class CountdownStateMachineModel is extended with this enumeration.
There are several transitions defined for the Program Finite State Machine. We define methods for each transition. In these methods the current state and the last transition is set.
The business logic is implemented here as well: e.g. in ReadyToRunning a timer is started that counts the the variable Value down. In addition, the TransitionEvents are fired.
To access all data that is needed to fire the event fields, we extend the generated class CountdownStateMachineModel.
The transition methods will be called by the OPC UA Method implementation. When an OPC UA Method is called, we need to check if it is possible to call the Method, since not all Methods can be called for all states of the program. If the Method can be called by the client, the transition method is called in the OPC UA Method implementation.
The methods of the ProgramState are implemented by the DemoNodeManager. So DemoNodeManager needs to implement the Interface IProgramStateMachineMethods.
After implementing the methods we need to link the CountdownStateMachineModel to the nodes in the address space.
Depending on the current state of the Program Finite State Machine, some Methods cannot be called by the client. So we set the Executable attribute to false if a Method cannot be called by a client. We add the NodeIds of the Methods as property to the class CountdownStateMachineModel and use the ServerInternalClient to get them.
The transition methods are setting the Executable attributes.
Finally, we can test the implementation with a client, e.g. UaExpert.
First we drag and drop the variables Value, CurrentState, and LastTransition to the DA View (see screenshot below). The variable CurrentState has the value Ready, because the program hasn’t been started yet. Thus, LastTransition is empty, because no transition has taken place. The value of Value is 1000, because the countdown hasn’t started yet.
Then choose Document → Add… from the menu. Select Event View from the drop-down list and confirm with OK. Drag and drop the Program object to the event view (see screenshot). We are now ready to receive transition events.
Switch back to the DA View and call the method Start. The value of CurrentState has changed to running. LastTransition has the value ReadyToRunning and the value of Value is decreasing. When switching to the Event View, we can see the transition event in the list of events.
Switch back to the DA View and call the method Suspend. The countdown stops and the values of CurrentState and LastTransition change accordingly. When calling the method Suspend again, you will get an error, because the method cannot be called in the suspended state.
Call the method Resume and wait for the program to finish if you like.