Sub Process_Globals
Private serialgsm As SoftwareSerial
Private bc As ByteConverter
Private astream As AsyncStreams
Private EOL() As Byte = Array As Byte(13, 10)
Private busy As Boolean
Private messageToSend(160) As Byte
Private SMSSlot(4) As Byte
Private retryEmptyMessages As Byte
End Sub
Public Sub Init(rx As Byte, tx As Byte)
serialgsm.Initialize(9600,rx,tx)
astream.Initialize(serialgsm.Stream, "astream_NewData", Null)
astream.WaitForMoreDataDelay = 50 'make sure that we receive full messages
SendCommand("AT+CMGDA=DEL ALL", True)
busy = False
SendCommand("AT+CMGF=1", True)
busy = False
SendCommand("AT+IPR=9600", True)
busy = False
EnableSMSEvents
End Sub
Public Sub EnableSMSEvents As Boolean
Return SendCommand("AT+CNMI=2,1,0,0,0", True)
End Sub
Public Sub SendSMS(PhoneNumber() As Byte, message() As Byte) As Boolean
If busy Then Return False
busy = True
astream.Write("AT+CMGS=""").Write(PhoneNumber).Write("""").Write(EOL)
bc.ArrayCopy(message, messageToSend)
Return True
End Sub
Private Sub SendSMSPart2
busy = True
astream.Write(messageToSend).Write(Array As Byte(0x1a))
End Sub
Private Sub SendSMSPart3
Log("Message was sent successfully")
End Sub
Public Sub Hangup As Boolean
Return SendCommand("ATA", False)
End Sub
Private Sub SendCommand (cmd() As Byte, ResponseExpected As Boolean) As Boolean
If busy Then Return False
busy = ResponseExpected
astream.Write(cmd).Write(EOL)
Delay(200)
Return True
End Sub
Public Sub CheckRegistered
SendCommand("AT+CREG?", True)
End Sub
Private Sub CheckRegisteredResult (buffer() As Byte)
If bc.IndexOf(buffer, "+CREG: 0,1") > -1 Or bc.IndexOf(buffer, "+CREG: 0,5") > -1 Then
Log("Registered")
Else
Log("Not registered")
End If
End Sub
Private Sub SMSArrived1(buffer() As Byte)
Dim i As Int = bc.IndexOf(buffer, ",")
Dim slot() As Byte = bc.SubString(buffer, i + 1)
bc.ArrayCopy(slot, SMSSlot)
Log("SMS received, slot = ", SMSSlot)
retryEmptyMessages = 0
Delay(500)
astream.Write("AT+CMGR=").Write(SMSSlot).Write(EOL)
busy = True
End Sub
Private Sub SMSArrived2(Buffer() As Byte)
busy = True
Dim afterCMGR As Boolean = False
Dim emptyMessage As Boolean = True
For Each line() As Byte In bc.Split(Buffer, Array As Byte(13, 10))
If afterCMGR Then
If line.Length = 0 Or bc.StartsWith(line, "OK") Then Continue
Log("Msg: ", line)
If emptyMessage Then
Main.MessageArrived(line)
emptyMessage = False
End If
Else If bc.StartsWith(line, "+CMGR") Then
Dim counter As Int = 0
For Each s() As Byte In bc.Split(line, """")
If counter = 3 Then
Log("From: ", s)
Else If counter = 7 Then
Log("Date: ", s)
End If
counter = counter + 1
Next
afterCMGR = True
End If
Next
If emptyMessage And retryEmptyMessages < 3 Then
retryEmptyMessages = retryEmptyMessages + 1
Log("Trying to read message again.")
Delay(200)
astream.Write("AT+CMGR=").Write(SMSSlot).Write(EOL)
Else
astream.Write("AT+CMGD=").Write(SMSSlot).Write(EOL)
End If
End Sub
Sub astream_NewData (Buffer() As Byte)
Select True
Case bc.IndexOf(Buffer, "+CREG:") > -1
CheckRegisteredResult(Buffer)
Case bc.IndexOf(Buffer, "AT+CMGS") > -1
SendSMSPart2
Case bc.IndexOf(Buffer, "+CMGS:") > -1
SendSMSPart3
Case bc.IndexOf(Buffer, "+CMTI:") > -1
SMSArrived1(Buffer)
Case bc.IndexOf(Buffer, "+CMGR:") > -1
SMSArrived2(Buffer)
Case Else
Log("****************")
Log(Buffer)
End Select
busy = False
End Sub