Click here to Skip to main content
Click here to Skip to main content

.NET Phone Communication Library Part IV - Receive SMS

, 10 Dec 2006
Rate this:
Please Sign up or sign in to vote.
Receive SMS using the atSMS library.

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

Share

About the Author

twit88
Web Developer
Malaysia Malaysia
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

Comments and Discussions

 
QuestionDelete Stored SMS PinmemberBenjamin PC249-Jul-14 4:09 
QuestionThanks a looooooooooooot .This is great.......... I tried so many, but this is the better solution. I need another favor buddy. PinprofessionalNipuna Sandas12-May-14 8:46 
Questiondelete storge Pinmemberalmofi28-Jul-13 16:50 
QuestionThanks Buddy you were a great help PinmemberMohammad Nasir Qureshi21-Jun-13 2:42 
Questionnice one and very good perfect connection PinmemberAlvinApolinario4-Mar-13 23:38 
Questionthe code PinmemberAngustong16-Jan-13 23:10 
there is an error for import ATMS. how to solve this error?
Questionobject reference not set to an instance of objectmake sure your sim memory is not full Pinmembervinodpii23-Oct-11 23:28 
QuestionHELP: send LongSMS [modified] Pinmemberrember2417-Apr-11 23:17 
GeneralMy vote of 5 Pinmemberleesong13-Feb-11 2:30 
Generalhello i have some suguessions Pinmemberleesong13-Feb-11 0:46 
QuestionDelete code does not work Pingrouphkkarri14-Sep-10 23:26 
Generalmanage and get delivery messege PinmemberAhmad Aghili2-Sep-10 2:39 
Generalsending option not working [modified] Pinmemberteninfotech8-Aug-10 21:27 
GeneralError Sending SMS Message. Unknown exception is sending command. Make sure your SIM Memory is not full PinmemberMember 401217019-Feb-10 19:28 
GeneralRe: Error Sending SMS Message. Unknown exception is sending command. Make sure your SIM Memory is not full Pinmemberalshaqsi21-Mar-10 15:40 
GeneralRe: Error Sending SMS Message. Unknown exception is sending command. Make sure your SIM Memory is not full PinmemberArieljays8-Jan-13 10:39 
GeneralRe: Error Sending SMS Message. Unknown exception is sending command. Make sure your SIM Memory is not full Pinmemberzxcvbnm1234564-Jun-13 0:21 
Questionhow to raise event for Newmessagereceived Pinmemberskinny869-Feb-10 21:58 
GeneralSIR I NEED YOUR HELP Pinmembermark aldrin reyes28-Dec-09 23:28 
Questionhow to receive sms ????? Pinmemberumerufm4-Jul-09 22:16 
GeneralRe:ATSMS Lib 1.0.0.5 Pinmembermanyara8-May-09 3:19 
GeneralRe:ATSMS Lib 1.0.0.5 Pinmembervan_walkman16-Jun-09 18:41 
GeneralRe:ATSMS Lib 1.0.0.5 PinmemberCarlsoft8-Dec-09 15:24 
Questioni need read all sms from our ATSMS .dll if its possible PinmemberMurugan_vdm21-Nov-08 22:25 
Questionhow to read sms from sim via gsm modem Pinmemberraju_cuet24-Jul-08 0:31 
Questionwant to send sms through phone memory not by sim....... [modified] Pinmemberhpindia23-Jul-08 3:34 
Generalunreadable text in receive text Pinmembereclkq15-Apr-08 11:08 
Generalcode for receive sms using gsm modem that connect to com port Pinmembershee_dee868-Apr-08 17:16 
Generaldeleting Inbox Pinmemberk.Daniel22-Feb-08 18:37 
QuestionHow to refresh New messages Pinmembernteng149-Jan-08 18:05 
Generaljust thanks Pinmembervahid11-Jan-08 11:34 
GeneralUnknown text on received message Pinmemberikkaz18-Nov-07 20:50 
QuestionRe: Unknown text on received message Pinmemberreinaldoa2-Jul-08 3:59 
GeneralError using ATSMS Pinmembercodeprojid12-Oct-07 23:40 
GeneralWhich mobile support Pinmemberparasaniasandip3-Oct-07 2:44 
Questionsending long messege Pinmemberagahii15-Sep-07 12:47 
General3230 query PinmemberA S Bhatia11-Sep-07 4:01 
QuestionHow to Delete Sent Messages Pinmembernteng146-Sep-07 18:41 
AnswerRe: How to Delete Sent Messages Pinmembermpdesign11-Sep-07 1:49 
AnswerRe: How to Delete Sent Messages Pinmemberagahii12-Sep-07 7:11 
GeneralRe: How to Delete Sent Messages Pinmembermpdesign12-Sep-07 8:14 
GeneralRe: How to Delete Sent Messages Pinmembermark duatin11-Jul-08 21:58 
GeneralRe: How to Delete Sent Messages Pinmembermpdesign12-Jul-08 13:37 
GeneralRe: How to Delete Sent Messages Pinmemberkaki4568-Feb-09 8:30 
GeneralRe: How to Delete Sent Messages Pinmembermpdesign8-Feb-09 12:45 
GeneralRe: How to Delete Sent Messages Pinmemberkaki4569-Feb-09 2:16 
GeneralRe: How to Delete Sent Messages Pinmembernomi_a10-Jul-09 20:53 
GeneralRe: How to Delete Sent Messages Pinmembermpdesign12-Jul-09 23:09 
GeneralRe: How to Delete Sent Messages Pinmemberelixirdagger1-Jan-12 13:44 
GeneralRe: How to Delete Sent Messages Pinmembermpdesign1-Jan-12 13:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 10 Dec 2006
Article Copyright 2006 by twit88
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid