1

I'm building a vehicle tracker with Arduino shield with Waveshare SIM 808. I am communicating with the Arduino via AT commands, and came across the following problem: The method "leserial ()" returns me the result of the command missing characters

Response +CGNSINF: 1,1,20161101222934.000,-23.285230,-51.20

Expected response +CGNSINF: 1,1,20161101223215.000,-23.285222,-51.204778,623.000,0.02,142.3,1,,1.2,2.5,2.2,,8,7,,,38,,

Code:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //RX, TX

String inputString = ""; 
char inChar; 

const int powerkey =  5;
const int statuspin = 6;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);  
  pinMode(powerkey, OUTPUT);
  pinMode(statuspin, INPUT);

  mySerial.println("AT");
  delay(2000);
  leserial();

  //TURN ON GPS
  mySerial.println("AT+ CGNSPWR =1");
  delay(2000);
  leserial();
}

void loop() {
  //GET LOCATION
  mySerial.println("AT+CGNSINF");
  delay(4000);
  leserial();
  delay(5000);
}

void leserial(){
  inputString = "";
  while (mySerial.available()>0){
    inChar = mySerial.read();  
    inputString += inChar;    
  }
  Serial.println(inputString);
}
2
  • Rookie mistake with serial reading. This explains: hackingmajenkoblog.wordpress.com/2016/02/01/…
    – Majenko
    Commented Nov 1, 2016 at 23:20
  • I want to know which clown first advocated blindly sending commands and delaying for 2 seconds to wait for a response. I see it so often it must be in some tutorial somewhere. Is it on Instructables? Youtube? Both are as bad as each other for spreading misinformation...
    – Majenko
    Commented Nov 1, 2016 at 23:24

2 Answers 2

2

Basically you're sending a command that gives a response, then waiting 2 seconds for that response to arrive (which is completely the wrong thing to do to start with).

You have to ask yourself: where does that response go when it does arrive, while it's waiting for you to finally get around to reading it?

The answer is: into a small internal buffer that is considerably smaller than the response.

Thus most of the response just gets thrown away because there isn't room for it.

Instead you should most definitely NOT delay for 2 seconds waiting for the response, and instead keep looking to see if there are any characters to read up until you reach the end of the line.

It's all explained here: https://hackingmajenkoblog.wordpress.com/2016/02/01/reading-serial-on-the-arduino/

4
  • Thanks for the tips, really using the "delay ()" to simulate waiting for the command to be run completely is quite common in the tutorials on the internet. Everyone who saw makes use of this function! Your article helped me better understand the operation of the serial communication, but applied some concepts that you had not got success, the answer still comes incomplete. As you mentioned in answer, apparently the buffer does not contain the size of the command response. I increase the buffer size? Commented Nov 2, 2016 at 0:08
  • If you remove the delay and read the bytes as they arrive the size of the buffer is irrelevant.
    – Majenko
    Commented Nov 2, 2016 at 0:10
  • I removed the sleeps but did not work. Any idea what could be? Thank you for your support. Commented Nov 2, 2016 at 1:48
  • If you show your new code we might be able to see where you are going wrong...
    – Majenko
    Commented Nov 2, 2016 at 11:13
1

I managed to solve the problem, I edited the file "SoftwareSerial.h" and changed the size stated in constant _SS_MAX_RX_BUFF.

#define _SS_MAX_RX_BUFF 64 // RX buffer size

to:

#define _SS_MAX_RX_BUFF 128 // RX buffer size

I reached the conclusion that when I realized that the number of bytes returned in the function "mySerial.avaliable ()" was no more than 63 bytes. I appreciate the support of all.

1
  • I am not sure if it is a good idea to patch the Arduino library as a workaround for your poor code.
    – jarnbjo
    Commented Nov 3, 2016 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.