0
votes

I'm using an Arduino UNO R3 and when I send an AT command to my GSM shield via Serial I get 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.

EDIT: Added full contens of serialEvent().

2
Your code snippet doesn't show any reads so the result is expected. It counts the bytes from the modem response.Hans Passant
Well like I mentioned towards the end the result is the same even with reads. I just edited the code to show my full code where I do reads.BOMEz

2 Answers

1
votes
sensorValue[serial+1] = '\0';
Serial.print(sensorValue);      // <== here

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.

1
votes

I'd suggest you use the software serial library -- [http://arduino.cc/en/Reference/SoftwareSerial]SoftwareSerial1 -- and use that to access the GSM modem, unless the GSM modem requires UART / RS-232 level signals.

Another alternative, which might be appropriate if most of your work involves talking to modems, would be an Arduino Mega 2560. It has four hardware serial interfaces, though it doesn't have RS-232 output signals. For that, you should be able to use one of the FTDI breakout boards.