Sei sulla pagina 1di 5

Send and Read SMS through a GSM Modem using AT Commands

By Syeda Anila Nusrat, 30 Aug 2010


4.8 !108 "otes#

Download application - 21.6 KB

Introduction
There are many di$$erent %inds o$ a&&'i(ations SMS a&&'i(ations in the mar%et today, and many others are )eing de"e'o&ed. A&&'i(ations in *hi(h SMS messaging (an )e uti'i+ed are "irtua''y un'imited. Some (ommon e,am&'es o$ these are gi"en )e'o*.erson/to/&erson te,t messaging is the most (ommon'y used SMS a&&'i(ation, and it is *hat the SMS te(hno'ogy *as origina''y designed $or. Many (ontent &ro"iders ma%e use o$ SMS te,t messages to send in$ormation su(h as ne*s, *eather re&ort, and $inan(ia' data to their su)s(ri)ers. SMS messages (an (arry )inary data, and so SMS (an )e used as the trans&ort medium o$ *ire'ess do*n'oads. 0)1e(ts su(h as ringtones, *a''&a&ers, &i(tures, and o&erator 'ogos (an )e en(oded in SMS messages. SMS is a "ery suita)'e te(hno'ogy $or de'i"ering a'erts and noti$i(ations o$ im&ortant e"ents. SMS messaging (an )e used as a mar%eting too'. 2n genera', there are t*o *ays to send SMS messages $rom a (om&uter 3 .C to a mo)i'e &honeConne(t a mo)i'e &hone or GSM3G.RS modem to a (om&uter 3 .C. Then use the (om&uter 3 .C and AT (ommands to instru(t the mo)i'e &hone or GSM3G.RS modem to send SMS messages. Conne(t the (om&uter 3 .C to the SMS (enter !SMSC# or SMS gate*ay o$ a *ire'ess (arrier or SMS ser"i(e &ro"ider. Then send SMS messages using a &roto(o' 3 inter$a(e su&&orted )y the SMSC or SMS gate*ay. 2n this arti('e, 2 *i'' e,&'ain the $irst *ay to send, read, and de'ete SMS using AT (ommands. But )e$ore starting, 2 *ou'd 'i%e to e,&'ain a 'itt'e )it a)out AT (ommands.

1. 2.

AT (ommands are instru(tions used to (ontro' a modem. AT is the a))re"iation o$ ATtention. 4"ery (ommand 'ine starts *ith 5AT5 or 5at5. That6s *hy modem (ommands are (a''ed AT (ommands. There are t*o ty&es o$ AT (ommands1. Basi( (ommands are AT (ommands that do not start *ith a 575. 8or e,am&'e, 9 !9ia'#, A !Ans*er#, : !:oo% (ontro'#, and 0 !Return to on'ine data state# are the )asi( (ommands. 2. 4,tended (ommands are AT (ommands that start *ith a 575. A'' GSM AT (ommands are e,tended (ommands. 8or e,am&'e, 7CMGS !Send SMS message#, 7CMG; !;ist SMS messages#, and 7CMGR !Read SMS messages# are e,tended (ommands. 2$ you *ant to get more in$ormation a)out AT (ommands, then you (an get it on my other arti('e on Code.ro1e(t here- htt&-33***.(ode&ro1e(t.(om3<B3system32ntrodu(tiontoAT(ommands.as&,.

AT Commands

Operating Modes

The SMS s&e(i$i(ation has de$ined t*o modes in *hi(h a GSM3G.RS modem or mo)i'e &hone (an o&erate. They are (a''ed SMS te,t mode and SMS .9= mode. !.9= stands $or .roto(o' 9ata =nit.# The mode that a GSM3G.RS modem or mo)i'e &hone is o&erating in determines the synta, o$ some SMS AT (ommands and the $ormat o$ the res&onses returned a$ter

e,e(ution. 2 am using SMS te,t mode in this arti('e.

:o* to Test GSM Modem Conne(ti"ity =sing :y&er Termina'


8irst, $ind the )est GSM modem that suits the needs. 2 tested this a&&'i(ation *ith a >a"e(om 8ASTRAC< M120 . =nderstand the AT Command set re?uired to (ommuni(ate *ith the modem. Conne(t the modem to the (om&uter a((ording to the setu& guide s&e(i$ied in the manua' &ro"ided *ith the GSM modem. .ut a "a'id S2M (ard into the mo)i'e &hone or GSM3G.RS modem. Conne(t your mo)i'e &hone or GSM3G.RS modem to a (om&uter, and set u& the (orres&onding *ire'ess modem dri"er. Run the MS :y&erTermina' )y se'e(ting Start /@ .rograms /@ A((essories /@ Communi(ations /@ :y&erTermina'. 2n the Conne(tion 9es(ri&tion dia'og )o,, enter a name and (hoose an i(on you 'i%e $or the (onne(tion. Then ('i(% the 0< )utton. 2n the Conne(t To dia'og )o,, (hoose the C0M &ort that your mo)i'e &hone or GSM3G.RS modem is (onne(ting to in the Connect using (om)o )o,. 8or e,am&'e, (hoose C0M1 i$ your mo)i'e &hone or GSM3G.RS modem is (onne(ting to the C0M1 &ort. Then ('i(% the 0< )utton. The .ro&erties dia'og )o, (omes out. 4nter the (orre(t &ort settings $or your mo)i'e &hone or GSM3G.RS modem. Then ('i(% the 0< )utton. To $ind the (orre(t &ort settings that shou'd )e used *ith your mo)i'e &hone or GSM3G.RS modem, (onsu't the manua' o$ your mo)i'e &hone or GSM3G.RS modem. Ty&e 5AT5 in the main *indo*. A res&onse 50<5 shou'd )e returned $rom the mo)i'e &hone or GSM3G.RS modem. 2$ A0<B returns, it means your mo)i'e &hone or GSM3G.RS modem is (onne(ted su((ess$u''y. A$ter su((ess$u' (onne(tion o$ the GSM 3G.RS modem *ith .C, you are ready to run this a&&'i(ation. 9o*n'oad the atta(hed &ro1e(t and run the a&&'i(ation.

Sending SMS t roug


.ort Settings

!SM Mode" using A# $o""ands

2n this ta), you *i'' ha"e to do &ort settings *hi(h *i'' )e the same as you did in the hy&er termina' and then ('i(% the 0< )utton. 2$ the modem is (onne(ted su((ess$u''y, a message )o, *i'' a&&ear *ith the message AModem is (onne(tedB.
Collapse | Copy Code

&u)'i( Seria'.ort 0&en.ort!string &Cstr.ortDame, int &CuBaudRate, int &Cu9ataBits, int &CuReadTimeout, int &Cu>riteTimeout# E re(ei"eDo* F ne* AutoReset4"ent!$a'se#G Seria'.ort &ort F ne* Seria'.ort!#G

try E &ort..ortDame F &Cstr.ortDameG //COM1 &ort.BaudRate F &CuBaudRateG //9600 &ort.9ataBits F &Cu9ataBitsG //8 &ort.Sto&Bits F Sto&Bits.0neG //1 &ort..arity F .arity.DoneG //None &ort.ReadTimeout F &CuReadTimeoutG //300 &ort.>riteTimeout F &Cu>riteTimeoutG //300 &ort.4n(oding F 4n(oding.Get4n(oding!5iso/88HI/15#G

&ort.9ataRe(ei"ed 7F ne* Seria'9ataRe(ei"ed4"ent:and'er !&ortC9ataRe(ei"ed#G &ort.0&en!#G &ort.9tr4na)'e F trueG &ort.Rts4na)'e F trueG J (at(h !4,(e&tion e,# E thro* e,G J return &ortG J

Send SMS

2n the se(ond ta), you (an send SMSCollapse | Copy Code

&u)'i( )oo' sendMsg!Seria'.ort &ort, string .honeDo, string Message# E )oo' isSend F $a'seG

try E string re(ie"ed9ata F 4,e(Command!&ort,5AT5, 300, 5Do &hone (onne(ted5#G re(ie"ed9ata F 4,e(Command!&ort,5AT7CMG8F15, 300, 58ai'ed to set message $ormat.5#G String (ommand F 5AT7CMGSFK55 7 .honeDo 7 5K55G re(ie"ed9ata F 4,e(Command!&ort,(ommand, 300, 58ai'ed to a((e&t &honeDo5#G (ommand F Message 7 (har.Con"ert8rom=t$32!2 # 7 5Kr5G re(ie"ed9ata F 4,e(Command!&ort,(ommand, 3000, 58ai'ed to send message5#G //3 seconds i$ !re(ie"ed9ata.4nds>ith!5KrKn0<KrKn5## E isSend F trueG J e'se i$ !re(ie"ed9ata.Contains!54RR0R5## E isSend F $a'seG J return isSendG J (at(h !4,(e&tion e,# E thro* ne* 4,(e&tion!e,.Message#G

J J

Read SMS

2n the third ta), you (an read SMSCollapse | Copy Code

&u)'i( ShortMessageCo''e(tion ReadSMS!Seria'.ort &ort# E // Set up the phone and read the messages ShortMessageCo''e(tion messages F nu''G try E Lregion 4,e(ute Command // Check connection 4,e(Command!&ort,5AT5, 300, 5Do &hone (onne(ted5#G // Use message format !e"t mode 4,e(Command!&ort,5AT7CMG8F15, 300, 58ai'ed to set message $ormat.5#G // Use character set #CC#$3% 4,e(Command!&ort,5AT7CSCSFK5.CC.43MK55, 300, 58ai'ed to set (hara(ter set.5#G // Se&ect S'M storage 4,e(Command!&ort,5AT7C.MSFK5SMK55, 300, 58ai'ed to se'e(t message storage.5#G // (ead the messages string in&ut F 4,e(Command!&ort,5AT7CMG;FK5A;;K55, H000, 58ai'ed to read the messages.5#G Lendregion

Lregion .arse messages messages F .arseMessages!in&ut#G Lendregion J (at(h !4,(e&tion e,# E thro* ne* 4,(e&tion!e,.Message#G J

i$ !messages NF nu''# return messagesG e'se return nu''G J

9e'ete SMS

2n the $ourth and 'ast ta), you (an (ount the num)er o$ SMS and de'ete SMS as *e''.
Collapse | Copy Code

&u)'i( )oo' 9e'eteMsg!Seria'.ort &ort , string &CstrCommand# E

)oo' is9e'eted F $a'seG try E Lregion 4,e(ute Command string re(ie"ed9ata F 4,e(Command!&ort,5AT5, 300, 5Do &hone (onne(ted5#G re(ie"ed9ata F 4,e(Command!&ort,5AT7CMG8F15, 300, 58ai'ed to set message $ormat.5#G String (ommand F &CstrCommandG re(ie"ed9ata F 4,e(Command!&ort,(ommand, 300, 58ai'ed to de'ete message5#G Lendregion

i$ !re(ie"ed9ata.4nds>ith!5KrKn0<KrKn5## E is9e'eted F trueG J i$ !re(ie"ed9ata.Contains!54RR0R5## E is9e'eted F $a'seG J return is9e'etedG J (at(h !4,(e&tion e,# E thro* ne* 4,(e&tion!e,.Message#G J J

Potrebbero piacerti anche