Shop OBEX P1 Docs P2 Docs Learn Events
pointer towards help parsing gsm / sms receive data — Parallax Forums

pointer towards help parsing gsm / sms receive data

jon123456789jon123456789 Posts: 11
edited 2009-10-21 07:17 in Propeller 1
Hi,
I'm trying to write the code to receive and act upon a SMS message received on a GSM modem.
I've successfully got the Prop / gsm modem to send a text message based on the status of input pins (i'm linking the GSM modem to my house alarm to send me a text message depending on certain status') but I also want to request a SMS message telling me that all is ok and to also get an update of credit left on the pay as you go sim card.

See my shortened code below to receive the sms message:

I would like to extract the senders mobile number in between the " " just after the +CMT: and use it as authentication to only send a reply to a phone number I have approved (i.e. mine).
I would then like to extract the reply text message which could be the reply from the balance check (ie. send BALANCE to 789000 on my phone) or the message which I send to the phone which could for instance just be "STATUS", I could then use a STRCOMP to act upon that request and also verify the senders phone number.

My question is (and I have spent many many hours trying to get this to work by trawling the message board and trying to take other code apart without success and I have read the manual!!!) what is the best (and easiest) way to parse a zero terminated string to extract a required shorter string / information out of it if I can define some rules such as it is after a known string upto a known character and/or it is inbetween two CR's (as an example)?
The method below is one slightly altered from another post reply, which originally used CASE and STRCOMP functions but I just could not get it to work so I've stripped it to the state that does work (ie. it just displays and stores the whole incoming text message including the header).

With thanks,
Jon
{example of incoming messages
 
+CMT: "789000",,"09/10/19,19:55:56+04"
Your Virgin Mobile airtime balance is 4.00 at 07:55pm on 19/10/09.
 
+CMT: "+447832000000",,"09/10/19,19:53:23+04"
STATUS CHECK

}
con
  _clkmode      = xtal1 + pll16x
  _xinfreq      = 5_000_000
  Serial_Baud_1   = 9600
  Serial_Mode_1   = 0
  SerialTX_1      = 26              { Serial Channel allocation }
  SerialRX_1      = 27              { Serial Channel allocation }
 
var
  byte inbuf[noparse][[/noparse]100]
  byte sms_number
  byte sms_message
OBJ
  Serial    :       "FullDuplexSerial"
 
pub main
  'initialization, etc.
  Serial.start(SerialRX_1,SerialTX_1,Serial_Mode_1,Serial_Baud_1)  ' Rx,Tx, Mode, Baud                                                                                                     
  repeat
    getstring(string(13),@inbuf)
    Serial.str(string("this is what is in the buffer: "))
    {I would like to extract the incoming sms number between the first "" after +CMT:
    and the message which is between two carriage returns,
    the message will not always be the same length!} 
    Serial.str(@inbuf)
    Serial.tx(13)
    
          
pub getstring(endstr, buffer) | rxchar, ec, c
  c := 0
  repeat
    rxchar := serial.rx
    ec := endstr
    'search term list for end char
    repeat while byte[noparse][[/noparse]ec] <> 0
      if byte[noparse][[/noparse]ec] == rxchar
        byte[noparse][[/noparse]buffer] := 0  'null terminate return str
        return byte [noparse][[/noparse]ec]   'return terminator
      else
        ec++
    if rxchar <> 10
      byte[noparse][[/noparse]buffer] := rxchar
    c++
    if c < 100
      buffer++

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2009-10-20 22:28
    I'd simply iterate through the string first searching for ".
    If a odd number of " has been found, the current index+1 is stored.
    If a even number of " has been found, the " is replaced by $00 (stringend).
    With this replacement the stored index points to a string which is·the phonenumber.
    Now you search for <CR> in the rest of the string.
    If a odd number <CR> has been found, store the current index+1.
    If a even number of <CR> has been found, replace with $00.
    With this replacement the stored index points to a string containing the message.

    Now you have the adress of phonenumber and message which are valid strings. As these strings are inside of your message-buffer you need to be done with processing the strings or you should copy them before reading the next message.
  • jon123456789jon123456789 Posts: 11
    edited 2009-10-21 07:17
    Thanks,
    that sounds like a robust approach, I'm just unsure exactly how to do it!

    If I was using something like Excel, I'd extract the string using LEN, FIND and MID commands.
    i.e. i'd search for the string "+CMT: " and use it to signify the start of a sensible message.
    I'd then search for the next " as the end of the number.
    I'd then MID the string between these two locations and call that the number.
    I'd then search for the next CR after the "+CMT: " and get all bytes upto the next CR.
    My issue is the best way to do this in SPIN. Am I on the right track to actually get the incoming data?
    I'm guessing not as I am ending on a CR where as I have two CR's in my incoming message.
    With thanks
    Jon
Sign In or Register to comment.