Powershell 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 Powershell 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 Powershell editor is recommended, so you can browse through objects, objects properties and object functions.

You're now able to write a more advanced Powershell script to receive SMS using SMS Component.

Step 3: Declare and create the ActiveXperts SMS Component objects

Create a new Powershell file called DEMO.PS1.

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:

$objGsm          = new-object -comobject AxSms.Gsm
$objSmsMessage   = new-object -comobject AxSms.Message
$objSmsConstants = new-object -comobject AxSms.Constants

Step 4: Select device

The following code will ask the user for the device where to recive messages from. This data will be stored in the 'strDevice' object. If any error occours while trying to open the connection, the script will end.

# Select Device
$strDevice = AskDevice( $objGsm )

If ($strDevice -eq $Empty)
{
  write-host "No devices or COM ports where found."
  write-host "Ready."
  exit  
}

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
write-host "Receiving messages..."
$objGsm.Receive($objSmsConstants.GSM_MESSAGESTATE_ALL, $False, 
                $objSmsConstants.GSM_STORAGETYPE_ALL)
$strResult = "Receive, result: " + $objGsm.LastError
write-host $strResult

if ($objGsm.LastError -ne 0)
{
  $objGsm.Close()
  write-host "Ready."  
  exit
}

Step 6: Retrieve messages

The following code will display the messages one by one:

# Retrieve messages one by one
$objSmsMessage = $objGsm.GetFirstSms()
$strResult = "GetFirstSms: " + $objGsm.LastError
write-host $strResult
while ($objGsm.LastError -eq 0)
{
  write-host "  Message from:"$objSmsMessage.FromAddress
  
  if ($objSmsMessage.BodyFormat -eq $objSmsConstants.BODYFORMAT_HEX)
  {
    write-host "  Message body: <DATA>"
  }
  else
  {
    write-host "  Message body:"$objSmsMessage.Body
  }
  
  $objSmsMessage = $objGsm.GetNextSms()
  $strResult = "GetNextSms: " + $objGsm.LastError
  write-host $strResult  
}

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
#
# Send a text SMS message through a connected GSM phone or modem.
# The GSM phone or modem must be connected to your computer.
#
# (c) Copyright ActiveXperts Software - www.activexperts.com
#
# ***************************************************************************

# ***************************************************************************
# Function AskDevice
# ***************************************************************************
Function AskDevice ( $objGsm )
{  
  $strMessage = "Connected devices:"
  
  $strDevice = $objGsm.FindFirstDevice()
  
  While ($objGsm.LastError -eq 0)
  {     
     $strMessage += "`r`n" + $strDevice
     $strDevice = $objGsm.FindNextDevice()
  }
  
  # Add COM Ports.
  # Get first COM Port.
  $strPort = $objGsm.FindFirstPort()
  
  While ($objGsm.LastError -eq 0)
  {
    $strMessage += "`r`n" + $strPort
    $strPort = $objGsm.FindNextPort()
  }

  $strDefaultDevice = $objGsm.FindFirstDevice()
  
  If ($objGsm.LastError -ne 0)
  {
    $strDefaultDevice = $objGsm.FindFirstPort()
  } 
  
  # Display Devices
  if ($strDefaultDevice -ne $Empty)
  {
    write-host $strMessage
    write-host $Empty
    $strSelectedDevice = ReadInput "Select Device:" $strDefaultDevice $False
  }
  

  return $strSelectedDevice 
}

# ***************************************************************************
# Function ReadInput
# ***************************************************************************
Function ReadInput($strPrompt, $strDefaultValue, $bAllowEmpty)
{ 
  $strReturn = ""  
  If ($strDefaultValue -ne "")
  {
     $strPrompt += " leave empty for " + $strDefaultValue
  }
  
  Do 
  {       
    write-host $strPrompt
    $strReturn = read-host
    
    If ($strReturn -eq "" -and $strDefaultValue -ne "")
    {
      $strReturn = $strDefaultValue
      write-host $strReturn
    }
    elseif ($strReturn -eq "" -and $bAllowEmpty -eq $True)
    {
      break
    }   
  } While ($strReturn -eq "") 
  
  write-host ""
  return $strReturn
}

# ***************************************************************************
# The script itself
# ***************************************************************************
cls

$objGsm = new-object -comobject AxSms.Gsm
$objSmsMessage = new-object -comobject AxSms.Message
$objSmsConstants = new-object -comobject AxSms.Constants


# Display SMS Component Version
write-host "SMS Component Version" $objGsm.Version "; "
                                    "Build" $objGsm.Build "; "
                                    "Module" $objGsm.Module

# Display if the ActiveXperts SMS Component is registered.
write-host "License Status:" $objGsm.LicenseStatus
write-host ""

# Set Logfile
$objGsm.Logfile = $env:temp + "\GSM_receivesms.txt"
write-host "Log file can be found here:"
write-host $objGsm.Logfile
write-host ""

# Select Device
$strDevice = AskDevice( $objGsm )

If ($strDevice -eq $Empty)
{
  write-host "No devices or COM ports where found."
  write-host "Ready."
  exit  
}

# Open connection to the device
$objGsm.Open($strDevice)

$strResult = "Open, result: " + $objGsm.LastError
write-host $strResult
#36101 means: modem requires pin code, see also: www.activexperts.com/support/errorcodes
If ($objGsm.LastError -eq 36101) 
{
  # Enter PinCode 
  $strPin = ReadInput "Enter PIN code" "" $False
  $objGsm.Open($strDevice, $strPin)
  $strResult = "Open, result: " + $objGsm.LastError
  write-host $strResult
}

If ($objGsm.LastError -ne 0)
{ 
  $objGsm.Close()
  write-host "Ready."  
  exit
}

# Receive messages
write-host "Receiving messages..."
$objGsm.Receive($objSmsConstants.GSM_MESSAGESTATE_ALL, $False, 
                $objSmsConstants.GSM_STORAGETYPE_ALL)
$strResult = "Receive, result: " + $objGsm.LastError
write-host $strResult

if ($objGsm.LastError -ne 0)
{
  $objGsm.Close()
  write-host "Ready."  
  exit
}

# Retrieve messages one by one
$objSmsMessage = $objGsm.GetFirstSms()
$strResult = "GetFirstSms: " + $objGsm.LastError
write-host $strResult
while ($objGsm.LastError -eq 0)
{
  write-host "  Message from:"$objSmsMessage.FromAddress
  
  if ($objSmsMessage.BodyFormat -eq $objSmsConstants.BODYFORMAT_HEX)
  {
    write-host "  Message body: <DATA>"
  }
  else
  {
    write-host "  Message body:"$objSmsMessage.Body
  }
  
  $objSmsMessage = $objGsm.GetNextSms()
  $strResult = "GetNextSms: " + $objGsm.LastError
  write-host $strResult  
}

# Ready
$objGsm.Close()
write-host "Ready."
exit

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.