NewSoftSerial - I tried on my own but I suck

I am trying to receive SMS messages from my cell module. I can get most of the returned text but not all of it. I even increased the size _NewSS_MAX_RX_BUFF to 512 and I was able to get even more but cuts off at 256. I am not clear on how to use the flush or overflow methods.

Can someone point in the right direction on how I can grab all this data?

This is the receive function right now... not the best but it helps me see what I am getting:
void ATGetResp(int flag) {

char c;
memset(szATBuff, BUFFSIZE, '\0');
nBufPos = 0; // start at begninning
int nCnt = 0;

while (nCnt < (BUFFSIZE -1)) {

c = cell.read();
szATBuff[nBufPos] = c;
nBufPos++;
nCnt++;
}

}

here is a typical response:

+CMGL: 1,0,"REC READ","907","10/09/19,13:55:04+00"

Welcome! Your FlexAccount balance is 5 dollars. To pay, refill or check balances, go to my.t-mobile.com or visit t-zones. Your mobile number is 7709065801

+CMGL: 2,0,"REC READ","+17703173606","10/09/19,

TEST: CMGL

+CMGL: 1,0,"REC READ","907","10/09/19,13:55:04+00"

Welcome! Your FlexAccount balance is 5 dollars. To pay, refill or check balances, go to my.t-mobile.com or visit t-zones. Your mobile number is 7709065801

+CMGL: 2,0,"REC READ","+17703173606","10/09/19,

TEST: CMGL

you see that I can only read up to /19 on the second text, how to I make it so NSS will clear the buffer and read the rest of the response from the cell module.

I guess what I am trying to figure out is how to tell the NSS to clear the buffer and read the next bit of data? :cry:

Post your entire Sketch. Please use code tags.

Very simple right now... And the cell module receives the SMS msgs just cant get all data from the module. Seems to hit 256 and then I dont know how to tell the NSS object to get more data so I can see the rest of the message and the next txt. There are like 4 SMS message sitting there but I can only consistently get the first one and part of the second. At one point I am not sure how I did it but was actually only getting the text from the second text... Which meant some how I told the NSS object to dump the first bucket full and read the next data but I couldn't see the first txt data at that point. I guess I was trashing it and continuing.

Thanks for you help.

//##### Include our libraries #####
#include <PString.h>
#include <NewSoftSerial.h>



//##### Constants #####
#define BUFFSIZE 512




//##### Global Vars #####
char szATBuff[BUFFSIZE];
char szInPut = 0;
NewSoftSerial cell(2,3); 
int CELL_REG = 0;
int CELL_AT = 0;
int CELL_SMSMODE = 0;
int nBufPos;
int msg = 0;










void setup() {
  
  Serial.begin(9600);
  cell.begin(9600);
  
  
  
   
 Serial.println("SM5100B Init!");
  
  delay(1500);
 
 
}


  
  


void loop() {
  
        ATGetResp(0);
        ParseATResp();
        //cell.flush();
        
        ATGetSMS();
        
        
        
            
    
    
}
  


//************************************************************
void ATGetResp(int flag) {
  
//***TESTING*** Just trying to get all the data right now but cant
//*** Have different logic but to no avail
    char c;
    memset(szATBuff, BUFFSIZE, '\0');
    nBufPos = 0; // start at begninning
    int nCnt = 0;
    
    
    while (nCnt < (BUFFSIZE -1)) {
      
      c = cell.read();
      szATBuff[nBufPos] = c;
      nBufPos++;
      nCnt++;
    }
        
       
}

  
//************************************************************
// This will be used to recv data via SMS
//************************************************************
void ATGetSMS(void) {
        
  
        if (CELL_REG == 1 && CELL_AT == 1) {
          
            if (!CELL_SMSMODE) {
              cell.println("AT+CMGF=1");
              CELL_SMSMODE = 1;
              Serial.println("CMFG SET to TEXT");
              ATGetResp(1);
              ParseATResp();
              cell.overflow();
            }
            
            
            cell.println("AT+CMGL=\"ALL\"");
            //cell.println("AT+CMGR=1");
            Serial.println("TEST: CMGL");
            ATGetResp(1);
            Serial.println(szATBuff);
                     
            
            ATGetResp(1);
            Serial.println(szATBuff);
           
  
            ATGetResp(1);
            Serial.println(szATBuff);
            //cell.flush();          
            
            ATGetResp(1);
            Serial.println(szATBuff);
            //cell.flush();                 
            
            
            
           
            
        }   

}


//************************************************************
void ParseATResp(void) {

        
        if (strstr(szATBuff, "+SIND: 11") != 0) {
            CELL_REG = 1;
            LCDWrite("Reg. to Cell Net!!");
            return;
        } 
        
        
        if (strstr(szATBuff, "+SIND: 4") != 0) {
            CELL_AT = 1;
            LCDWrite("Cell AT Ready");
            return;
        }
        
}

Which board are you using?

duemilanove

With which processor?

void ATGetResp(int flag) {
[snip]
    while (nCnt < (BUFFSIZE -1)) {
      
      [glow]// You first need to ensure that a character has arrived.  Something like this...[/glow]
      [glow]if ( cell.available() > 0 ) {[/glow]
      c = cell.read();  // This returns -1 (0xFF) if there is not a character available
      szATBuff[nBufPos] = c;
      nBufPos++;
      nCnt++;
      [glow]}[/glow]
    }
    [glow]// At this point, a terminator needs to be placed in the buffer[/glow]
    [glow]szATBuff[nBufPos] = 0;[/glow]
}

  
[snip]

void ATGetSMS(void) {
        
  
        if (CELL_REG == 1 && CELL_AT == 1) {
          
            if (!CELL_SMSMODE) {
[snip]
              [glow]cell.overflow();  // This clears the overflow flag.  Used this way it serves no useful purpose.  Remove the line.[/glow]
            }

ATMEGA328P-PU

I did have those lines of code in my program initially but I was getting the same results. The only difference is when I print the buffer to the screen I see all the nulls where there we no characters rx'ed.

I will try this again when I get home.
Is there a proper way to handle a condition where the NSS rx buffer is full to tell it you have processed whats there and now get the rest?

Thanks again for taking the time to help me.

Maybe I need to start over... Are there any good tutorials for parsing the SMS messages from a cell module.?

I dont know why I am having such a hard time with this.

Generally, the more details you provide, the better advice you'll receive. It may be time to take a step back...

I am trying to receive SMS messages from my cell module

Why? What do you plan to do with this data?

What is the longest SMS message you expect to receive?

What is the largest SMS message your provider will send?

Sorry for the delay man!!! Day job had me slammed.

I want to use the SMS as a sort of out-of-band management facility.
I will be sending a predefined set of possible commands to the device. We'll say that it will always be 128 bytes. SMS can handle 160. I want to be able to issue the read command to the cell module then read each SMS. The response could be some number of SMS msgs. The best I can tell each SMS msg comes with a header that is terminated with a \r\n then the data comes next.
So ideally I would read Header then Data. I was trying to make read routine generic enough that I could use it for reading all responses. It does appear the module uses \r\n to end/separate reposes.

My problem seems to be generally that I am re-reading the same data or that I not telling the NSS object to get the next data. I havnt looked to deeply in the NSS but it would seem like there is a circular buffer. I am new to MC programming and especially serial so maybe my logic for reading serial is messed up... like I need to wait longer for a complete response to be returned..

The best I can tell each SMS msg comes with a header that is terminated with a \r\n then the data comes next.

Is the data also terminated? With \r\n?

So ideally I would read Header then Data. I was trying to make read routine generic enough that I could use it for reading all responses.

Sounds reasonable.

It does appear the module uses \r\n to end/separate reposes.

Are you saying the SMS stream is like this...

Header #1\r\n
Text #1\r\n
\r\n
Header #2\r\n
Text #2\r\n
\r\n
Header #3\r\n
Text #3\r\n
\r\n
...

My problem seems to be generally that I am re-reading the same data

I believe SMS is considered an unreliable protocol. It is possible you are receiving the same message twice. It is also possible that you need to issue a command to delete each message after you've received it.

I havnt looked to deeply in the NSS but it would seem like there is a circular buffer

There is.

I am new to MC programming and especially serial so maybe my logic for reading serial is messed up... like I need to wait longer for a complete response to be returned..

That is one strategy but one that isn't very reliable.

Hey Coding Badly,

I am real sorry for the delay... We have some stuff hit the fan at work and I am the only one dealing with it...

The problem I am/was having is that the NSS buffer would fill up and I couldnt find a reliable way to handle it.

This is what I was seeing:

Cell AT Ready

CMFG SET to TEXT

1

HEADER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

HEADER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

DATA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

OK

DATA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

0

(first record)
HEADER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

+CMGL: 1,0,"REC READ","+17703173606","10/09/24,20:02:00+00"

HEADER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

DATA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

gggggcccvvbbb

DATA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

0

HEADER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

+CMGL: 2,0,"REC READ","+17703173606","10/09/24,20:04:00+00"

HEADER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

DATA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

gvvvbjoihiihij

DATA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

1

HEADER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

+CMGL: 3,0,"REC READ","+17703173606","10/09/26,18:00:05+00"

HEADER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

DATA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

aaaaaaaaa

DATA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

0

(record 4-- bad)
HEADER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

+CMGL: 4,0,"REC READ","++CMGL: 1,0,"REC READ","+17703173606","10/09/24,20:02:00+00"

HEADER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

DATA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

gggggcccvvbbb

DATA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

in the ATGetSMS I call the ATGetResp multiple times before looping back round and re-issuing the command to get SMS msgs. As you can I get the header then data in order and all looks good for the first 3 SMS records. Then on the 4th record I have a problem... Record all the sudden overwrites the tail of record 4 and the data portion is that of record 1....

To get the results I needed I changed my code to delete the first msg after I reading basically popping it of the stack of messages. This allowed to loop through and read each message until there were none left. However this not what I wanted. I would like to issue the AT command to get all messages then parse them... but I cannot figure out how to get around the overflow issue.

The format of the data is :
HEADER \r\n
DATA \r\n
HEADER\r\n
DATA \r\n
so on...
Here is the function I am using now to read the data:

void ATGetResp(char * ATBuff) {
    
    char c;
    memset(ATBuff, '\0', BUFFSIZE);
    
    nBufPos = 0; // start at begninning
    int nBytes = 0;
    
    nBytes = cell.available();
    
    if (nBytes > 0) {
      
      for (int iii = 1; iii <= nBytes; iii++) {
        
        if (nBufPos == BUFFSIZE - 3)
          break;
          
        c = cell.read();
               
        if (c == '\r') {
            ATBuff[nBufPos] = c;
            
            c = cell.read();
        
            if (c == '\n') {
 
              ATBuff[nBufPos] = '\0';
             
              return;
            }
        }
        
        
         
        ATBuff[nBufPos] = c;
        nBufPos++;
      }
     
    }
        
      ATBuff[nBufPos] = '\0';
      return;
}

I will try to respond in a more timely fashion