How to write GUI for a StateWORKS application
Graphical User Interface for StateWORKS Run-time Systems
Most applications need some sort of graphical user interface (GUI). The GUI is used to communicate with the program: to set parameters, send commands, display results, show alarms, etc. In a StateWORKS run-time system the entire information is stored in the Real Time Data Base (RTDB). RTDB has a TCP/IP interface which allows access to the data. The RTDB operates as a server. To access the data in the RTDB we need a TCP/IP client.
Monitoring programs that are part of the StateWORKS Studio are programs that use a TCP/IP interface for communication with the RTDB: they are RTDB clients.
To make the task easy we provide a TCP/IP Client library which should be used for building any GUI interface for a StateWORKS application.
TCP/IP Client Library
The TCP/IP C++ library has been built and tested with MS Visual Studio. To use it 3 h-files are required. The most interesting h-file TCPIPClient.h
contains the declaration of all functions used for building GUI programs:
The port created with the TCP/IP library contains two sockets: one for sending Request, Poke, etc. and the second one for events generated by Advised objects or in reply to Request.
You can learn from the GUI_CPP.cpp
file (Appendix 1) the details of the connection. The file has been taken from the GUI_CPP.exe
project prepared with MS Visual Studio .NET. Of course, the details of windows programming will differ for other development platform. Therefore, I ignore them and concentrate only on TCP/IP relevant topic. The description below contains some detailed information explaining the idiosyncrasies of the library functions.
The initialization
The Initialize()
method called by a start of the program in the OnInitDialog()
requires two parameters:
- the reference to the callback function
RepEvt
used by the event thread run in the TCP/IP client. - the pointer to the Owner of the
RepEvt
function.
The Connection
The Connect()
method called in the OnBnClickedConnect()
requires two parameters: the server name and the port number. As both parameters have default values: respectively LOCALHOST
and 9091
(the StateWORKS port number) they can be omitted if the client will run on the same computer as the StateWORKS run-time system.
The Disconnection
The Disconnect()
method called in a local Disconnect()
function does not take any parameters (actually, it has one parameter bWithUnadvise
that you can ignore as it has a default value false
). The local Disconnect()
function is used twice: in OnBnClickedDisconnect()
activated when the user disconnects the client with the Disconnect button and in the RepEvt
function when the server terminates the connection.
Getting a value
The Request()
method is called several times: by Connect
, Get
, Set
and in the RepEvt
function. It should be called by setting a value in the RTDB as the automatic answer coming to the Write value operation is not the actual value but the written value.
The Request()
method requires 3 parameters:
- the object Name, for instance
Ni:ActualPressureValue
, - the object Attribute to be read (see Appendix), for instance
IATT_Val
, - the reference to a Value variable which will contain the read attribute value.
Setting a value
The Poke()
method is called in the OnBnClickedSet()
function and requires 3 parameters:
- the object Name, for instance
Ni:ActualPressureValue
, - the Value to be written, for instance
IATT_Trc
, - the object Attribute to be written (see Appendix), for instance
.Val
.
As mentioned already before, it makes sense to complete the writing an attribute value by a reading operation because the value returned during the write operation is not the actual value but the written value (a kind of echo).
Advise an object attribute
The Advise operation instructs the server to send an event to the client if the “advised” object attribute has changed. The AdviseStart()
method is called in the OnBnClickedAdvise()
function and requires 3 parameters:
- the object Name, for instance
Ni:ActualPressureValue
, - the object Attribute to be advised (see Appendix), for instance
IATT_Val
, - the reference to a Value variable which will contain the just advised attribute value (for an advise operation the actual value is returned).
Unadvising an object attribute
The Unadvise operation instructs the server to stop sending events to the client. The AdviseStop()
method is called in the OnBnClickedUnAdvise()
function and requires 2 parameters:
- the object Name, for instance
Ni:ActualPressureValue
, - the object Attribute to be unadvised (see Appendix), for instance
IATT_Val
.
Note that all methods parameters except the Name of the object and Attribute in case of the Poke() method has default values. If you do not set the Attribute value the functions return doing nothing.
The RepEvt
function
The RepEvt()
function cannot belong to the Application class (in this case CGUI_CPPDlg
). Therefore while called it receives the pointer to the environment which allows it to use the method of the application class.
The RepEvt()
function is called in the client event thread implemented in the TCP/IP library (not accessible to the programmer). It is called there in 3 relevant cases:
- when the server send an event with the advised data object,
- when the server restarts the application configuration file,
- when the server exits.
It is called also by request, but normally we do not use it as we get the requested value anyway when the Request()
function returns.
Example GUI_CPP
The GUI interface depends on the application. It is impossible to write something of a general character, except Monitors which offer access to all RTDB data, so the example given is a kind of Monitor. The source code, which is provided for download from our website, shows the usage of the TCP/IP Client functions. In addition to its tutorial function, the example displays all object attributes and allows quickly to check the read/write feature of the attribute – clicking the get / set buttons produces a message if the operation is not allowed.
Running the example
The GUI_CPP.exe
example is included in the StateWORKS Studio package. You find it in the Installation directory.
While running any StateWORKS application, for instance SWLab, start the SWQuick Monitor program which uses the described GUI interface. The usage is so obvious that it does not make sense to provide any Help or description.
Summary
With the TCP/IP Client Library the user gets an important component for linking the Graphical User Interface with StateWORKS run-time systems. This library can also be used to build any other kinds of interfaces to communicate with the run-time system.
Appendix 1
GUI_CPP.cpp
Appendix 2. Attributes
Attributes
The article was originally written by author F. Wagner in February 2004.
Here you can view the unedited file directly from the author: PDF