The application described in the following enables you to control the Web-IO under Linux. The user interface was assembled using Version 3.3.5 of Qt-Designer.
Using the following C++ program example you can represent your Web-IO Digital with its inputs and outputs in a Linux application. You can also switch the outputs on the Web-IO.
You don’t have a Web-IO yet but would like to try the example out sometime?
No problem: We will be glad to send you the Web-IO Digital 2xInput, 2xOutput at no charge for 30 days. Simply fill out a sample ordering form, and we will ship the Web-IO for testing on an open invoice. If you return the unit within 30 days, we will simply mark the invoice as paid.
1. Combining the various operating elements and display objects in Qt-Designer
To create this application, a new C++ project is created in Qt-Designer to which you can assign any desired name. We are using the name "Client". In the next step a main window is generated in which the graphical elements are placed and named according to their property.
To make compiling the application easier under a Unix system, we create two files called "Uic" and Qmake". This gives us functional C++ source text, which one can easily compile using a makefile.
You create a new text file and write the two following lines to it. Then save the file as "Uic".
Note: These lines only work if qt3 is used and installed at the same place in the system. Otherwise please adapt the two lines accordingly.
Now you create another text file which is saved as "Qmake".
/usr/lib/qt3/bin/qmake -o Makefile $1.pro
Once both files have been created, place them both in the directly containing the project. Then configure the two files so that they are executable. For this you can use the chmod command.
-> chmod a+x Dateiname
In the following the project can be successfully converted into C++ source code and compiled. This only take three additional steps.
./Uic Dateiname(.ui)
./Qmake Projekt(.pro)
make
Now an executable file has been created which has the same name as the project.
2. Create the source code file for the graphical interface
If Qt-Designer is started and you have the main window open, double-clicking on the window creates a .ui.h source code file. In the following you are shown in detail which steps are necessary to get this application running.
To create new slots, right-click on the main window to open a properties window, and select the Slots entry. There you create the required slots, which then automatically appear with an empty body in the generated .ui.h file.
Now we include a few required header files. The clientDlg.h and the clientDlg.cpp are generated after the application with ./Uic from the .ui file (main window). To be able to access the elements, the header file is included here.
Then global variables for a TCP connection and polling are declared and thereby made accessible to every method in the class. Since we are not defining our own namespace, the standard namespace is used.
Setting up the operating elements The group with the operating elements for the Web-IO is first blocked from operation. As soon as a connection is opened, all required elements are enabled. For this you can set the Enable function in the properties of the GroupBox cb_io_control, as well as for all other elements, to ’false’ or ’true’ .
The name of the respective operating element is derived from the element itself depending on the context. The first two characters in the name stand for the element type (cb -> Checkbox, bt -> Button, gb -> Groupbox and le-> LineEdit).
4. Connection control
Establishing the connection After entering the IP address for the Web-IO in the text field li_ip and Port 80 in the text field li_port, clicking on the bt_connect button allows a connection to be opened. If no IP address or port is entered, a message is displayed in the status bar
Opening the connection To be able to open a TCP connection, the already declared socket variable is initialized.
Once everything has been correctly entered, an attempt is made to open a connection. To determine whether the connection has been established, the system waits for a signal from the socket which reports a successful connection opening.
void clientDlg::onConnect()
{
client = new QSocket(this);
bool ok;
if(le_ip->text() == "")
le_statusBar->setText("No IP entered!"); else if(le_port->text() == "")
le_statusBar->setText("No port
entered!"); else
client->connectToHost(le_ip->text(),
(le_port->text()).toInt(&ok,10));
}
If there was an error in opening the connection, a corresponding message appears in the status bar.
void clientDlg::connectError( int
)
{
le_statusBar->setText("Error while connecting!");
}
Connection is made After successfully opening a connection, all useful operating elements for the application are enabled and the Connect button deactivated. In addition, the application immediately begins to go into receive ready mode.
Disconnecting The connection remains open until the user clicks on the Disconnect button or until it is ended by the Web-IO. After clicking on the button a message is displayed that the connection has been ended.
Since all counter states can be read or reset using one command, there must be a method implemented which processes the reply string from the Web-IO and assigns each counter in the application its specific state.
void clientDlg::readAndClearCounter(QString
data)
{ QString counter[12]; int j = 0; int length
= data.length(); for(int
i = 0; i < length; i++) { if(data[i]
== ';') j++; else counter[j]
+= data[i]; } le_counter0->setText(counter[0]); le_counter1->setText(counter[1]);
}
6. Data reception from the Web-IO
Process and display the received data
All commands and requests to the Web-IO are acknowledged with a reply string. The replies have a specific structure depending on the type:
For the outputs: output;<binary value of the output status in hexadecimal format> For a special output: outputx;<ON or OFF>
For the inputs: input;<binary value of the input status in hexadecimal format> For a special input inputx;<ON or OFF>
Then there is the reply string for a counter, which looks as follows.
Counter: counterx;<decimal counter state>
or counter;<decimal counter state 0 >; <decimal counter state 0 >; ... if you want to read all the counters at one time.
All reply strings are finished off with a 0 byte.
In our application the method OnReceiver() is invoked to receive such a message. In this method the reply from the Web-IO is processed. The unique feature here is that this function invokes itself the entire time, i.e. is continually looking for messages which may have been sent by the Web-IO. We have established that directly after this a connection has been successfully initialized (connectDone()).
Cyclical polling of particular values It is desirable that the status of an individual component be updated by the component itself. For this the program uses a timer which sends cyclical queries to the Web-IO at a user-defined time interval.
The cycle time is entered in the field le_interval.
Of course this also catches cases where the user enters a nonsense value, such as a negative time value. This is immediately followed by a message and the value is not applied.
bool clientDlg::checkRange()
{ bool ok; int tmp = (le_interval->text()).toInt(&ok,
10); if(ok &&
tmp > 0) { le_statusBar->setText("Range
changed to " + le_interval->text() + " ms!"); returntrue; } else { le_statusBar->setText("Please
type a positive integer value for the range of polling!"); returnfalse; }
}
To now carry out cyclical polling of the Web-IO states, the choose from between polling the outputs, the inputs or the counters.
Activating the checkbox cb_polling_outputs then means the outputs are polled.
The sample program supports all common functions of the Web-IO in command string mode, optimized for the Web-IO 2x Digital Input, 2x Digital Output. For the other Web-IO models you may have to adapt the program. Additional program examples for socket programming can be found on the Tool pages for the Web-IO. A detailed description for the socket interface of the Web-IO Digital models can be found in the reference manual.