Menu

Parsing SIM800L Module SMS Message to extract sender phone number and message

Awais
2023-12-22
2023-12-25
  • Awais

    Awais - 2023-12-22

    Hi,
    I am integrating SIM800L module with PIC16F1827 using serial port. I want to extract sender phone number and message from the received SMS using non blocking code but getting no idea how to do it. After receiving SMS of appropriate command, PIC will send back the data to the sender, e.g. a count value.

    The serial data format received from SIM800L module is as follows.

    +CMT: "+123456789012","","23/10/15,14:51:31+20"
    MSEG

    (HEX format with printable characters)
    0D 0A +2B C43 M4D T54 :3A 20 "22 +2B 131 232 333 434 535 636 737 838 939 030
    131 232 "22 ,2C "22 "22 ,2C "22 232 333 /2F 131 030 /2F 131 535 ,2C 131 434 :3A
    535 434 :3A 232 737 +2B 232 030 "22 0D 0A M4D S53 E45 G47 0D 0A

    +123456789012 is the phone number and MSEG is the message.

    Thanks and regards,
    Awais

     
  • Anobium

    Anobium - 2023-12-22

    Write a function to find the : in the string, and then find ",". Then, return the string.

    No tested, but, try this approach.

    function cutstring ( in sourcestring as string * 128 ) as string * 16
        dim _lpos, _rpos as Byte
        _lpos = instr( sourcestring, ":" )
        _rpos = instr( sourcestring, "," )
        cutstring = mid ( sourcestring, _lpos + 2, _rpos -2
    end function
    
     
  • Anobium

    Anobium - 2023-12-22

    I should have tried... the idea... it will take a while to debug....

    I had to change the \" in the string to ' for this little test. Should work ok with a real string.

    dim ss, rr as String
    ss = "+CMT: '+123456789012','','23/10/15,14:51:31+20'"
    
    rr = cutstring( ss )
    // rr as the result = +123456789012
    
    end
    
    function cutstring ( in sourcestring as string ) as string * 16
        dim _lpos, _rpos as Byte
        _lpos = instr( sourcestring, ":" )
        _rpos = instr( sourcestring, "," )
        // add 3 to move from _rpos an take 4 off the string length
        cutstring = mid ( sourcestring, _lpos + 3, _rpos - _lpos - 4 )
    end function
    
     
  • Awais

    Awais - 2023-12-23

    Thanks, Evan. I will test and reply.

    Just to mention that "MSEG" is the part of replied string from SIM800L and there are three CR and LF in the string. "MSEG" part is between second and third CR and LF.

    I am planning to use your ring buffer code to receive serial data from the module and save it in a string array, count received number of bytes which will be 57 then process the string array.

    I did not understand this part "as string * 16".

    Regards,
    Awais

     

    Last edit: Awais 2023-12-23
  • Anobium

    Anobium - 2023-12-25

    Dim


    Syntax:

    *_For String:_*
    Dim _string [* _size_] [At _location_]
    

    Explanation:

    A String declared with a fixed size (numeric constant that can be evaluated at compile time) is a fixed length string. It has no descriptor and it is not resized to fit its contents. If data overflows the size of the string, the memory may be overwritten.
    Fixed length strings are not NULL terminated, and they use size + 1 bytes of space.
    String variable names need not end in a dollar sign $ as in other dialects of BASIC.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.