Serial problems ... GPS and SIM908

Hello.

I’m experiencing a strange problem with the serial interface of my arduino UNO R2.

I’m using a SIM908 and this code, is just for me to know the GPS position.
I manually (before start my arduino) type this two commands to the SIM908 :

  • at+cgpsrst=1
  • at+cgpsinf=0 (just to see the GPS working)

Now i have the SIM908 sending the GPS string to my UNO XD

I’m using the hardware Tx/Rx to communicate with the SIM908 and using the softserial to debug.
Every time that i need to program the UNO, i must disconnect the serial communication of the SIM908,
because i’m using the hardware Tx/Rx…

Now i start my UNO with this code :

/*
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

String inputString;               // a string to hold incoming data
boolean stringComplete = false;   // whether the string is complete

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  mySerial.begin(9600);
  
  mySerial.println("OSTIUM");

  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
  
}

void loop() // run over and over
{
  // Asking the GPS for positioning
  Serial.print("at+cgpsinf=0\r");

    while (Serial.available()) {
      // get the new byte:
    char inChar = (char)Serial.read(); 
      //mySerial.print(inChar);
      if (inChar == '\n'){
        stringComplete=true;
        mySerial.print(" inputString = ");
        mySerial.println(inputString);
        // clear the string:
        inputString = "";
        stringComplete = false;
      }
      
      // add it to the inputString:
      if(stringComplete == false) {
        inputString += inChar;
      }
    }

  Serial.flush();
  delay(4000);

}

// This commented, because of the strange behavior ... so i put all this in the loop !!??
/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
/*void serialEvent() {
    while (Serial.available()) {
      // get the new byte:
      char inChar = (char)Serial.read(); 
      //mySerial.print(inChar);
      if (inChar == '\n' || inChar == '\r'){
        stringComplete=true;
        mySerial.println(inputString);
      }
      
      // add it to the inputString:
      if(stringComplete == false) {
        inputString += inChar;
      }
    }
}
*/

This is what i get in my serial port (minicom) :

OSTIUM
inputString = at+cgpsinf=0
inputString =
0,-839.465252,4111.720923,174.961227,20130923213at+cg
inputString =
0,52,43,1at+cgpsinf=0
inputString =
0,-839.465252,4111.720923,174.961227,20130923213at+
inputString =
5252923,7at+cgpsinf=0
inputString =
0,-839.465252,4111.720923,174.961227,20130923213at+cg
inputString =
4652209at+cgpsinf=0

When i do the comand ‘at+cgpsinf=0’, using the serial interface (minicom) of the SIM908 i get this :

0,-839.472718,4111.710553,172.852158,20130923214109.000,41,12,0.000000,208.770493

So, i don’t understand why i get the :

  • inputString = at+cgpsinf=0
  • 0,52,43,1at+cgpsinf=0

it should be a ‘clean’ string equals to the one that i receive using the minicom.

I’m quite sure that i’m doing something wrong … perhaps a problem with a basic solution …

Can anyone help me with this ? Can anyone explain me why is this happening ?

Thank you for your attention.
Joao Jacob

Every time that i need to program the UNO, i must disconnect the serial communication of the SIM908, because i'm using the hardware Tx/Rx...

It would be far easier to use SoftwareSerial to talk to the SIM908, and HardwareSerial (the Serial instance) to talk to the PC.

Is the SIM908 actually communicating at 115200? If so, SoftwareSerial is not a good choice to talk to it, and you'll need to continue to unplug it to change code.

  Serial.flush();

Block until all outgoing serial data has been sent. It that REALLY necessary? The Serial.print() function will block on it's own as needed.

I'm quite sure that i'm doing something wrong ... perhaps a problem with a basic solution ...

Sure. You are assuming that all the serial data available to read at one time constitutes a single packet - no more and no less. That is not the case.

Ditch the delay(4000). Use millis(), as explained in the blink without delay example, to send the message to the device once every 4 seconds, instead.

    while (Serial.available()) {
      // get the new byte:

As PaulS said, you probably don't have everything available. Read this:

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

Thank you PaulS and Nick Gammon.

PaulS :

  • The SIM908 GPS serial port, is working 11520 (default) ... i can change this and use softserial :)
  • Serial.flush() ... just trying to 'clean' all data in the serial port. But i'll delete that line !!
  • I really thing that i have all data available as soon as i send the 'at+cgpsinf=0' command. I don't have it ? If not, how can i solve this ? Using the link that Nick Gammon sugested ?
  • I'll change to millis() ...

Nick Gammon : I assumed that i had all data available !!?? If not, as you both are saying, shall i use the code in the link that you've suggested to solve my problem ?

Thank you very much for your attention. Joao Jacob

I really thing that i have all data available as soon as i send the 'at+cgpsinf=0' command.

On the other hand, I really KNOW that you don't. Serial data transmission is far slower than the Arduino can read it.

If not, as you both are saying, shall i use the code in the link that you've suggested to solve my problem ?

You need to figure out exactly what constitutes an end-of-packet marker, and read and store data until that marker arrives. Only then can you use the data in the packet.

ostium: I really thing that i have all data available as soon as i send the 'at+cgpsinf=0' command.

Not at all. When you order a pizza is it immediately available? I think not.

Hello.

I believe in you PaulS and in Nick Gammon.

I know that Arduino is much faster than serial communication … i know, but didn’t think about this :roll_eyes:
The pizza explanation is funny and easy to understand.

I’ve search on SIM908 AT command specifications, and found this :

Commands are usually followed by a response that includes. “”

So my ‘end-of-packet marker’ is really the ‘’ (ascii code 10).
I’ll try to search for this end of packet … Then i’ll ‘work’ the packet !!!

I’ll feedback as soon as i test this …

Thank you for your help.
Joao Jacob

Hi.

I've tested the software in the link that Nick Gammon advice me, and worked perfect !!! I had no doubts about this :D

Now i can see the GPS string exactly as it is. I can also make complex math as calculating the distance between two points, with no loss of serial characters !!

From now on, i will not be 'afraid' about arduino processing capacity related to the serial baud rate :)

Thank you very much for your help. Joao Jacob