AN #117 - Sending an SMS with the Siemens GSM M1 module |
|
Sending an SMS with the Siemens GSM M1 module
This aplication note was submitted by Warren
Read
Download source code
'Notes
'The code has been written to conserve stack space and
will send the message while encoding it.
'Also this code has not been optimised, so be warned!!!
'msg$ is the text message to send
'snum$ is the sending sms number
'Written for an Siemens GSM Module M1 using PDU mode
'I am happy to reveive any comments or questions regarding
this code.
'Any code improvement suggestions would be appreciated.
'March 2002
'Warren Read
'warren@read.net.au
'SMS variables
Dim Slen0 As Byte
Dim Slen1 As Byte
Dim Slen2 As Byte
Dim Slenl As Byte 'index variable
Dim B1 As Byte
Dim B2 As Byte
Dim B3 As Byte
Dim I$ As String * 1
Dim Rs232$ As String * 1
Dim Spre$ As String * 20 'holds the preamble data
Dim Snum$ As String * 12
Dim Snu1$ As String * 8
Dim Snu2$ As String * 8
Dim Snu3$ As String * 12
Dim Shex$ As String * 2
Dim Shex1$ As String * 2
Dim Str2$ As String * 20
Dim Msg$ As String * 150
Dim I As Byte
Dim I1 As Byte
Dim I2 As Byte
Dim I3 As Byte
Declare Sub Initmodem
Declare Sub Sendsms
Declare Sub Modemdata
config SERIALIN= BUFFERED,size=40
'-------------------------------------------------------------------------------
'Initialise Modem
'-------------------------------------------------------------------------------
Sub Initmodem
Print "AT"
Call Modemdata
'Reset modem
Print "atz"
Call Modemdata
'Set Result Code To Digit
Print "ate0"
Call Modemdata
'Disable Answering
Print "ats0=0"
Call Modemdata
'Change to PDU mode
Print "at+cmgf=0"
Call Modemdata
'Set cpms to sim
Print "at+cpms=sm"
Wait 2
Call Modemdata
Wait 3
End Sub
'-------------------------------------------------------------------------------
' send SMS message to the phone
'-------------------------------------------------------------------------------
Sub Sendsms
'Prepare the SMS number
Slenl = Slen0 Mod 2 'find out if it is an even or odd number
If Slenl > 0 Then Snum$ = Snum$ + "F" 'if its odd add a blank
Slen0 = Len(snum$)
Shex$ = Hex(slen0)
'Convert the number into some weird format so that it will
be accepted by the modem
'I am guessing that the GSM standard was written by an
Economist, no one else
'could have created such mess.
Snu3$ = ""
For I = 1 To Slen0 Step 2
I1 = I + 1
Snu1$ = Mid(snum$ , I , 1)
Snu2$ = Mid(snum$ , I1 , 1)
Snu3$ = Snu3$ + Snu2$
Snu3$ = Snu3$ + Snu1$
Next
'Add some preamble code
'Note you may need to remove "00" for some modem types
Spre$ = Snu3$
Spre$ = Spre$ + "0001a7"
'Calc the length of the message and add it to the preamble
Slen0 = Len(msg$)
Shex1$ = Hex(slen0)
Spre$ = Spre$ + Shex1$
'Calc the total SMS string length
'Who ever wrote the SMS standard was either insane or
wanted to make it
'as difficult as possible
Slen1 = Len(spre$)
Slen2 = 0
If Slen0 > 8 Then
Slen2 = Slen0 / 8
Slen0 = Slen0 - Slen2
End If
Slen1 = Slen1 + 8
Slen1 = Slen1 / 2
Slen1 = Slen1 + Slen0
'This is the SMS service centre number, in international format,
'note this number does not have to be converted into a
weird format
'For Australia it is +61 and the service centre number
Drop the leading 0 from the
'number. The number
is different for each service provider.
'The example below is for Optus. {034} is a "
Print "at+csca={034}+61411990003{034}"
Call Modemdata
Print "at+cnmi=2,0,0,0"
Call Modemdata
'Slen1 is the total length of the SMS string
Snu1$ = "at+cmgs=" + Str(slen1)
Print Snu1$
Call Modemdata
'Send the preamble to the phone
'You may need to adjust the wait times depending on the
modem
Print "1100";
Waitms 200
Print Shex$;
Waitms 200
Print "81";
Waitms 100
For I = 1 To Len(spre$)
I$ = Mid(spre$ , I , 1)
Print I$;
Waitms 150
Next
'Encode and send the message to the modem
I = 1
I1 = 1
I2 = 7
Locate 4 , 1
For I = 1 To Len(msg$)
Snu1$ = Mid(msg$ , I , 1)
B1 = Asc(snu1$)
Snu1$ = Bin(b1)
Snu1$ = Right(snu1$ , 7)
Snu1$ = Mid(snu1$ , 1 , I2)
I3 = I + 1
Snu2$ = Mid(msg$ , I3 , 1)
B2 = Asc(snu2$)
Snu2$ = Bin(b2)
Snu2$ = Right(snu2$ , 7)
Snu2$ = Mid(snu2$ , I2 , I1)
Snu3$ = Snu2$ + Snu1$
B3 = Binval(snu3$)
Shex$ = Hex(b3)
I1 = I1 + 1
I2 = I2 - 1
If I1 = 8 Then
I1 = 1
I2 = 7
I = I + 1
End If
Print Shex$;
Waitms 150
Next
Print "{094}{122}"
'To be sure that the message has been sent wait 15
seconds,
'This is a little conservative.
Wait 15
Call Modemdata
Wait 1
'This will check for any errors in sending and should give
you an error code
Print "at+ceer"
Call Modemdata
Wait 5
End Sub
'-------------------------------------------------------------------------------
'get the data from the modem
'-------------------------------------------------------------------------------
Sub Modemdata
Wait 1
Str2$ = ""
While _RS_HEAD_PTR0 <>_RS_TAIL_PTR0 'check the status of the RS232 register
Rs232$ = Inkey()
If Asc(rs232$) > 32 Then 'Filter out LF and CR etc
Str2$ = Str2$ + Rs232$
End If
Wend
Wait 1
End Sub
End 'end program
|