I’m using an Arduino Uno R3 and when I send an AT command to my GSM shield via Serial, I get an increasing number from Serial.available().
Here is the example I have been using to debug:
void loop()
{
Serial.println("AT+CADC?");
delay(3000);
}
void serialEvent()
{
char * sensorValue;
int serial = Serial.available();
Serial.print("-");
Serial.print(serial);
Serial.println("-");
if(serial >0)
{
sensorValue = (char*) malloc(sizeof(char) * (serial +1));
int i;
for(i = 0; i < serial; i++)
{
sensorValue[i] = Serial.read();
//Serial.print(sensorValue[i]);
}
sensorValue[serial+1] = '\0';
Serial.print(sensorValue);
}
delay(2000);
}
The result I get from the serial monitor is:
-30-
-63-
-63-
-63-
…
Why does the number of bytes available start off at 30 and then max out at 63? This happens even when I use Serial.read(), which should consume the data in the buffer.
You send whatever you receive right back to the modem. Which promptly echoes it back. So once you got it going with an AT command, you’ll forever loop sending the same bytes back and forth. Remove the Serial.print() calls.