Satellite Navigation & the Internet 
by Felix Toran-Marti, Javier Ventura-Traveset, and Juan Carlos de Mateo

Listing One
/////////////////////////////////////////////////////////////////////
// SISNET USer Application Software (UAS) Example                   //
// (c) ESA 2001 by Felix Toran-Marti, Javier Ventura-Traveset and  //
//     Juan Carlos de Mateo.                                       //
// For any question or suggestion, please contact the authors at:  //
// Felix Toran-Marti (ftoran@esa.int ; ftoran@aimme.es)            //
// Javier Ventura-Traveset (jventura@esa.int)                      //
// J.C. de Mateo (jdemateo@estec.esa.nl)                           //
// This application demonstrates how to build a basic SISNET UAS.  //
// The source code can be used as a baseline for integrating       //
// SISNET applications on it. In fact, this application basically  //
// monitors the EGNOS messages received from the SISNET DS.        //
// Only one processing block has been applied, which mission is to //
// analyse the received message types.                             //
////////////////////////////////////////////////////////////////////

//-------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Main.h"
//-------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SINCA"
#pragma resource "*.dfm"
TMainForm *MainForm;
//-------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
        : TForm(Owner)
{
}
//-------------------------------------------------------------------
void __fastcall TMainForm::BtnCloseClick(TObject *Sender)
{
  //Close the application
  Close();
}
//-------------------------------------------------------------------
void __fastcall TMainForm::BtnConnectClick(TObject *Sender)
{
  //Configure the client component
  Client->Address=DataServerIP->Text;
  try
   {
      Client->Port=Port->Text.ToInt();
   }
  catch(...)
   {
      ShowMessage("The port must be an integer number!!!");
      return;
   }
  //Connect to the SISNET Data Server
  try
   {
     Client->Open();
     BtnConnect->Enabled=false;
     BtnClose->Enabled=false;
     BtnDisconnect->Enabled=true;
   }
  catch(...)
   {
      ShowMessage("Connection failed. Please, check the settings.");
   }
}
//-------------------------------------------------------------------
void __fastcall TMainForm::ClientError(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
   //Communications Error
   ShowMessage("Communications error. Please, check IP address and port.");
   //Close the connection
   Client->Close();
   ResetBtns();
   ErrorCode=0;
}
//-------------------------------------------------------------------
void __fastcall TMainForm::ClientConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  //The client is now connected to the SISNET Data Server.
  Status->SimpleText="Connected to the SISNET Data Server!!!";
  //A timer is enabled, in order to send an EGNOS message request each
second.
  Timer->Enabled=true;
}
//-------------------------------------------------------------------
void __fastcall TMainForm::ClientConnecting(TObject *Sender,
      TCustomWinSocket *Socket)
{
  //The Data Server has been found. The connection starts now.
  Status->SimpleText="SISNET Data Server found, connecting...";
}
//-------------------------------------------------------------------
void __fastcall TMainForm::ClientLookup(TObject *Sender,
      TCustomWinSocket *Socket)
{
  //The client socket will search the server socket just now.
  Status->SimpleText="Looking up SISNET Data Server...";
}
//-------------------------------------------------------------------
void __fastcall TMainForm::ClientDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  //The client socket is now disconnected from the server socket.
  Status->SimpleText="Disconnected from the SISNET Data Server.";
  ResetBtns();
}
//-------------------------------------------------------------------
//The client socket has received an answer from the Data Server.
void __fastcall TMainForm::ClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
  //Receive the command sent by the data server
  AnsiString Cmd=Client->Socket->ReceiveText();
  Status->SimpleText="Command received from the Data Server ("+
  TimeToStr(Now())+")";
  //The only allowed message is "*MSG"
  if( ExtractDS2DCField(Cmd,1)!="*MSG" )
    return;
  //Extract and show GPS week and time
  Week->Caption=ExtractDS2DCField(Cmd,2);
  Time->Caption=ExtractDS2DCField(Cmd,3);
  //Extract and decompress the message
  SINCA->Data=ExtractDS2DCField(Cmd,4);
  SINCA->Decompress();
  //Show the decompressed EGNOS message
  EGNOSMsg->Caption=SINCA->Data;
  //Finally, calculate and show the message type
  MsgType->Caption=GetMT(EGNOSMsg->Caption);
}
//-------------------------------------------------------------------
void __fastcall TMainForm::TimerTimer(TObject *Sender)
{
  //Ask the server for an EGNOS message
  Client->Socket->SendText("MSG");
}
//-------------------------------------------------------------------
void __fastcall TMainForm::BtnDisconnectClick(TObject *Sender)
{
  //Disconnect from the Data Server
  Status->SimpleText="Disconnecting from the Data Server...";
  Client->Close();
}
//-------------------------------------------------------------------
//This method returns a field from a DS2DC '*MSG' command. The arguments
//indicate the full command to analyse and the order number of the field to
//extract. Please, note no error handling code is included. It is assumed
//that a correctly-formatted command is received.
AnsiString __fastcall TMainForm::ExtractDS2DCField(AnsiString Cmd,int
FieldNum)
{
  //Ease the search algorithm by placing a comma at the end of the command
  Cmd=Cmd+",";
  int pos=0;
  //Delete previous fields
  for(int n=0;n<FieldNum-1;n++)
   {
     pos=Cmd.Pos(",");
     Cmd.Delete(1,pos);
   }
  //Extract the requested field
  pos=Cmd.Pos(",");
  return Cmd.SubString(1,pos-1);
}
//-------------------------------------------------------------------
//Resets the command buttons to the original state
void __fastcall TMainForm::ResetBtns()
{
  BtnConnect->Enabled=true;
  BtnClose->Enabled=true;
  BtnDisconnect->Enabled=false;
}
//-------------------------------------------------------------------
//Returns the message type of an EGNOS message
int __fastcall TMainForm::GetMT(AnsiString Msg)
{
  //If we number the message's bits starting from 1,
  //the 6-bit message type identifier starts at bit 9.
  //Just the third and fourth hexadecimal digits are significative.
  Msg=Msg.SubString(3,2);
  //Convert to a decimal value (8-bit is enough)
  Msg="0x"+Msg;
  char dec=Msg.ToInt();
  //Apply a mask to extract only the first 6 bits
  dec&=0xFC;
  //Shift the bits two times to the right
  dec/=4;
  return dec;
}





