Grazie per la risposta...
Ho provato ma niente ancora...
Ho trovato questa funzione
#define LINEBUF_SIZE 129
char databuffer[LINEBUF_SIZE];
byte last_read_timed_out=0;
unsigned int read_line(unsigned long timeout_ms=2000) {
//Reads a line of text from the 'duino Serial library's buffer (128 bytes unless changed).
//Reads until
// - a newline (which then makes it to the buffer - can make some string tests easier)
// - the buffer is full (hardcoded length)
// - reading a next character times out
// ...whichever comes first.
//
// Writes the a null-terminated string into databuffer,
// (so reads at most (LINEBUF_SIZE-1) useful characters)
// Returns the amount of bytes we read (==strlen(databuffer))
//
// On timeout:
// - Waits no longer than 'timeout_ms' milliseconds for each next character.
// - On timeout, sets last_read_timed_out=1
// - Note that currently function only returns 0 when it timed out reading
// anything at all, so that can also be a simple test for timeout
// - Timeout values of <30ms can be too short
//
// On newlines:
// - was written with the idea that you may get only CR or LF
// (not sure if that's ever true)
// - will not emit empty lines
// - which combines to mean that it will leave \n in the Serial buffer
// and only ignores it on the next call of read_line(). This is significant
// when you're doing data exchange, because that byte will mess things up.
char *tptr=databuffer;
unsigned long last_byte_time;
last_read_timed_out=0;
databuffer[0]=0x00; // (make sure empty string on timeout is null terminated)
last_byte_time=millis(); // used in a 'last byte was read this long ago' test
while (true) { //(not and endless loop, broken off by timeout if not by end-of-line)
//If the buffer is full, break off.
if ((tptr-databuffer) > (LINEBUF_SIZE-2)) { //verify for off-by-one error.
// -2 so that the last position can safely be set to the null terminator.
tptr[1]=0x00;
break;
}
// Read a byte, if it's there
if (Serial.available()>0) { // If there's a byte to be read,
tptr[0]=Serial.read(); // read it in
tptr[1]=0x00; //(could be postponed until we know we're done, actually)
tptr++;
last_byte_time=millis(); // update 'last byte read came at' time
// Terminate reading on a newline, deal with empty lines by ignoring them
if ( ((tptr[-1]=='\n') || (tptr[-1]=='\r')) && ( ((int)(tptr-databuffer)) == 1 ) )
{ //empty line or crlf? Pretend this line didn't exist, start over with next character.
tptr=databuffer;
databuffer[0]=0x00;
continue;
}
if (tptr[-1]=='\r') { // CR, \r, but not at start of line - assume this is a complete response, return it
break;
} //note that this leaves the \n from a CRLF in the Serial buffer. This function deals with it, your manual Serial code may not.
continue; //we read a byte, skip timeout check this round
} else { //no byte available yet
//delay(1); //probably unnecessary.
}
// Time out?
if (millis()-last_byte_time > timeout_ms) { //TODO: check behaviour at millis overflow
tptr[1]='\0';
last_read_timed_out=1; //signal that we timed out
break;
}
}
// I use software-serial for debug to print out the time of receive,
// the just-read line's length, string representation, and hex representation
// (omitted here for relative brevity)
return (int)(tptr-databuffer); // amount of bytes we read into databuffer (TODO: check)
}
int get_signal_strength() {
//returns dBm. Returns 0 on error/unknown.
module_write("AT+CSQ\r");
char ret=0;
for (int i=0;i<2;i++) { //should be +CSQ and OK
read_line(300); //Probably takes 200ms or less, but hey
if (startswith(databuffer,"+CSQ")) { //should look like "+CSQ: 15,99"
ret = atol(&databuffer[5]);
if (ret==99)
ret=0;
else
ret = -113+2*ret;
}
}
return ret;
}
inline byte startswith(char *str, char *start, int n=-1) {
//Returns whether str stats with start.
//Example use: if (startswith(read_buffer, "+CSQ"))
//The n parameter lets you check only the first n characters
// instead of all of the length of start
// (and, in theory, lets you hardcode to avoid a strlen).
// I haven't used this but I figured it could be useful.
if (n==-1)
n=strlen(start);
return ((strncmp(str,start,n))==0);
}
Mettendo un:
if (get_signal_strenght()>0)
{
// codice per fare una chiamata persa
}
Non succede niente. =(
Non capisco perché a volte facendo un semplice print di una stringa (esempio: Serial.print("test") ) vedo il comando sull'output, altre volte no. Volevo vedere se la variabile databuffer avevo qualcosa ... ma sembra niente...
Può essere che sbaglio qualcosa, nessuno ha mai provato qualcosa del genere ?