Pages: [1]   Go Down
Author Topic: Weird characters when parsing incoming SMS SM5100B-D  (Read 1041 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,

Recently got myself a Uno and a SM5100B-D GSM which i've got successfully calling and hanging up. Done the same with SMS but now i'm wanting to read incoming messages.

I've found a few bits and pieces around and came across this:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1294527225

Code:
/* Cellular shield code, by Phil Gordon http://mrphilipgordon.com
 This code assumes a pull down pushbutton switch attached to pin 9.

 Code expects text in the format x,number1,number2 with number1 being either 1 or 2.

 It will store number2 in a variable depending on number 1.

 Values of variables will be printed on press of pushbutton attached to pin 9

 */




#include <NewSoftSerial.h>         //Include the NewSoftSerial library to send serial commands to the cellular module.
NewSoftSerial cell(2,3);           //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.

char c = 0;                        //Will hold the incoming character from the Serial Port.
int ATReady = 0;                   //Cellular shield ready
char module1[128];                 //Value for module1
char module2[128];                 //Vue value for module2
int index;                         //Array index
long lastPress = 0;                //For debounce pushbutton
long debounceDelay = 500;          //Delay between represses             


void setup()
{
  Serial.begin(9600);              //Initialize serial ports for communication.
  cell.begin(9600);
  Serial.println("Starting SM5100B Communication...");
}

void loop() {
  if(cell.available() >0)           //If a character comes in from the cellular module...
  {
    c=cell.read();                  //Get the character from the cellular serial port.
    Serial.print(c);                //Print that character to serial monitor
    if(c == '+')                   
    {
      c=cell.read();
      Serial.print(c);
      switch(c)
      {
      case 'S':
        c=cell.read();
        Serial.print(c);
        delay(10);
        c=cell.read();
        Serial.print(c);
        delay(10);
        c=cell.read();
        Serial.print(c);
        delay(10);
        c=cell.read();
        Serial.print(c);
        delay(10);
        c=cell.read();
        Serial.print(c);
        delay(10);
        c=cell.read();
        Serial.print(c);
        switch(c)
        {
        case '3':                                  //Check for initiallisation statud, waiting for command
          Serial.print("\n \rAlmost there...");    //  +SIND: 4
          break;
        case '4':
          Serial.print("\n \rNetwork established");
          ATReady=1;                 
          cell.println("AT+CMGF=1");               //Set message mode to text
          cell.println("AT+CNMI=3,3,0,0");         //Display messages when received
          break;
        case '7':
          Serial.print("\n \rSomething b0rked");
          ATReady=0;
          break;
        }
        break;
      case 'C':
        c=cell.read();
        Serial.print(c);
        delay(10);
        if(c == 'M')
        {
          c=cell.read();
          Serial.print(c);
          delay(10);
          if(c == 'T')
          {
            while(c != 'x')            //Wait untill the character 'x' is displayed
            {
              c=cell.read();
              Serial.print(c);
            }
            c=cell.read();
            Serial.print(c);
            delay(10);
            c=cell.read();
            Serial.print(c);
            switch(c)
            {
            case '1':                  //Run this code if number1 is a 1
              index=0;                 //Reset array index
              c=cell.read();
              Serial.print(c);
              delay(10);
              c=cell.read();
              Serial.print(c);
              while(c != '\r')         //Run loop till end of the line
              {
                module1[index] = c;    //Add character to the array
                c=cell.read();
                Serial.print(c);
                index++;               //Advance array
              }
              module1[index]='\0';     //Terminate array
              break;
            case '2':
              index=0;
              c=cell.read();
              Serial.print(c);
              delay(10);
              c=cell.read();
              Serial.print(c);
              while(c != '\r')
              {
                module2[index] = c;
                c=cell.read();
                Serial.print(c);
                index++;
              }
              module2[index]='\0';             
              break;       
            }

            break;
          }
        }
      }
    }
  }
  if(Serial.available() >0)              //If a character is coming from the terminal to the Arduino...
  {
    c=Serial.read();                     //Get the character coming from the terminal
    cell.print(c);                       //Send the character to the cellular module.
  }

  if (digitalRead(9) == HIGH && (millis() - lastPress) > debounceDelay)   //stop button repeatedly sending commands.
  {
    lastPress = millis();
    Serial.println();
    Serial.print("Module1: ");
    Serial.println(module1);
    Serial.print("Module2: ");
    Serial.println(module2);
  }

}

It works fine, until a message comes in and in the serial window i see

Code:
+CMT: "+642********","+642********","12/05/01,21:00:56+00",4
Test
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

Then the ÿ characters keep spamming and eventually freezes up.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 650
Posts: 50838
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if(cell.available() >0)           //If a character comes in from the cellular module...
  {
    c=cell.read();                  //Get the character from the cellular serial port.
    Serial.print(c);                //Print that character to serial monitor
    if(c == '+')                   
    {
      c=cell.read();
      Serial.print(c);
      switch(c)
      {
      case 'S':
        c=cell.read();
        Serial.print(c);
        delay(10);
        c=cell.read();
        Serial.print(c);
        delay(10);
        c=cell.read();
        Serial.print(c);
        delay(10);
        c=cell.read();
So, if there is at least one character to read, read the first one. If, by chance, it is a '+', read the next one that hasn't arrived yet.

How is that working for you? Never mind, I already know.

Quote
Then the ÿ characters keep spamming and eventually freezes up.
Well, duh. You are reading and printing data that has not arrived yet.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a better way i can handle an incoming message then?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 650
Posts: 50838
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there a better way i can handle an incoming message then?
Of course, there is. Figure out what constitutes the start of a packet (most likely the +). Figure out what constitutes the end of a packet (most likely a carriage return or line feed ; possibly two CRs in a row or CR,LF,CR,LF).

Discard data that arrives before the start of packet marker. Store all data that arrives after the start marker, until the end marker arrives. When the end marker arrives, then parse the stored data.

 
Code:
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 510
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a better way i can handle an incoming message then?

For example: http://www.gammon.com.au/serial
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks - I'm new to the Arduino scene so a bit in the deeper end but i have a good knowledge of programming so its more the actual interfacing and how its all handled.
Logged

Pages: [1]   Go Up
Jump to: