Combining the code already supplied gives you this:
int foo; // Useless declaration to molify the IDE
#define isdigit(X) (((X) >= '0') && ((X) <= '9'))
void setup()
{
Serial.begin(9600);
}
void loop()
{
int AGL=read_int();
// Do math
}
int read_int()
{
static byte c;
static int i;
i = 0;
while (1)
{
while (!Serial.available())
{;}
c = Serial.read();
if (c == '\r')
{
return i;
}
if (isdigit(c))
{
i = i * 10 + c - '0';
}
else
{
Serial.print("\r\nERROR: \"");
Serial.print(c);
Serial.println("\" is not a digit\r\n");
return -1;
}
}
}
Compiled, not tested