I've got a Mega and a GSM Playground (GE863 Quad) from HWKitchen. I've bent out the GSM shield serial pins and instead moved them to Serial1 such that it doesn't interfere with uploading to the Mega.
The problem I am having is that the serial data is unreliable. If I connect directly to the GSM module and connect to the serial port, the returned data from commands such as 'AT' are fine, all rx perfectly.
When using the Mega, the GSM module connected to Serial1, the characters seem to get corrupted because the module is unpredictable and when I read them from Serial1 and print them to Serial0 I can see the responses are corrupt. I've also increased the hardware serial buffer to 256 bytes, no real reason, but I'm lost for ideas.
What is the reason for this? Thanks for any advice you can give.
// str - the string to send to the serial port
// charDelay - the delay between sending each character
// rxDelay - the delay after sending the command, allows for data to be received
void sendCommand(char* str, int charDelay, int rxDelay){
Serial.print("Sending:");
Serial.println(str);
for (int i = 0; str[i] != '\0'; i++){
Serial1.print(str[i]);
delay(charDelay);
}
delay(charDelay);
Serial1.print("\r\n");
delay(rxDelay);
}
void setup(){
delay(2000);
// initialization of serial line
Serial.begin(115200);
Serial.println("Serial0 online");
Serial1.begin(115200);
// turn on GSM module
digitalWrite(GSM_ON, HIGH);
delay(1200);
digitalWrite(GSM_ON, LOW);
delay(1200);
Serial1.flush();
Serial.flush();
// Set speed of the serial interface
sendCommand("AT+IPR=115200",10,30);
// set serial interface to auto detect connection settings
sendCommand("AT+ICF=0,0",10,30);
// disable echo
sendCommand("ATE0",10,300);
// send some AT's to let the module sync the connection settings
for (int i = 0; i < 5; i++){
sendCommand("AT",10,30);
}
// Reset both serial buffers
Serial1.flush();
Serial.flush();
delay(1000);
int cmdDelay = 300;
int charDelay = 5;
sendCommand("AT",charDelay,cmdDelay);
sendCommand("AT",charDelay,cmdDelay);
sendCommand("AT",charDelay,cmdDelay);
sendCommand("AT",charDelay,cmdDelay);
sendCommand("AT",charDelay,cmdDelay);
sendCommand("AT",charDelay,cmdDelay);
// Expect 6 x OK's to be RX'd , hardware serial buffer is 256bytes
while (Serial1.available()){
char c = (char)Serial1.read();
Serial.print(c);
if (c == '\0'){
Serial.println();
break;
}
}
}
void loop(){
}
Output from serial (this is quite a good run, OK was actually RX'd a few times!):
Serial0 online
Sending:AT+IPR=115200
Sending:AT+ICF=0,0
Sending:ATE0
Sending:AT
Sending:AT
Sending:AT
Sending:AT
Sending:AT
Sending:AT
Sending:AT
Sending:AT
Sending:AT
Sending:AT
Sending:AT
OK
K
OK
OK
OK