Hi,
I've connected a GPS module to Arduino Uno via UART (named GPS_Serial).
Data is sent from the GPS module to Arduino, and from Arduino's serial port to PC.
Initial baud rate of the GPS module is 4800 bps, and this is the baud I set for the serial port (from Arduino to PC) and to the GPS_Serial.
So far so good.
The problem arise when I try to change the baud rate.
I monitor the messages sent to the GPS, and if I recognise a change baud rate message - I send this message to the GPS and switch the baud rate of Serial - between Arduino and PC.
Here I have a problem. After setting the port in my terminal to the new baud rate (for instance 9600bps) - I get gibberish. Actually I get gibberish in every possible baud rate.
When I connect directly to the Tx pin - the pin on which the GPS sends data to Arduino - I get proper data in the new baud rate (9600 bps). Meaning the GPS module received the command. But I get gibberish from the serial port.
What can cause this problems?
I stated from flush() function, the added a reset function, but still getting gibberish on serial port.
This is the code - in this code I monitor 2 baud rate changes - to 115,200 bps, and to 9600 bps.
For each baud rate the implementation if different, but the result is the same - gibberish on the serial port.
#include <SoftwareSerial.h>
SoftwareSerial GPS_Serial(11,12);
// pin 11 is the Rx of Ardoino. Should be connected to the Tx of GNSS module
// pin 12 is the Tx of Ardoino. Should be connected to the Rx of GNSS module
String str = "";
int Baud_Rate = 4800; // initial baud rate - default of the module
void setup()
{
Serial.begin(Baud_Rate);
GPS_Serial.begin(Baud_Rate);
Serial.println("connection established");
}
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void loop()
{
//if we have some incomming serial data then..
if (GPS_Serial.available())
{
Serial.write(GPS_Serial.read());
}
while (Serial.available())
{
char c = Serial.read();
str += c;
if (c =='\n')
{
if (str.indexOf($PSRF100,1,115200,8,1,0*05") >= 0)
{
GPS_Serial.flush();
GPS_Serial.print(str);
delay(1000);
GPS_Serial.begin(115200);
delay(1000);
Serial.flush();
Serial.begin(115200);
}
else if (str.indexOf("$PSRF100,1,9600,8,1,0*0D") >= 0 )
{
GPS_Serial.print(str);
GPS_Serial.flush();
delay(1000);
GPS_Serial.end();
Serial.flush();
delay(2000);
Baud_Rate = 9600;
resetFunc(); //call reset
setup();
}
else
{
GPS_Serial.print(str);
}
str="";
}
}
}
Thanks a lot,
Mark