ActiveXperts SMS Component

Quicklinks

VBScript 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.


The following instructions will show you how to send an SMS in VBScript using a GSM Modem.

Introduction

In this example we are going to create a Visual Basic Script to receive SMS messages. 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 script

Create a new script using your favorite editor. You can simply use notepad. However, a VBScript editor is recommended, so you can browse through objects, objects properties and object functions.

Create a new VBScript file called Demo.vbs. :

Step 3: Declare and create the ActiveXperts SMS Component objects

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

Insert the following line to declare and create the GSM object:

Set objGsm     = CreateObject("AxSms.Gsm")
Set objMessage = CreateObject("AxSms.Message")

Now, add the following lines to the file to have your fist SMS Component VBScript program:

WScript.Echo "License Status: " & objGsm.LicenseStatus & vbCrLf

Step 4: Select device

Select the device where to receive the messages from.

' Select Device
strDevice                     = AskDevice ( objGsm )

Step 5: Receive SMS messages

You can now receive SMS messages.

The following Powershell code shows how to receive a SMS message using a connected GSM modem:

' Receive messages
WScript.Echo "Receiving messages..."
objGsm.Receive objSmsConstants.GSM_MESSAGESTATE_RECEIVED_UNREAD, False, _
  objSmsConstants.GSM_STORAGETYPE_ALL
WScript.Echo "Receive, result: " & objGsm.LastError
If ( objGsm.LastError <> 0 ) Then 
  objGsm.Close
  WScript.Sleep 3000
  WScript.Quit
End If

Step 6: Retrieve messages

The following code will retrieve and display the messages one by one from the messages we just got from the device.

' Retrieve messages one by one
Set objSmsMessage = objGsm.GetFirstSms
WScript.Echo "GetFirstSms, result: " & objGsm.LastError
While ( objGsm.LastError = 0 )
  WScript.Echo "  Message from: " & objSmsMessage.FromAddress
  
  If (objSmsMessage.BodyFormat = objSmsConstants.BODYFORMAT_DATA) Then
    WScript.Echo "  Message body: <DATA>"
  Else
    WScript.Echo "  Message body: " & objSmsMessage.Body
  End If
  Set objSmsMessage = objGsm.GetNextSms()
  WScript.Echo "GetNextSms, result: " & objGsm.LastError
Wend

Appendix: Full source code

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

' ********************************************************************
'
' ActiveXperts SMS Component
'
' Receive SMS message(s) through connected GSM phone or modem.
' The GSM phone or modem must be connected to your computer
'
' (c) Copyright ActiveXperts Software - www.activexperts.com
'
' ********************************************************************


Option Explicit

' Declare Objects
Dim objGsm, objSmsMessage, objSmsConstants

' Declare Variables
Dim strPin, strDevice

' Create Objects
Set objGsm                    = CreateObject ( "AxSms.Gsm" )
Set objSmsConstants           = CreateObject ( "AxSms.Constants" )

' Display SMS Component Version
WScript.Echo "SMS Component Version " & objGsm.Version

' Display if the ActiveXperts SMS Component is registered.
WScript.Echo "License Status: " & objGsm.LicenseStatus & vbCrLf 

' Set Logfile
Dim fso: Set fso            = CreateObject("Scripting.FileSystemObject")
objGsm.LogFile              = fso.GetSpecialFolder(2) & "\GSM_receivesms.txt"
WScript.Echo "Log file can be found here:"
Wscript.Echo objGsm.LogFile

' Select Device
strDevice                     = AskDevice ( objGsm )

' Open connection to the device
objGsm.Open strDevice
WScript.Echo "Open, result: " & objGsm.GetErrorDescription(objGsm.LastError)
If (objGsm.LastError = 36101) Then '36101 means: modem requires pin code
  ' Enter PinCode (optional)
  strPin                      = ""
  objGsm.Open strDevice, strPin
  WScript.Echo "Open, result: " & objGsm.GetErrorDescription(objGsm.LastError)
End If

If (objGsm.LastError <> 0) Then
  WScript.Echo "Ready."  
  WScript.Quit  
End If

' Receive messages
WScript.Echo "Receiving messages..."
objGsm.Receive objSmsConstants.GSM_MESSAGESTATE_RECEIVED_UNREAD, False, _
  objSmsConstants.GSM_STORAGETYPE_ALL
WScript.Echo "Receive, result: " & objGsm.LastError
If ( objGsm.LastError <> 0 ) Then 
  objGsm.Close
  WScript.Sleep 3000
  WScript.Quit
End If

' Retrieve messages one by one
Set objSmsMessage = objGsm.GetFirstSms
WScript.Echo "GetFirstSms, result: " & objGsm.LastError
While ( objGsm.LastError = 0 )
  WScript.Echo "  Message from: " & objSmsMessage.FromAddress
  
  If (objSmsMessage.BodyFormat = objSmsConstants.BODYFORMAT_DATA) Then
    WScript.Echo "  Message body: <DATA>"
  Else
    WScript.Echo "  Message body: " & objSmsMessage.Body
  End If
  Set objSmsMessage = objGsm.GetNextSms()
  WScript.Echo "GetNextSms, result: " & objGsm.LastError
Wend

' Ready
objGsm.Close
WScript.Echo "Ready."
WScript.Sleep 3000

' ***************************************************************************
' Function AskDevice
' ***************************************************************************
Function AskDevice( objDevice )
  Dim strMessage, strDefaultDevice, strDevice
  
	strMessage = "Connected devices: " & vbCrLf
	strMessage = strMessage & objGsm.FindFirstDevice & vbCrLf

	While objGsm.LastError = 0
		strMessage = strMessage & objGsm.FindNextDevice & vbCrLf
	WEnd

	strMessage = strMessage & "COM1" & vbCrLf & "COM2" & vbCrLf & "COM3" & vbCrLf & "COM4"

	strDefaultDevice = objGsm.FindFirstDevice

	If (objGsm.LastError <> 0) Then
		strDefaultDevice = "COM1"
	End If

	Do
		strDevice = InputBox( strMessage, "Input", strDefaultDevice )
	Loop Until strDevice <> ""

  AskDevice = strDevice
End Function

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.