ActiveXperts SMS Component

Quicklinks

Visual Basic .NET 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 use Visual Studio 2008 to create a console application project named 'DemoApp' in a solution named 'DemoSolution'. We are going to store this project in the directory 'C:\MyProjects'. All of these names can be changed according to your preferences. 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 Visual Basic .NET Project

Launch Microsoft Visual Studio from the Start menu. Choose 'New' from the 'File' menu and click on 'Project'. In the 'New Project' dialog, select a Visual Studio template. Select a name for the application and a name for the solution. Also, select the directory where you want to store the project:

Visual Basic .NET

(Click on the picture to enlarge)

Step 3: Refer to the ActiveXperts SMS Component Library

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 the ActiveXperts SMS Component objects. To do so, choose 'Add Reference...' from the 'Project' menu. In the 'Add Reference' dialog that pops up, select the 'COM' tab and select the 'ActiveXperts SMS Component Type Library' as shown in the following picture:

Visual Basic.NET

(Click on the picture to enlarge)

Click 'OK' to close the 'Add Reference' dialog.

Step 4: 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 send 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.

Imports AxSms
Imports System.IO
Imports System.Threading

Module GsmProgram

  Sub Main()
    Dim objGsm          As Gsm = New Gsm()
    Dim objSmsMessage   As Message = New Message()
    Dim objSmsConstants As Constants = New Constants()
    Dim objStatusReport As GsmDeliveryReport = Nothing
    ...
  End Sub  

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 'objMessage' object. If any error occours while trying to open the connection, the script will end.

' 36101 means: modem requires pin code, see also: www.activexperts.com/support/errorcodes
If (objGsm.LastError = 36101) Then 
  objGsm.Open(strDevice, ReadInput("Enter PIN code", False), 0)
  Console.WriteLine("Open, result: " & 
                  objGsm.LastError.ToString() & " (" & 
                  objGsm.GetErrorDescription(objGsm.LastError) & ")")
End If

If (objGsm.LastError <> 0) Then
  GoTo _EndMain
End If

' Message settings
objMessage.ToAddress = ReadInput("Enter recipient: ", False)
objMessage.BodyFormat = objConstants.BODYFORMAT_TEXT
objMessage.Body = ReadInput("Enter message body: ", False)
objMessage.RequestDeliveryReport = ReadYesNo("Do you want a status report (y/n)? ")

Step 6: Send SMS messages

The following code shows how to send an SMS message using the data that was stored in the 'objMessage' and 'objConstants' objects. The timeout value is set to 25 seconds, which means that the modem will try to send the message for 25 seconds, after this value it will stop trying.

' Send the message !
objGsm.SendSms(objMessage, objConstants.MULTIPART_OK, 25000)
Console.WriteLine("SendSms, result: " & 
                  objGsm.LastError.ToString() & " (" & 
                  objGsm.GetErrorDescription(objGsm.LastError) & ")")
If (objGsm.LastError <> 0) Then
  GoTo _EndMain
End If

Appendix: Full source code

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

'-----------------------------------------------------------------------
' <copyright file="GsmProgram.vb" company="ActiveXperts">
'     Copyright (c) ActiveXperts Software - www.activexperts.com
' </copyright>
' <author>ActiveXperts</author>
'-----------------------------------------------------------------------

Imports AxSms
Imports System.IO
Imports System.Threading

Module GsmProgram

  Sub Main()
    Dim objGsm As Gsm = New Gsm() ' Create instance of COM Object
    Dim objMessage As Message = New Message() ' Create instance of COM Object
    Dim objConstants As Constants = New Constants() ' Create instance of COM Object
    Dim objStatusReport As GsmDeliveryReport = Nothing

    Dim strReference As String, strDevice As String

    ' Set Logfile (optional, for debugging purposes)
    objGsm.LogFile = String.Format("{0}{1}", Path.GetTempPath(), "GsmLog.txt")

    Console.WriteLine("SMS Component: Build: " & objGsm.Build & ";" +
                                                "Module: " & objGsm.Module)
    Console.WriteLine(String.Empty)

    Console.WriteLine("Log file can be found here:")
    Console.WriteLine(objGsm.LogFile)
    Console.WriteLine(String.Empty)

    strDevice = ReadDevice(objGsm)
    If (strDevice = String.Empty) Then
      GoTo _EndMain
    End If

    objGsm.Open(strDevice, String.Empty, 0)
    Console.WriteLine("Open, result: " & 
                      objGsm.LastError.ToString() & " (" & 
                      objGsm.GetErrorDescription(objGsm.LastError) & ")")
    ' 36101 means: modem requires pin code, see also: www.activexperts.com/support/errorcodes
    If (objGsm.LastError = 36101) Then 
      objGsm.Open(strDevice, ReadInput("Enter PIN code", False), 0)
      Console.WriteLine("Open, result: " & 
                      objGsm.LastError.ToString() & " (" & 
                      objGsm.GetErrorDescription(objGsm.LastError) & ")")
    End If

    If (objGsm.LastError <> 0) Then
      GoTo _EndMain
    End If

    ' Message settings
    objMessage.ToAddress = ReadInput("Enter recipient: ", False)
    objMessage.BodyFormat = objConstants.BODYFORMAT_TEXT
    objMessage.Body = ReadInput("Enter message body: ", False)
    objMessage.RequestDeliveryReport = ReadYesNo("Do you want a status report (y/n)? ")

    Dim bSearchStatusReport As Boolean = objMessage.RequestDeliveryReport

    ' Send the message !
    objGsm.SendSms(objMessage, objConstants.MULTIPART_OK, 25000)
    Console.WriteLine("SendSms, result: " & 
                      objGsm.LastError.ToString() & " (" & 
                      objGsm.GetErrorDescription(objGsm.LastError) & ")")
    If (objGsm.LastError <> 0) Then
      GoTo _EndMain
    End If

    strReference = objMessage.Reference
    Console.WriteLine("Message Reference (can be used with status reports): " & strReference)

    ' Status Report
    Do While (bSearchStatusReport)
      objGsm.Receive(objConstants.GSM_MESSAGESTATE_ALL, False, 
                     objConstants.GSM_STORAGETYPE_ALL, 25000)
      Console.WriteLine("Receive, result: " & 
                        objGsm.LastError.ToString() & " (" & 
                        objGsm.GetErrorDescription(objGsm.LastError) & ")")

      If (objGsm.LastError <> 0) Then
        Exit Do
      End If

      objStatusReport = objGsm.GetFirstReport()

      Do While (objGsm.LastError = 0)
        If (objStatusReport.Reference = strReference) Then
          bSearchStatusReport = False
          Console.WriteLine(String.Format("Status Report {0} received!", 
                            objStatusReport.Reference))
          objGsm.DeleteReport(objStatusReport)
          Console.WriteLine("DeleteReport, result: " & 
                            objGsm.LastError.ToString() & " (" & 
                            objGsm.GetErrorDescription(objGsm.LastError) & ")")
          Exit Do
        Else
          objStatusReport = objGsm.GetNextReport()
        End If
      Loop

      If (bSearchStatusReport) Then
        Console.WriteLine("Status report not found yet.")
        Thread.Sleep(3000)
      End If
    Loop

_EndMain:
    objGsm.Close() ' Make sure the port gets closed

    Console.WriteLine("Ready.")
    Console.WriteLine("Press <ENTER> to continue.")
    Console.ReadLine()

End Sub

Function ReadYesNo(ByVal strTitle As String)
    Dim strInput As String
    Console.Write(strTitle)
    strInput = Console.ReadLine()
    Console.WriteLine()
    If (strInput.Length > 0) Then
      If (strInput(0) = "y" Or strInput(0) = "Y") Then
        Return True
      Else
        Return False
      End If
    Else
      Return False
    End If
End Function

Function ReadInput(ByVal strTitle As String, ByVal bAllowEmpty As Boolean)

    Dim strInput As String
    Dim strReturn As String = String.Empty

    Console.WriteLine(strTitle)
    Do
      Console.Write("  > ")
      strInput = Console.ReadLine()
      If (strInput.Length > 1) Then
        strReturn = strInput
      End If
    Loop While (strReturn = String.Empty And Not bAllowEmpty)

    Return strReturn
End Function

Function ReadDevice(ByVal objGsm As Gsm)
    Dim strInput As String = String.Empty
    Dim strDevice As String = String.Empty
    Dim strPort As String = String.Empty
    Dim strMessage As String = String.Empty
    Dim strDevices As String = String.Empty
    Dim strSelectedDevice As String = String.Empty
    Dim i As Integer = 0
    Dim lsDeviceNames As List(Of String) = New List(Of String)

    strMessage = "Select a device: "

    strDevice = objGsm.FindFirstDevice()

    While (objGsm.LastError = 0)
      strDevices = strDevices & vbCrLf & "  " & i.ToString() & ": " + strDevice
      lsDeviceNames.Add(strDevice)
      strDevice = objGsm.FindNextDevice()
      i = i + 1
    End While

    strPort = objGsm.FindFirstPort()
    While (objGsm.LastError = 0)
      strDevices = strDevices & vbCrLf & "  " & i.ToString() & ": " + strPort
      lsDeviceNames.Add(strPort)
      strPort = objGsm.FindNextPort()
      i = i + 1
    End While

    If (strDevices = String.Empty) Then
      Console.WriteLine("No devices or COM ports where found.")
    Else
      Console.WriteLine(strMessage & strDevices)
      While (strSelectedDevice = String.Empty)
        Console.Write("  > ")
        strInput = Console.ReadLine()

        Dim iDevice As Integer

        If (Not Integer.TryParse(strInput, iDevice)) Then
          iDevice = 0
        End If

        If (iDevice > lsDeviceNames.Count - 1) Then
          iDevice = 0
        End If
        strSelectedDevice = lsDeviceNames(iDevice)
      End While
      Console.WriteLine("  Selected device: " & strSelectedDevice & vbCrLf)
    End If
    Return strSelectedDevice
  End Function
End Module

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.