ActiveXperts SMS Component

Quicklinks

Delphi sample - Send 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 'gsmsend.dpr' to send 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 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: SmsConstants;
  objSmsMessage: SmsMessage;
  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 the recipient telephone number and the content of the text message. If a PIN code is required, then it will be prompted you to enter one. This data will be stored in the 'objSmsMessage' object.

// Collect SMS information
Writeln('Send an SMS message to:');
Readln(strTo);
Writeln('Message body:');
Readln(strBody);
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;

Step 6: Send SMS messages

You can now send SMS messages.

The following code shows how to send an SMS message using a connected GSM modem:

// Send the SMS message
objSmsMessage := CoSmsMessage.Create;
objSmsMessage.ToAddress := strTo;
objSmsMessage.Body := strBody;
objOleVariant := OleVariant(objSmsMessage);
objGsm.SendSms(objOleVariant, objConstants.MULTIPART_OK, 20000);

Appendix: Full source code

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

program gsmsend;

{$APPTYPE CONSOLE}

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

var
 objGsm: Gsm;
 objConstants: SmsConstants;
 objSmsMessage: SmsMessage;
 objOleVariant: OleVariant;
 strSuggestion: WideString;
 strDevice: WideString;
 strPin: WideString;
 strTo: WideString;
 strBody: 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 + 'gsmsend.log';
  objGsm.LogFile := strLogPath;
  Writeln('Log file set to: ' + strLogPath);
  Writeln;

  // Collect SMS information
  Writeln('Send an SMS message to:');
  Readln(strTo);
  Writeln('Message body:');
  Readln(strBody);
  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;

  // Send the SMS message
  objSmsMessage := CoSmsMessage.Create;
  objSmsMessage.ToAddress := strTo;
  objSmsMessage.Body := strBody;
  objOleVariant := OleVariant(objSmsMessage);
  objGsm.SendSms(objOleVariant, objConstants.MULTIPART_OK, 20000);

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

  // Close the device and exit to the command prompt
  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.