Go Down

Topic: SM5100B trouble finding <CR> (Read 3654 times) previous topic - next topic


Hello everyone,

I'm having an issue parsing a response from the SM5100B and finding a carriage return.  Currently, I set the module to notify me of a new SMS (AT+CNMI=3,3,0,0).  According to the documentation:

http://www.sparkfun.com/datasheets/CellularShield/SM5100B%20AT%20Command%20Set.pdf (page 72 of 141, PDF)

there is a carriage return right before the text of the SMS.  When my ParseResp() function is run, it can't find a carriage return.

I know my ParseResp() code works because I tested it by looking for a different character.  For instance, I told it to look for a "&", and then a put a "&" at the beginning of my SMS to the module.  Obviously this is a quick fix for the problem, and I could just start all my SMSs with a "&", but I would rather fix the issue than skirt around it.

Here is all my code:
Code: [Select]
/*  There is an issue here with digitalRead and digitalWrite.
   When I send pin 8 HIGH, pin 7 reads as HIGH for some reason.

#include <NewSoftSerial.h>  
#include <string.h>        
#define BUFFSIZE 512

char ATBuff[BUFFSIZE];
String SMSBuff = "";
String Mess = "";
int CELL_REG = 0;
int CELL_AT = 0;
//int CELL_SMSMODE = 0;
int nBufPos;
int dataReady = 0;
int noCR = 0;
NewSoftSerial cell(2,3);

void setup()
 Serial.println("Starting Serial");
 pinMode(7,INPUT); //get rid of this
 while(CELL_REG == 0 || CELL_AT == 0){
 Serial.println("Found Network");
 /*   Sends SMS
 cell.print("'IT WORKS!!!!!'");

void loop() {
 //This loop exists to delete SMSs while the code is running
 if (digitalRead(7)==HIGH){
   Serial.println("SMSs deleted.");
 //This loop exists to look for incoming data from the sm5100b
 //   If it finds data, it parses it and decides what to do
 dataReady = cell.available();
 if (dataReady > 0){
   //Serial.println("data available");
   dataReady = 0;


// This function gets commands from the sm5100b
void ATGetResp(void) {
   char c;
   memset(ATBuff, '\0', BUFFSIZE);                // clear array
   nBufPos = 0;                                   // Reset array counter
   int nBytes = 0;                                
   nBytes = cell.available();                     // Store number of bytes in serial queue
   if (nBytes > 0) {
     for (int i = 1; i <= nBytes; i++) {          // Keep running loop till no more bytes in queue
       if (nBufPos == BUFFSIZE - 3) {             // Stop if array is full
         nBufPos = 0;
       c = cell.read();                           // Set c to current serial character
       if (c == '\r') {                           // If c is carriage return
           ATBuff[nBufPos] = c;                   //   add it to the array
           c = cell.read();                       // Doesnt work without this line, can anyone explain why?
           if (c == '\n') {                       // If c is newline
             ATBuff[nBufPos] = c;                 //   add to array
             ATBuff[nBufPos+1] = '\0';            //   and terminate
             return;                              //   then return to program
       ATBuff[nBufPos] = c;
   ATBuff[nBufPos] = '\0';

//This function parses the intial responses from the sm5100b
//   to make sure everything is operating correctly
void ATParse(void){
 if (strstr(ATBuff, "+SIND: 11") != 0) {
   CELL_REG = 1;
 if (strstr(ATBuff, "+SIND: 4") != 0) {
   CELL_AT = 1;

//This function sets the sm5100b to TEXT mode and waits
//  for error free response
void SetText(void){
 int isText = 0;
   if (strstr(ATBuff,"OK") !=0){
     isText = 1;
     Serial.println("Text is go");

//This function sets the sm5100b to notify when it
//   receives an SMS and waits for an error free response
void SetNotify(void){
 int isNotify = 0;
   if (strstr(ATBuff,"OK") !=0){
     isNotify = 1;
     Serial.println("Notify is go");

//This function parses the data that is received after
//   the sm5100b is set up correctly.  Right now it just
//   looks for SMS and attempts to parse the response to
//   pull out the actual message
//   NOTE to me:  issue with noCR, should be resolved
//   with <CR> issue
void ParseResp()
 if (strstr(ATBuff,"+CMT:") !=0 || noCR == 1){
   String SMSBuff = ATBuff;
   if (SMSBuff.indexOf('\r') != -1){   //found CR '\r'  tried 13...no dice
     for(int j = SMSBuff.indexOf('\r')+1; j<=SMSBuff.length(); j++){
       Mess += SMSBuff[j];
       noCR = 0;
   Serial.print("The message is: ");
   else{      //no <CR>
     noCR = 1;
     Serial.println("No <CR> found :(");

NOTE:  I took the ATGetResp() code from user Philherup.

My question for anyone out there is:  Why can't I find the carriage return?  What am I missing and/or doing wrong?



In the ATGetResp() function, I would recommend putting each { on a new line, and properly indenting the code.

Put the useful comments BEFORE the code. Get rid of the useless comments.
Code: [Select]
       c = cell.read();                           // Set c to current serial character
Well, duh?

When you have long comments at the end of the code, and more code than fits in the window, I have to scroll up and down and left and right to read it. Scrolling windows inside of windows is difficult. Make it easy by making the lines short, so the vertical scroll bars are not necessary.

Comments after code are almost always added to explain what a statement is doing, for the benefit of new users. The people that will be able to help you are NOT new users.

Read each character, and decide what to do with THAT character. Nested reads are very difficult to keep straight.
The art of getting good answers lies in asking good questions.

Go Up