Tutorial for the Web-IO Digital:
Control and monitor Web-IO Digital with Delphi
As an easy to learn higher language Delphi offers everything you need for programming TCP/IP applications. This also makes Delphi a favorite tool for creating applications that communicate with the Web-IO Digital. Additional drivers or DLLs are not required.
Using the following program example you can represent your Web-IO Digital with its inputs and outputs in a Windows application. You can also switch the Web-IO outputs.
Preparations
- Provide power to the Web-IO and connect the IOs
- Connect the Web-IO to the network
- Assign IP addresses
On the Web-IO in Communication channels >> Socket API activate TCP-ASCII sockets and enable outputs for switching
Combining the various operating elements and display objects in the Delphi form

When naming the individual objects it is helpful to use logical names. In this example the first part of the name describes the type of object and the second part the function.
To process TCP communication the TIdTCPClient component of Indy is used, which is part of the Delphi components.
Global variables and procedures
First a few global variables need to be declared.
var
.......
ClientSocketThread : TIdThreadComponent;
Receivestring : string;
Important is the thread component ClientSocketThread for which a corresponding procedure also needs to be created. Later this thread will process the asynchronous receiving of incoming network data.
private
{ Private-Deklarationen }
public
procedure ClientSocketThreadRun(Sender: TIdThreadComponent);
end;
The corresponding procedure must be created in the implementation area (see below).
Starting the program
Setting up the operating elements
At first the group with the operating elements for the Web-IO is blocked from use. The status line indicates that there is not yet a connection.
procedure Twebio_ascii_client.FormCreate(Sender: TObject);
begin
ClientSocketThread := TIdThreadComponent.Create();
ClientSocketThread.onRun := ClientSocketThreadRun;
StatusBar1.SimpleText := ’No Connection’;
bt_disconnect.Enabled := False;
gb_io.Enabled := False;
end;
Connection control
Establishing the connectionThe connection is opened by entering the IP address of the Web-IO in the text field ed_ip and of the TCP port in the text field ed_port and clicking on the bt_connect button.
procedure Twebio_ascii_client.bt_connectClick(Sender: TObject);
begin
if ed_ip.Text <> ’’ then
begin
ClientSocket.Host := ed_ip.Text;
ClientSocket.Port := strtoint(ed_port.Text);
ClientSocket.Connect;
end;
end;
Connection is made
As soon as the Web-IO accepts the connection, the ClientSocket control element carries out the corresponding procedure. The status line indicates that the connection has been established, the control elements are enabled for use and the Disconnect button is active again.
procedure Twebio_ascii_client.ClientSocketConnected(Sender: TObject);
begin
ClientSocketThread.Active := true;
StatusBar1.SimpleText := ’Connected to ’ + ed_ip.Text;
bt_connect.Enabled := False;
bt_disconnect.Enabled := True;
gb_io.Enabled := True;
end;
Disconnecting
The connection remains open until it is ended by the user clicking on the Disconnect button, or the Web-IO ends the connection.
procedure Twebio_ascii_client.bt_disconnectClick(Sender: TObject);
begin
ClientSocket.Disconnect;
end;
Here again the ClientSocket control element invokes a corresponding procedure.
procedure Twebio_ascii_client.ClientSocketDisconnected(Sender: TObject);
begin
ClientSocketThread.Active := false;
ClientSocket.Disconnect;
StatusBar1.SimpleText := ’No Connection’;
bt_connect.Enabled := True;
bt_disconnect.Enabled := False;
gb_io.Enabled := False;
end;
Operation and communication from the client side
As soon as a connection is made with the Web-IO, the user can use the corresponding program elements to send commands to the Web-IO.
Setting the outputsThe user sets the outputs by using the two check boxes cb_outputx. The program uses the MouseUP event of this object. If a Mouse Up, i.e. releasing the output check box, is used, the program carries out the corresponding procedure and - depending on whether the check box is set or not - passes the appropriate command to the Web-IO.
procedure Twebio_ascii_client.cb_outputMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if sender = cb_output0 then
if cb_output0.Checked then
ClientSocket.IOHandler.
Write(’GET /outputaccess0?PW=’ + ed_password.Text + ’&State=ON&’)
else
ClientSocket.IOHandler.
Write(’GET /outputaccess0?PW=’ + ed_password.Text + ’&State=OFF&’)
else
if cb_output1.Checked then
ClientSocket.IOHandler.
Write(’GET /outputaccess1?PW=’ + ed_password.Text + ’&State=ON&’)
else
ClientSocket.IOHandler.
Write(’GET /outputaccess1?PW=’ + ed_password.Text + ’&State=OFF&’);
end;;
Query output/input status
The user can request the status of the outputs and inputs by clicking on the corresponding button.
procedure Twebio_ascii_client.bt_outputs_readClick(Sender: TObject);
begin
ClientSocket.IOHandler.Write(’GET /output?PW=’ + ed_password.Text + ’&’);
end;
procedure Twebio_ascii_client.bt_inputs_readClick(Sender: TObject);
begin
ClientSocket.IOHandler.Write(’GET /input?PW=’ + ed_password.Text + ’&’);
end;
Read clear counters
Also the counter states of the input counters can be read or cleared.
procedure Twebio_ascii_client.bt_counter_readClick(Sender: TObject);
begin
if sender = bt_counter_read0 then
ClientSocket.IOHandler.Write(’GET /counter0?PW=’ + ed_password.Text + ’&’)
else
ClientSocket.IOHandler.Write(’GET /counter1?PW=’ + ed_password.Text + ’&’);
end;
procedure Twebio_ascii_client.bt_counter_clearClick(Sender: TObject);
begin
if sender = bt_counter_clear0 then
ClientSocket.IOHandler.Write(’GET /counterclear0?PW=’ + ed_password.Text + ’&’)
else
ClientSocket.IOHandler.Write(’GET /counterclear1?PW=’ + ed_password.Text + ’&’);
end;
Of course all the counters can be read or cleared at the same time.
procedure Twebio_ascii_client.bt_counter_readallClick(Sender: TObject);
begin
ClientSocket.IOHandler.Write(’GET /counter?PW=’ + ed_password.Text + ’&’)
end;
procedure Twebio_ascii_client.bt_counter_clearallClick(Sender: TObject);
begin
ClientSocket.IOHandler.Write(’GET /counterclear?PW=’ + ed_password.Text + ’&’)
end;
Receiving data from the Web-IO
Process and display the received dataAll the commands and queries to the Web-IO are acknowledged with a reply string. Receipt and processing of the replies is handled by the ClientSocketThreadRun thread mentioned earlier.
Format of the repliesThe replies have a specific structure depending on the type.
- For the outputs:
output;<Binärwert des Outputstatus im hexadezimalen Format>
- For the inputs:
input;<Binärwert des Outputstatus im hexadezimalen Format>
- For the counters:
counterx;<dezimaler Zählerstand>
- or
counter;<dezimaler Zählerstand 0 >; <dezimaler Zählerstand 0 >; ......
,
if all the counters need to be read at one time.
All reply strings are finished off with a 0 byte. If data are received by the ClientSocket control element, it invokes the corresponding procedure.
procedure Twebio_ascii_client.ClientSocketThreadRun(Sender: TIdThreadComponent);
var
Receivechar : string;
SemiPosition : integer;
OutputValue : word;
InputValue : word;
begin
Receivechar := ClientSocket.IOHandler.ReadString(1);
if Receivechar[1] <> chr(0) then
Receivestring := Receivestring + Receivechar
else
begin
if Receivestring[1] = ’o’ then
begin
OutputValue := StrtoInt(’$’+copy(ReceiveString,
pos(’;’,ReceiveString)+1,length(ReceiveString)-pos(’;’,ReceiveString)));
if OutputValue and 1 = 1 then
cb_output0.Checked := True
else
cb_output0.Checked := False;
if OutputValue and 2 = 2 then
cb_output1.Checked := True
else
cb_output1.Checked := False;
end;
if Receivestring[1] = ’i’ then
begin
InputValue := StrtoInt(’$’+copy(ReceiveString,
pos(’;’,ReceiveString)+1,
length(ReceiveString)-pos(’;’,ReceiveString)));
if InputValue and 1 = 1 then
cb_input0.Checked := True
else
cb_input0.Checked := False;
if InputValue and 2 = 2 then
cb_input1.Checked := True
else
cb_input1.Checked := False;
end;
if Receivestring[1] = ’c’ then
begin
if copy(ReceiveString, 8, 1) = ’0’ then
ed_counter0.Text := copy(ReceiveString, 10,length(ReceiveString)-9);
if copy(ReceiveString, 8, 1) = ’1’ then
ed_counter1.Text := copy(ReceiveString, 10,length(ReceiveString)-9);
if copy(ReceiveString, 8, 1) = ’;’ then
begin
ReceiveString[8] := ’ ’;
ed_counter0.Text := copy(ReceiveString, 9, pos(’;’,ReceiveString)-9);
ed_counter1.Text := copy(ReceiveString,
pos(’;’,ReceiveString)+1,
length(ReceiveString)-pos(’;’,ReceiveString));
end;
end;
receivestring := ’’;
end;
end;
The receive procedure uses the first character of the receive data to check whether these are input, output or counter messages. Depending on the result it may be determined for example which output has what status. Since the output status is given in hexadecimal, it must be converted into an integer value in order to calculate further steps. This is done by using a preceding ’$’
for the StrToInt
function. In the case of the counters it is possible to both query individual counter values and read out all the counters at one time. The individual counter states are then output in decimal format in a semicolon delimited string.
Polling
Cyclical polling of particular valuesIn order to enable automatic refreshing of the display, a timer is used.
Depending on the check boxes for output, input and counter polling, the corresponding information is obtained from the Web-IO at a set interval.
procedure Twebio_ascii_client.timer_pollingTimer(Sender: TObject);
begin
if ClientSocket.Connected and cb_output_polling.Checked then
ClientSocket.IOHandler.Write(’GET /output?PW=’ + ed_password.Text + ’&’);
if ClientSocket.Connected and cb_input_polling.Checked then
ClientSocket.IOHandler.Write(’GET /input?PW=’ + ed_password.Text + ’&’);
if ClientSocket.Connected and cb_counter_polling.Checked then
ClientSocket.IOHandler.Write(’GET /counter?PW=’ + ed_password.Text + ’&’);
end;
The desired interval can be entered in the corresponding text field. When changes are made the timer interval is automatically adjusted.
procedure Twebio_ascii_client.ed_intervalChange(Sender: TObject);
begin
timer_polling.Interval := strtoint(ed_interval.Text);
end;
The sample program supports all common functions of the Web-IO in command string mode, optimized for the Web-IO Digital 4.0 2xIn, 2xOut. 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.