Wiesemann & Theis GmbH

Networking, sensors and interface technology for industry, office and IT

Tutorial for the Web-IO Digital:

Accessing the Web-IO Digital with Delphi using
binary sockets


A simple to learn high level language, Delphi offers everything you need for programming TCP/IP applications. This makes Delphi a popular aid for creating applications that communicate with the Web-IO Digital using binary sockets. No additional drivers or DLLs are required.

Controlling Web-IO Digital with C Sharp

With the following Delphi program example you can access your Web-IO Digital with its inputs and outputs in a Windows application using binary socket mode.


Preparations


Delphi operating elements

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.

The binary structures

For binary access the necessary binary structures used for communicating with the Web-IO must be defined. A detailed description of these structures can be found in the binary brief overview or in the programming manual for the Web-IO.

The IODriver structure

With its four 16-bit variables the IODriver is the basic structure which is also part of all other structures.

							
Type
  READriver=packed record
    Start_1:word;
    Start_2:word;
    StructType:word;
    StructLength:word;
  end;

  RWriteRegister=packed record
    EADriver : READriver;
    Amount:word;
    Value:word;
  end;

  RSetBits=packed record
    EADriver : READriver;
    Mask:word;
    Value:word;
  end ;

  RRegisterState=packed record
    EADriver : READriver;
    DriverID : word;
    InputValue : word;
    OutputValue : word;
  end;

  RReadCounter=packed record
    EADriver : READriver;
    CounterIndex : word;
  end;

  RCounter=packed record
    EADriver : READriver;
    CounterIndex: word;
    CounterValue: longword;
  end;

  RAllCounter=packed record
    EADriver : READriver;
    CounterNoOf: word;
    CounterValue: array [0..11]of longword;
  end;

  ROptions=packed record
    EADriver : READriver;
    Version : longword;
    Options : longword;
  end;
							
						

For the structures it is important that the individual variables be stored in memory in their exact sequence. Delphi does not do this automatically, especially when variables of different sizes are combined in a structure. To still ensure ordered storage in memory, =packed record is used to specify that there are no unused memory cells between the individual variables.

Starting the program

Setting up the operating elements

To handle asynchronously occurring events such as data reception, a preparatory corresponding thread is defined, stored and started when the program starts.

The group with the operating elements for the Web-IO is first blocked from operation. As soon as a connection is established, all elements are enabled which have a meaningful format.

							

  .....
  .....
  private
    { Private-Deklarationen }
  public
    procedure ClientSocketThreadRun(Sender: TIdThreadComponent);
    { Public-Deklarationen }
  end;
var
  webio_binary_client: Twebio_binary_client;
  ClientSocketThread : TIdThreadComponent;

implementation

{$R *.DFM}

procedure Twebio_binary_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 connection

The connection is opened by entering the IP address of the Web-IO in the text field ed_ip and clicking on the bt_connect button.

							
procedure Twebio_binary_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.

By sending the structure Options to the Web-IO, the latter is instructed to send the changed state when an output is set, using the structure RegisterState.

							
procedure Twebio_binary_client.bt_disconnectClick(Sender: TObject);
begin
  ClientSocket.Disconnect;
end;

procedure Twebio_binary_client.ClientSocketConnected(Sender: TObject);
var
  Options : structOptions;
  SendBuffer : TIdBytes;
begin
  ClientSocketThread.Active := true;
  StatusBar1.SimpleText := ’Connected to ’ + ed_ip.Text;
  bt_connect.Enabled := False;
  bt_disconnect.Enabled := True;
  gb_io.Enabled := True;
  Options.EADriver.Start_1 := 0;
  Options.EADriver.Start_2 := 0;
  Options.EADriver.StructType := $1F0;
  Options.EADriver.StructLength := $10;
  Options.Version := 1;
  Options.Options := 1;
  SendBuffer := RawToBytes(Options, Options.EADriver.StructLength);
  ClientSocket.IOHandler.Write(SendBuffer);
end;
							
						

Sender oof the binary structures

The Indy TIdTCPClient control element cannot directly send structures. Therefore each individual structure must be converted before sending using RawToBytes into a byte array.

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 opened to the Web-IO, the user can use the corresponding program elements to send binary structures to the Web-IO.

Setting the outputs

The user sets the outputs by using the two check boxes cb_outputx. For this the program uses the MouseUP event of this object. When a MouseUP, i.e. releasing the output checkbox, is registered, the program executes the corresponding procedure and passes - depending on whether the checkbox is set or not - the structure SetBit filled with the appropriate values to the Web-IO.

							
procedure Twebio_binary_client.cb_outputMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  SetBits : structSetBits;
  SendBuffer : TIdBytes;
begin
  SetBits.EADriver.Start_1 := 0;
  SetBits.EADriver.Start_2 := 0;
  SetBits.EADriver.StructType := $09;
  SetBits.EADriver.StructLength := $0C;
  if sender = cb_output0 then
  begin
    SetBits.Mask := 1;
    if cb_output0.Checked then
      SetBits.Value := 1
    else
      SetBits.Value := 0;
  end;
  if sender = cb_output1 then
  begin
    SetBits.Mask := 2;
    if cb_output1.Checked then
      SetBits.Value := 2
    else
      SetBits.Value := 0;
  end;
  SendBuffer := RawToBytes(SetBits, SetBits.EADriver.StructLength);
  ClientSocket.IOHandler.Write(SendBuffer);
end;
							
						

Querying output/input status

The user can request the status of the outputs and inputs by clicking on the corresponding button.

							
procedure Twebio_binary_client.bt_outputs_readClick(Sender: TObject);
var
  EADriver : structEADriver;
  SendBuffer : TIdBytes;
begin
  EADriver.Start_1 := 0;
  EADriver.Start_2 := 0;
  EADriver.StructType := $21;
  EADriver.StructLength := $08;
  SendBuffer := RawToBytes(EADriver, EADriver.StructLength);
  ClientSocket.IOHandler.Write(SendBuffer);
end;
							
						

By sending the structure RegisterRequest the switching states of inputs and outputs are requested. The Web-IO replies to this request with the structure RegisterState.

To query only the inputs, click on the bt_inputs button. Here the structure ReadRegister is sent, to which the Web-IO replies with the structure WriteRegister.

							
procedure Twebio_binary_client.bt_inputs_readClick(Sender: TObject);
var
  EADriver : structEADriver;
  SendBuffer : TIdBytes;
begin
  EADriver.Start_1 := 0;
  EADriver.Start_2 := 0;
  EADriver.StructType := $01;
  EADriver.StructLength := $08;
  SendBuffer := RawToBytes(EADriver, EADriver.StructLength);
  ClientSocket.IOHandler.Write(SendBuffer);
end;
							
						

Read/clear counters

You can also query or clear the counter states of the input counters. Here the structure ReadCounter or ReadClearCounter is sent, whereby CounterIndex is used to send the number of the counter. The Web-IO replies with the structure Counter.

							
procedure Twebio_binary_client.bt_counter_readClick(Sender: TObject);
var
  ReadCounter : structReadCounter;
  SendBuffer : TIdBytes;
begin
  ReadCounter.EADriver.Start_1 := 0;
  ReadCounter.EADriver.Start_2 := 0;
  ReadCounter.EADriver.StructType := $B0;
  ReadCounter.EADriver.StructLength := $0A;
  if sender = bt_counter_read0 then ReadCounter.CounterIndex := 0;
  if sender = bt_counter_read1 then ReadCounter.CounterIndex := 1;
  SendBuffer := RawToBytes(ReadCounter, ReadCounter.EADriver.StructLength);
  ClientSocket.IOHandler.Write(SendBuffer);
end;

procedure Twebio_binary_client.bt_counter_clearClick(Sender: TObject);
var
  ReadCounter : structReadCounter;
  SendBuffer : TIdBytes;
begin
  ReadCounter.EADriver.Start_1 := 0;
  ReadCounter.EADriver.Start_2 := 0;
  ReadCounter.EADriver.StructType := $C0;
  ReadCounter.EADriver.StructLength := $0A;
  if sender = bt_counter_clear0 then ReadCounter.CounterIndex := 0;
  if sender = bt_counter_clear1 then ReadCounter.CounterIndex := 1;
  SendBuffer := RawToBytes(ReadCounter, ReadCounter.EADriver.StructLength);
  ClientSocket.IOHandler.Write(SendBuffer);
end;
							
						

The structure type RegisterState ReadAllCounter or ReadClearAllCounter can be used to read or clear all the counters at the same time. The Web-IO replies with the structure AllCounter.

							
procedure Twebio_binary_client.bt_counter_readallClick(Sender: TObject);
var
  EADriver : structEADriver;
  SendBuffer : TIdBytes;
begin
  EADriver.Start_1 := 0;
  EADriver.Start_2 := 0;
  EADriver.StructType := $B1;
  EADriver.StructLength := $08;
  SendBuffer := RawToBytes(EADriver, EADriver.StructLength);
  ClientSocket.IOHandler.Write(SendBuffer);
end;

procedure Twebio_binary_client.bt_counter_clearallClick(Sender: TObject);
var
  EADriver : structEADriver;
  SendBuffer : TIdBytes;
begin
  EADriver.Start_1 := 0;
  EADriver.Start_2 := 0;
  EADriver.StructType := $C1;
  EADriver.StructLength := $08;
  SendBuffer := RawToBytes(EADriver, EADriver.StructLength);
  ClientSocket.IOHandler.Write(SendBuffer);
end;
							
						

Receiving data from the Web-IO

Process and display the received data

The Web-IO returns the appropriate structure depending on the query or triggering event. When data are received the corresponding callback procedure is invoked. For processing the first 8 bytes of the received byte array are BytesToRaw first filled with an IODriver structure. The application recognizes which structure type it is using the variable IODriver.StructType.

  • IODriver.StructType = 8
    structure WriteRegister for the status of the inputs

  • IODriver.StructType = 31 (hex)
    structure RegisterState for the status of the inputs and outputs

  • IODriver.StructType = B4 (hex)
    structure Counter for the value of individual counters

  • IODriver.StructType = B5 (hex)
    structure AllCounter for the value of all counters

For processing the received byte array now uses BytesToRaw to fill the appropriate structure.

The values thus sent are then processed and displayed. For the inputs and outputs WriteRegisterValue, RegisterStae.InputValue and RegisterStae.outputValue are used to send the bit pattern of all inputs and outputs.

							
procedure Twebio_binary_client.ClientSocketThreadRun(Sender: TIdThreadComponent);
var
  ReceiveBuffer : TIdBytes;
  EADriver : structEADriver;
  WriteRegister : structWriteRegister;
  RegisterState : structRegisterState;
  Counter : structCounter;
  AllCounter : structAllCounter;
begin
  while not ClientSocket.IOHandler.InputBufferIsEmpty do
  begin
    SetLength(ReceiveBuffer, length(ReceiveBuffer)+1);
    ReceiveBuffer[length(ReceiveBuffer)-1] := ClientSocket.IOHandler.ReadByte;
  end;
  if length(ReceiveBuffer) > 7 then
    BytesToRaw(ReceiveBuffer, EADriver, 8);
    case EADriver.StructType of

    $08 : begin
            if length(ReceiveBuffer) >= EADriver.StructLength then
            begin
              BytesToRaw(ReceiveBuffer, WriteRegister, EADriver.StructLength);
              if WriteRegister.Value and 1 = 1 then
                cb_input0.Checked := True
              else
                cb_input0.Checked := False;
              if WriteRegister.Value and 2 = 2 then
                cb_input1.Checked := True
              else
                cb_input1.Checked := False;
            end;
          end;
    $31 : begin
            if length(ReceiveBuffer) >= EADriver.StructLength then
            begin
              BytesToRaw(ReceiveBuffer, RegisterState, EADriver.StructLength);
              if RegisterState.InputValue and 1 = 1 then
                cb_input0.Checked := True
              else
                cb_input0.Checked := False;
              if RegisterState.InputValue and 2 = 2 then
                cb_input1.Checked := True
              else
                cb_input1.Checked := False;
              if RegisterState.OutputValue and 1 = 1 then
                cb_output0.Checked := True
              else
                cb_output0.Checked := False;
              if RegisterState.OutputValue and 2 = 2 then
                cb_output1.Checked := True
              else
                cb_output1.Checked := False;
            end;
          end;
    $B4 : begin
            if length(ReceiveBuffer) >= EADriver.StructLength then
            begin
              BytesToRaw(ReceiveBuffer, Counter, EADriver.StructLength);
              if Counter.CounterIndex = 0 then
                ed_counter0.Text := IntToStr(Counter.CounterValue);
              if Counter.CounterIndex = 1 then
                ed_counter1.Text := IntToStr(Counter.CounterValue);
            end;
          end;
    $B5 : begin
            if length(ReceiveBuffer) >= EADriver.StructLength then
            begin
              BytesToRaw(ReceiveBuffer, AllCounter, EADriver.StructLength);
              ed_counter0.Text := IntToStr(AllCounter.CounterValue[0]);
              ed_counter1.Text := IntToStr(AllCounter.CounterValue[1]);
            end;
          end;
    end;
end;
							
						

Polling

Cyclical polling of particular values

In 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_binary_client.timer_pollingTimer(Sender: TObject);
begin
  if ClientSocket.Connected and cb_output_polling.Checked then
    bt_outputs_readClick(self);
  if ClientSocket.Connected and cb_input_polling.Checked then
    bt_inputs_readClick(self);
  if ClientSocket.Connected and cb_counter_polling.Checked then
    bt_counter_readallClick(self);
end;
							
						

The desired interval can be entered in the corresponding text field. When changes are made the timer interval is automatically adjusted.

							
procedure Twebio_binary_client.ed_intervalChange(Sender: TObject);
begin
  timer_polling.Interval := strtoint(ed_interval.Text);
end;
							
						

The sample program supports the common functions of the Web-IO in binary socket mode, optimized for the Web-IO 2x Digital Input, 2x Digital Output. For the other Web-IO models you may have to make changes to the program. A detailed description of the binary structures can be found in the binary brief overview or in the programming manual for the Web-IO.

Download program example


Products



^