Delphi sample - Receive SMS text through a COM/USB GSM modem

The ActiveXperts SMS Component is a software development kit (SDK) to enhance an application or script with SMS or Pager functionality. SMS messages can be sent/received using a GSM modem, an SMPP provider or an HTTP compliant SMSC.


Introduction

In this example we are going to create a Delphi console application project named 'gsmreceive.dpr' to receive SMS messages. This demo project will ask the user to give a phone number and a message body on the command prompt. A GSM modem is required for this demo.

Step 1: Download and install The ActiveXperts SMS Component

Download the ActiveXperts SMS Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Delphi Project

Launch Delphi from the Start menu. Choose 'New' from the 'File' menu and select: 'Console Application'.

Delphi

(Click on the picture to enlarge)

Step 3: Refer to the ActiveXperts SMS Component Library and create the objects

Now that a new project has been created, you must add a reference to the ActiveXperts SMS Component in the project to be able to use the ActiveXperts SMS Component objects. To do so, choose 'Import Component...' from the 'Component' menu. The Import Components' dialog appears. Select 'Import a Type Library':

Delphi

(Click on the picture to enlarge)

In the 'Registered Type Libraries' page, select ActiveXperts SMS Component Type Library' and click 'Next':

Borland

(Click on the picture to enlarge)

In the 'Components' page, leave all fields default and click 'Next':

Delphi

(Click on the picture to enlarge)

In the 'Install' page, select 'Create Unit' and click 'Next':

Delphi

(Click on the picture to enlarge)

The interface code is generated now and is shown in the AxSms_TLB tab of the project.

Step 4: Declare and create the object

Add the following AxSms_TLB to the 'Uses' statement to refer to the ActiveXperts SMS Component library:

Delphi

(Click on the picture to enlarge)

Step 4: Declare and create the objects

Add the following AxSms_TLB to the 'Uses' statement to refer to the ActiveXperts SMS Component library:

Delphi

(Click on the picture to enlarge)

The following code will show you how to declare and create the GSM and SMS objects. We will use the 'objGsm' object to send the message itself. The 'objSmsMessage' object will be used to store information of the message and the 'objConstants' object containes constant values releated to the SMS objects.

program gsmsend;

{$APPTYPE CONSOLE}

uses
  SysUtils, ActiveX, Windows,
  AxSms_TLB in '..\typelib-imports\AxSms_TLB.pas';

var
  objGsm: Gsm;
  objConstants: Constants;
  objSmsMessage: Message;
  objOleVariant: OleVariant;
...

You can now create the objects

objGsm := CoGsm.Create;
objConstants := CoSmsConstants.Create;

Step 5: Gather information

The following code will ask the user for modem where to connect to. If a PIN code is required, then it will be prompted you to enter one.

// Get the name of the COM port or TAPI device to connect to
Writeln('Type the name of the device to connect to (e.g. "COM1"):');
strSuggestion := objGsm.FindFirstDevice;
if (objGsm.LastError <> 0) then strSuggestion := 'COM1';
Writeln('Press enter to use ' + strSuggestion);
Readln(strDevice);
if (strDevice = '') then strDevice := strSuggestion;

// Open the GSM device. If a PIN code is required; ask for the PIN code
Writeln('Opening device...');
objGsm.Open(strDevice, '', objConstants.GSM_BAUDRATE_115200);
if objGsm.LastError = 36101 then  // PIN code required
begin
  Writeln('PIN code required, Type the PIN code and press enter:');
  Readln(strPin);
Writeln('Opening device...');
  objGsm.Open(strDevice, strPin, objConstants.GSM_BAUDRATE_115200);
end;

Step 6: Receive SMS messages

You can now receive SMS messages.

The following code shows how to receive an SMS message from a connected GSM modem:

// Retrieving all messages from all storages, don't delete them
Writeln('Querying device...');
objGsm.Receive(objConstants.GSM_MESSAGESTATE_ALL, false,
               objConstants.GSM_STORAGETYPE_ALL, 10000);

Step 7: Retrieve SMS messages

The following code will dieplay all messages, retrieved in the previous step, one by one.

// Iterate over all SMS messages
Writeln('Showing all new and existing messages: ');
objOleVariant := objGsm.GetFirstSms;
while objGsm.LastError = 0 do
begin
  objSmsMessage := IDispatch(objOleVariant) as SmsMessage;
  Writeln('From: ' + objSmsMessage.FromAddress + '; body: ' + objSmsMessage.Body);
  objOleVariant := objGsm.GetNextSms;
end;
Writeln;

Appendix: Full source code

Following you can find the full source code which is also included in the ActiveXperts SMS Component package.

program gsmreceive;

{$APPTYPE CONSOLE}
uses
  SysUtils, ActiveX, Windows,
  AxSms_TLB in '..\typelib-imports\AxSms_TLB.pas';

var
 objGsm: Gsm;
 objConstants: Constants;
 objSmsMessage: Message;
 objSmsReport: GsmDeliveryReport;
 objOleVariant: OleVariant;
 strSuggestion: WideString;
 strDevice: WideString;
 strPin: WideString;
 strLogPath: string;
 arrTempPath: array[0..MAX_PATH] of Char;

begin
  objGsm := CoGsm.Create;
  objConstants := CoSmsConstants.Create;  

  // Display version information about the ActiveXperts SMS Component
  Writeln('SMS Component Version ' + objGsm.Version +
    '; Build ' + objGsm.Build + '; Module ' + objGsm.Module);
  Writeln;

  // Set a log file. This will log all communication with the GSM device
  GetTempPath(MAX_PATH, arrTempPath);
  strLogPath := StrPas(arrTempPath);
  strLogPath := strLogPath + 'gsmreceive.log';
  objGsm.LogFile := strLogPath;
  WriteLn('Log file set to: ' + strLogPath);
  Writeln;

  // Get the name of the COM port or TAPI device to connect to
  Writeln('Type the name of the device to connect to (e.g. "COM1"):');
  strSuggestion := objGsm.FindFirstDevice;
  if (objGsm.LastError <> 0) then strSuggestion := 'COM1';
  Writeln('Press enter to use ' + strSuggestion);
  Readln(strDevice);
  if (strDevice = '') then strDevice := strSuggestion;

  // Open the GSM device. If a PIN code is required; ask for the PIN code
  Writeln('Opening device...');
  objGsm.Open(strDevice, '', objConstants.GSM_BAUDRATE_115200);
  if objGsm.LastError = 36101 then  // PIN code required
  begin
    Writeln('PIN code required, Type the PIN code and press enter:');
    Readln(strPin);
	Writeln('Opening device...');
    objGsm.Open(strDevice, strPin, objConstants.GSM_BAUDRATE_115200);
  end;

  // Show error if we could not open the device
  if objGsm.LastError <> 0 then
  begin
    Writeln('Unable to open device: ' +
      objGsm.GetErrorDescription(objGsm.LastError));
    Writeln('Press ENTER to close the program');
    readln;
    Exit;
  end;
  Writeln;

  // Retrieving all messages from all storages, don't delete them
  Writeln('Querying device...');
  objGsm.Receive(objConstants.GSM_MESSAGESTATE_ALL, false,
    objConstants.GSM_STORAGETYPE_ALL, 10000);

  // Show error if we could not receive messages
  if objGsm.LastError <> 0 then
  begin
    Writeln('Unable to receive messages: ' +
      objGsm.GetErrorDescription(objGsm.LastError));
    objGsm.Close;
    Writeln('Press ENTER to close the program');
    readln;
    Exit;
  end;
  Writeln;

  // Iterate over all SMS messages
  Writeln('Showing all new and existing messages: ');
  objOleVariant := objGsm.GetFirstSms;
  while objGsm.LastError = 0 do
  begin
    objSmsMessage := IDispatch(objOleVariant) as SmsMessage;
    Writeln('From: ' + objSmsMessage.FromAddress + '; body: ' + objSmsMessage.Body);
    objOleVariant := objGsm.GetNextSms;
  end;
  Writeln;

  // Iterate over all delivery reports
  Writeln('Showing all new and existing delivery reports: ');
  objOleVariant := objGsm.GetFirstReport;
  while objGsm.LastError = 0 do
  begin
    objSmsReport := IDispatch(objOleVariant) as GsmDeliveryReport;
    Writeln('From: ' + objSmsReport.FromAddress + '; reference: ' + objSmsReport.Reference);
    objOleVariant := objGsm.GetNextReport;
  end;
  Writeln;

  // Close device
  objGsm.Close;
  Writeln('Press ENTER to close the program');
  Readln;
end.

You can download the full source code of this project from the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/sms-component. There are many other working samples included with the product or on the FTP site.

NOTE: Demo Projects are created with Microsoft Visual Studio 2008

The ActiveXperts SMS Component project ships with a set of Microsoft Visual Studio .NET samples. The projects are created with Microsoft Visual Studio 2008.

Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.