Click here to Skip to main content
6,934,113 members and growing! (12,521 online)
Email Password   helpLost your password?
 
Languages » VB.NET » Samples     Intermediate

.NET Phone Communication Library Part IV - Receive SMS

By twit88

Receive SMS using the atSMS library.
VB.NET2.0, WinXP, Win2003VS2005, Dev
Posted:10 Dec 2006
Views:73,223
Bookmarked:77 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 5.00 Rating: 4.64 out of 5

1

2

3
2 votes, 16.7%
4
10 votes, 83.3%
5

Sample Image - phonesmsrecv.jpg

Introduction

In part II of the article, I showed you how to send SMS using a GSM modem which is built into your mobile phone. However, I did not show you how to detect incoming SMS. To detect new SMS received, it is a bit tricky as different mobile phones from different manufacturers may need different configurations. In this article, I am going to show you how to detect new SMS using the open source atSMS library.

How It Works

In order to detect incoming SMS, you must use the "AT+CNMI" command to set the new message indication to the terminal equipment (TE).

For the "+CNMI" command, the values are in the format of mode,mt,bm,ds,bfr. In order to receive SMS, mt must be 1. For other values, different handsets may require different values to be set. By default, the atSMS library sets mode and mt to 1 which should be sufficient for most handsets. However, you can always use the CheckATCommands function to check your phone supported values.

Take note also that during my testing, it was noticed that for certain Nokia phones only, Class 2 SMS can be detected. For newer models of Nokia phones, e.g., N70, N80, the "+CNMI" is not supported, and you have no way of detecting incoming SMS.

The Code

By setting NewMessageIndication to True, and if your phone supports "+CNMI", the NewMessageReceived event should be raised.

Imports ATSMS

Public Class MainForm

    Private WithEvents oGsmModem As New GSMModem

    Private Sub MainForm_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False
    End Sub

    
    Private Sub btnPhone_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnConnect.Click

        If cboComPort.Text = String.Empty Then
            MsgBox("COM Port must be selected", MsgBoxStyle.Information)
            Return
        End If

        oGsmModem.Port = cboComPort.Text

        If cboBaudRate.Text <> String.Empty Then
            oGsmModem.BaudRate = Convert.ToInt32(cboBaudRate.Text)
        End If

        If cboDataBit.Text <> String.Empty Then
            oGsmModem.DataBits = Convert.ToInt32(cboDataBit.Text)
        End If

        If cboStopBit.Text <> String.Empty Then
            Select Case cboStopBit.Text
                Case "1"
                    oGsmModem.StopBits = Common.EnumStopBits.One
                Case "1.5"
                    oGsmModem.StopBits = Common.EnumStopBits.OnePointFive
                Case "2"
                    oGsmModem.StopBits = Common.EnumStopBits.Two
            End Select
        End If

        If cboFlowControl.Text <> String.Empty Then
            Select Case cboFlowControl.Text
                Case "None"
                    oGsmModem.FlowControl = Common.EnumFlowControl.None
                Case "Hardware"
                    oGsmModem.FlowControl = Common.EnumFlowControl.RTS_CTS
                Case "Xon/Xoff"
                    oGsmModem.FlowControl = Common.EnumFlowControl.Xon_Xoff
            End Select
        End If

        Try
            oGsmModem.Connect()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            Return
        End Try

        Try
            oGsmModem.NewMessageIndication = True
        Catch ex As Exception

        End Try

        btnSendMsg.Enabled = True
        btnSendClass2Msg.Enabled = True
        btnCheckPhone.Enabled = True
        btnDisconnect.Enabled = True

        MsgBox("Connected to phone successfully !", MsgBoxStyle.Information)

    End Sub


    Private Sub btnDisconnect_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnDisconnect.Click
        Try
            oGsmModem.Disconnect()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

        btnSendMsg.Enabled = False
        btnSendClass2Msg.Enabled = False
        btnCheckPhone.Enabled = False
        btnDisconnect.Enabled = False
        btnConnect.Enabled = True

    End Sub

   
    Private Sub btnSendMsg_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnSendMsg.Click
        If txtPhoneNumber.Text.Trim = String.Empty Then
            MsgBox("Phone number must not be empty", MsgBoxStyle.Critical)
            Return
        End If

        If txtMsg.Text.Trim = String.Empty Then
            MsgBox("Phone number must not be empty", MsgBoxStyle.Critical)
            Return
        End If

        Try
            Dim msg As String = txtMsg.Text.Trim
            Dim msgNo As String
            If StringUtils.IsUnicode(msg) Then
                msgNo = oGsmModem.SendSMS(txtPhoneNumber.Text, msg, _
                        Common.EnumEncoding.Unicode_16Bit)
            Else
                msgNo = oGsmModem.SendSMS(txtPhoneNumber.Text, msg, _
                        Common.EnumEncoding.GSM_Default_7Bit)
            End If
            MsgBox("Message is sent. Reference no is " & msgNo, _
                    MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message & ". Make sure your SIM memory" & _ 
                   " is not full.", MsgBoxStyle.Critical)
        End Try

        'Try

        '    Dim storages() As Storage = oGsmModem.GetStorageSetting

        '    Dim i As Integer

        '    txtStorage.Text = String.Empty

        '    For i = 0 To storages.Length - 1

        '        Dim storage As Storage = storages(i)

        '        txtStorage.Text += storage.Name & "(" & _

        '          storage.Used & "/" & storage.Total & "), "

        '    Next

        'Catch ex As Exception

        '    txtStorage.Text = "Not supported"

        'End Try

    End Sub

    Private Sub btnSendClass2Msg_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnSendClass2Msg.Click
        If txtPhoneNumber.Text.Trim = String.Empty Then
            MsgBox("Phone number must not be empty", MsgBoxStyle.Critical)
            Return
        End If

        If txtMsg.Text.Trim = String.Empty Then
            MsgBox("Phone number must not be empty", MsgBoxStyle.Critical)
            Return
        End If

        Try
            Dim msg As String = txtMsg.Text.Trim
            Dim msgNo As String
            If StringUtils.IsUnicode(msg) Then
                msgNo = oGsmModem.SendSMS(txtPhoneNumber.Text, msg, _
                        Common.EnumEncoding.Unicode_16Bit)
            Else
                msgNo = oGsmModem.SendSMS(txtPhoneNumber.Text, msg, _
                        Common.EnumEncoding.Class2_7_Bit)
            End If
            MsgBox("Message is sent. Reference no is " & msgNo, _
                    MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message & ". Make sure your SIM memory is not full.", _
                   MsgBoxStyle.Critical)
        End Try

        'Try

        '    Dim storages() As Storage = oGsmModem.GetStorageSetting

        '    Dim i As Integer

        '    txtStorage.Text = String.Empty

        '    For i = 0 To storages.Length - 1

        '        Dim storage As Storage = storages(i)

        '        txtStorage.Text += storage.Name & "(" & _

        '             storage.Used & "/" & storage.Total & "), "

        '    Next

        'Catch ex As Exception

        '    txtStorage.Text = "Not supported"

        'End Try

    End Sub


    Private Sub oGsmModem_NewMessageReceived(ByVal e As _
            ATSMS.NewMessageReceivedEventArgs) Handles _
            oGsmModem.NewMessageReceived
        txtMsg.Text = "Message from " & e.MSISDN & ". Message - " & _
                      e.TextMessage & ControlChars.CrLf
    End Sub

    Private Sub btnCheckPhone_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnCheckPhone.Click
        MsgBox("Going to analyze your phone. It may take a while", _
                MsgBoxStyle.Information)
        oGsmModem.CheckATCommands()
        If oGsmModem.ATCommandHandler.Is_SMS_Received_Supported Then
            MsgBox("Your phone is able to receive SMS. Message " & _ 
                   "indication command is " & _
                   oGsmModem.ATCommandHandler.MsgIndication, _
                   MsgBoxStyle.Information)
            oGsmModem.NewMessageIndication = True
        Else
            MsgBox("Sorry. Your phone cannot receive SMS", _
                   MsgBoxStyle.Information)
        End If
    End Sub
End Class

In the next article, I will show you how to send vCard, vCalendar, and WAP Push messages using the atSMS library.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

twit88


Member
A programmer for a long time, and still learning everyday.

A supporter for open source solutions, and have written quite a few open source software both in .NET and Java.

Homepage
http://twit88.com/home

Blog
http://twit88.com/blog
Occupation: Web Developer
Location: Malaysia Malaysia

Other popular VB.NET articles:

Article Top
 
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 63 (Total in Forum: 63) (Refresh)FirstPrevNext
GeneralError Sending SMS Message. Unknown exception is sending command. Make sure your SIM Memory is not full PinmemberMember 401217019:28 19 Feb '10  
Generalhow to raise event for Newmessagereceived Pinmemberskinny8621:58 9 Feb '10  
GeneralSIR I NEED YOUR HELP Pinmembermark aldrin reyes23:28 28 Dec '09  
Generalhow to receive sms ????? Pinmemberumerufm22:16 4 Jul '09  
GeneralRe:ATSMS Lib 1.0.0.5 Pinmembermanyara3:19 8 May '09  
GeneralRe:ATSMS Lib 1.0.0.5 Pinmembervan_walkman18:41 16 Jun '09  
GeneralRe:ATSMS Lib 1.0.0.5 PinmemberCarlsoft15:24 8 Dec '09  
Questioni need read all sms from our ATSMS .dll if its possible PinmemberMurugan_vdm22:25 21 Nov '08  
Generalhow to read sms from sim via gsm modem Pinmemberraju_cuet0:31 24 Jul '08  
Questionwant to send sms through phone memory not by sim....... [modified] Pinmemberhpindia3:34 23 Jul '08  
Generalunreadable text in receive text Pinmembereclkq11:08 15 Apr '08  
Generalcode for receive sms using gsm modem that connect to com port Pinmembershee_dee8617:16 8 Apr '08  
Generaldeleting Inbox Pinmemberk.Daniel18:37 22 Feb '08  
some one plz help me how to delete Inbox in phone... im un known to use 'oGsmModem.AutoDeleteNewMessage = True
'ATSMS.ATHandler.CMGD_COMMAND = "6,4"

plz help me...... im in peak time to submit my project.........
my mail id : daniel_praveen_kumar@yahoo.co.in
Smile

daniel
GeneralHow to refresh New messages Pinmembernteng1418:05 9 Jan '08  
Generaljust thanks Pinmembervahid111:34 1 Jan '08  
GeneralUnknown text on received message Pinmemberikkaz20:50 18 Nov '07  
QuestionRe: Unknown text on received message Pinmemberreinaldoa3:59 2 Jul '08  
GeneralError using ATSMS Pinmembercodeprojid23:40 12 Oct '07  
GeneralWhich mobile support Pinmemberparasaniasandip2:44 3 Oct '07  
Questionsending long messege Pinmemberagahii12:47 15 Sep '07  
General3230 query PinmemberA S Bhatia4:01 11 Sep '07  
GeneralHow to Delete Sent Messages Pinmembernteng1418:41 6 Sep '07  
GeneralRe: How to Delete Sent Messages Pinmembermpdesign1:49 11 Sep '07  
GeneralRe: How to Delete Sent Messages Pinmemberagahii7:11 12 Sep '07  
GeneralRe: How to Delete Sent Messages Pinmembermpdesign8:14 12 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 10 Dec 2006
Editor: Smitha Vijayan
Copyright 2006 by twit88
Everything else Copyright © CodeProject, 1999-2010
Web10 | Advertise on the Code Project