General

How do I put into a buffer, a part of Serial response (UART)

userHead pierrot10 2014-09-11 02:49:43 1984 Views1 Replies
May be let show it simplier.

Here is my new code.
It should
1. start the module, read the response of AT
2. Go to GSM mode
3. Power and rest GPS, read the response of AT+GPSPWR....
Code: Select all
#include <SoftwareSerial.h>

int baud_rate = 9600;
int pin_gsm = 3;
int pin_gps = 4;
int pin_power = 5;
//int pin_dtr = 6;
boolean debug = true;
boolean raedy_to_go = false;

// Reading String
#define BUFFERSIZE 200
char buffer[BUFFERSIZE];
char inChar;
int index;

void setup()
{
  Serial.begin(baud_rate);
  delay(5000);                         // Wait for 5sec after begin
  
  if(debug)
  {
    Serial.println(F("\n****************************"));
    Serial.println(F("STARTING SYSTEM Read AT stream"));
    Serial.println(F("******************************"));
  }
  pinMode(pin_gsm,OUTPUT);            // Set the pins
  pinMode(pin_gps,OUTPUT);
  pinMode(pin_power,OUTPUT);
 
  digitalWrite(pin_power,HIGH);
  delay(1500);
  digitalWrite(pin_power,LOW);
  // The module is started but I want to be sure it has really started
  // I send an AT command and I should receive an response RDY
  // That function should 
  // 1. Send the AT commans
  // 2. Store the response in a buffer

  if(strstr(read_AT_string("AT",5000),"RDY") != NULL)
  {
    Serial.println(F("\n> Module is started UP"));
    Serial.println(F("------- Show buffer------"));
    Serial.println(buffer);
    Serial.println(F("-------------------------\n"));
    
    // Enabling GSM
    gsm_enable();
    
    Serial.println(F("\n> Powering GPS"));
    Serial.println(F("------- Show buffer------"));
    Serial.println(read_AT_string("AT+CGPSPWR=1",5000));
    Serial.println(F("-------------------------\n"));
    // AT+CGPSPWR, should it return OK
    
    Serial.println(F("\n> reset GPS in autonomy mode"));
    Serial.println(F("------- Show buffer------"));
    Serial.println(read_AT_string("AT+CGPSRST=1",5000));
    Serial.println(F("-------------------------\n"));
    // AT+CGPSRST=1, should it return OK
    
    
  }
  else{
    Serial.println(F("> Module is NOT started UP"));
    Serial.println(F("------- Show buffer------"));
    Serial.println(buffer);
    Serial.println(F("-------------------------\n"));
  }
}

void loop()
{

    
}

char* read_AT_string(char* command, int timeout)
{
  unsigned long previous;
  previous = millis();
  index=0;
  // Clear the buffer
  buffer[0]='\0';
  
  // SEND THE AT COMMAND. I AM ASKING IF I HAVE TO KEEP THE ln
  Serial.println(command);
  do
  {
    if(Serial.available() > 0) // Don't read unless
    {
      if(index < BUFFERSIZE-1)
            {
              //Serial.println("b");
              inChar = Serial.read();  // Read a character
              buffer[index] = inChar;  // Store it
              index++;                 // Increment where to write next
			    // BUFFER CONTAINING THE DESIRED RESPONSE
              buffer[index] = '\0';    // Null terminate the string
            }
    }
  }while(((millis() - previous) < timeout));
  
  return buffer;
}

void gps_enable(void)
{
  if(debug)
  {
    Serial.println(F("\n> Enabling GPS"));
  }
  digitalWrite(pin_gps,LOW);                //Enable GPS mode
  digitalWrite(pin_gsm,HIGH);                //Disable GSM mode
  delay(2000);
}

void gsm_enable(void)
{
  if(debug)
  {
    Serial.println(F("\n> Enabling GSM"));
  }
  digitalWrite(pin_gsm,LOW);                //Enable GSM mode
  digitalWrite(pin_gps,HIGH);               //Disable GPS mode
  delay(2000);
}
The module is UP but the display is a mess:
[quote]****************************
STARTING SYSTEM Read AT stream
******************************
AT

> Module is started UP
------- Show buffer------
ÿÿÿþÿÿÿ
RDY

+CFUN: 1

+CPIN: READY
AT

OK

Call Ready

GPS Ready

-------------------------


> Enabling GSM

> Powering GPS
------- Show buffer------
AT+CGPSPWR=1

> Module is started UP
------- Show buffer------
ÿÿÿþÿÿÿ
RD
> Powering GPS
------- Show buffer------
AT+CGPSPWR=1

OK

-------------------------


> reset GPS in autonomy mode
------- Show buffer------
AT+CGPSRST=1

> Module is started UP
------- Show buffer------
ÿÿÿþÿÿÿ
RD
> Powering GPS
------- Show buffer------
AT+CGPSPWR=1

OK

-------------------------


> reset GPS in autonomy mode
------- Sho
-------------------------

[/quote]

And this is my problem.

the read_AT_string is supposed to send the AT command and store in a buffer, the response ONLY of the sent AT command.
Apparently, it store anything and keep all Serial communication even if I clean the buffer
Code: Select all
buffer[0]='\0';
Now, I red this
[quote]
The "AT" or "at" prefix must be set at the beginning of each Command line. To terminate a
Command line enter <CR>.
Commands are usually followed by a response
that includes. "<CR><LF><response><CR><LF>"
Throughout this document, only the responses are presented, <CR><LF> are omitted
intentionally.
[/quote]

How can I adapt my read_AT_string function to make sure, it will store, into the buffer, ONLY the response, wich are between < CR>< LF>?????????

THIS IS MY BIG problem, I want to buffer the response. Pls help, me I spent all day on it :o||.

I think the Serial should be cleaned before sending a new AT command, or to filder content between <CR><LF> and I am not able to do that.

Otherwise, does my simpe code sound good?

Many thnyk!!!
2014-09-11 13:31:05 Is that any possible that "response" is the response time? Or you think it is a content?
After Serial port sends the Command, it will clean the buffer every time.
userHeadPic Grey.CC