How to Receive SMS using .NET SMS Library
By admin on Sep 23, 2007 in .NET, Programming, open source
To detect new SMS received, it is a bit tricky as different mobile phones from different manufacturers may need different configurations.
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 SMS 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.
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 SubPrivate Sub btnPhone_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnConnect.ClickIf cboComPort.Text = String.Empty Then
MsgBox(“COM Port must be selected”, MsgBoxStyle.Information)
Return
End IfoGsmModem.Port = cboComPort.Text
If cboBaudRate.Text <> String.Empty Then
oGsmModem.BaudRate = Convert.ToInt32(cboBaudRate.Text)
End IfIf cboDataBit.Text <> String.Empty Then
oGsmModem.DataBits = Convert.ToInt32(cboDataBit.Text)
End IfIf 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 IfIf 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 IfTry
oGsmModem.Connect()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Return
End TryTry
oGsmModem.NewMessageIndication = True
Catch ex As ExceptionEnd Try
btnSendMsg.Enabled = True
btnSendClass2Msg.Enabled = True
btnCheckPhone.Enabled = True
btnDisconnect.Enabled = TrueMsgBox(“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 TrybtnSendMsg.Enabled = False
btnSendClass2Msg.Enabled = False
btnCheckPhone.Enabled = False
btnDisconnect.Enabled = False
btnConnect.Enabled = TrueEnd 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 IfIf txtMsg.Text.Trim = String.Empty Then
MsgBox(“Phone number must not be empty”, MsgBoxStyle.Critical)
Return
End IfTry
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 SubPrivate 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 IfIf txtMsg.Text.Trim = String.Empty Then
MsgBox(“Phone number must not be empty”, MsgBoxStyle.Critical)
Return
End IfTry
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 SubPrivate Sub oGsmModem_NewMessageReceived(ByVal e As _
ATSMS.NewMessageReceivedEventArgs) Handles _
oGsmModem.NewMessageReceived
txtMsg.Text = “Message from “ & e.MSISDN & “. Message - “ & _
e.TextMessage & ControlChars.CrLf
End SubPrivate 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
OpenSourcer | Sep 29, 2007 | Reply
Hi! thank you for providing this SMS Library, it helps a lot easier when developing SMS based application.
But I get this exception when receiving an SMS.
ATSMS.InvalidOpException was unhandled
Message=”Error getting SCA”
Source=”ATSMS”
StackTrace:
at ATSMS.GSMModem.GetSCA()
at ATSMS.GSMModem.get_SMSC()
at ATSMS.GSMModem.DecodeHexPDU(String PDU)
at ATSMS.GSMModem.serialDriver_DataReceived(Object sender, SerialDataReceivedEventArgs e)
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src, SerialDataReceivedEventArgs e)
at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object state)
at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.
RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup
(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
thoughtworks | Sep 30, 2007 | Reply
What is your phone model ? You need to make sure your phone support the AT+CNMI command. Try to use HypterTerminal to check first.
Prashant | Oct 12, 2007 | Reply
Hi,
Is it possible to notify when Mobile phone is connected to PC
admin | Oct 12, 2007 | Reply
This library connects to the mobile phone through infrared, serial or bluetooth using a pre-configured COM port. You can check if the PC is connected to the mobile phone by using the IsConnected method.
Prashant | Oct 16, 2007 | Reply
Hi,
Do we require any additional software like PC Suite provided with phone. could you give some information how we can configure COM port.
Thanks..
admin | Oct 17, 2007 | Reply
Read the post “Use the .NET SMS Library to Retrieve Phone Settings”
Prashant | Nov 21, 2007 | Reply
can we send flash messages through .NET sms lib?
sandhya | Dec 15, 2007 | Reply
hi,
im using ATSMS to send sms from an asp.net web page but it works fine only when 1 user is sending sms. when 2 or more users are accesing the page, they can connect because i am using virtual ports but the sms is not sent.
if u can please help solving this problem
thanks
shirou | Dec 30, 2007 | Reply
hi…
Hi! thank you for providing this SMS Library, it helps me developing SMS gateway for my project…
i wnat to ask !! How it is possible to track down whether SMS was delivered or not.. thanks before
Alwin | Feb 2, 2008 | Reply
hey thoughworks,
can you provide an example on how to catch the delivery reports. You do have an option to enable delivery reports, and the delivery report is indeed received by the phone (check on the phone), but the report isn’t catched by the NewMessageReceived event. Please help…
thanks!
Alwin
Anil | Feb 7, 2008 | Reply
hi… Can i connect the same server to my web page for sending sms
sa | May 15, 2008 | Reply
I am getting garbage when i received a Message. I am using Motrolla L7 Mobile. Pls help.
Isam | May 22, 2008 | Reply
i test it with noki 6220 old model it work
Teddy | Jun 15, 2008 | Reply
Hi, I’m using your library for a project, can you tell me how to check the messages in the SIM memory or phone memory using your library
Dewo | Jun 15, 2008 | Reply
I have similar problem with Teddy, can you give an example on reading messages in the memory? I tried using GsmModem.MessageStore.Message(index).text but it failed, saying index out of bounds no matter what value i give to index.
Robbie | Jun 28, 2008 | Reply
love the work you’ve been doing but i am also having a problem with oGsmModem_NewMessageReceived sub i can only get the incoming call one to work and if you could help with listing and reading th emessages on the phone that would be great too thanks
Mostafa | Aug 4, 2008 | Reply
Hi .
I Have a Nokia7610 .
I will to Recive SMS or Read All My Inbox Phone , But Can’t .
if You see NoKia PcSuite ,
I wish Read Inbox such as PcSuite .
Please take a Sample by (VB-C#).Net or Delphi …. for me .
Please Help Me….
Tanke You .
Good Luck!
Abed | Aug 14, 2008 | Reply
how do I get the ATSMS source code?
thanks.
Abed | Aug 14, 2008 | Reply
Hi, need your advice.
how do we get the trigger from the modem / phone when the newmessage received.?
shirou | Aug 15, 2008 | Reply
to Abed
i’m using timer to read message from phone every 20’s or less
tapic | Oct 25, 2008 | Reply
Hi,
I have observed that multipart SMS messages (long messages) are not correctly handled. Is there a fix for this?
I am fan of this library, I would be glad if you could solve this. Thx.
Vivek | Dec 10, 2008 | Reply
Yes, I too tried experimentin MULTIPART messages.
If someone has the solution, please post it meanwhile I’ll try experimenting.
Regards,
Vivek.