Bonjour à tous!
J'essaye de comprendre un truc.
Quand je lance une commande AT
come
Serial.print("AT")
Serial.println("AT+CGPSSTATUS?");
Ca doit retourner des valeurs correspondantes à ces commande.
Apparament ces valeurs peuvent être mélangée avec un
Serial.println("coucou");
J'ai lu dans ce document, à la page 13
http://www.dfrobot.com/image/data/TEL0051/3.0/SIM908_AT%20Command%20Manual_V1.01.pdf
ceci:
The "AT" or "at" prefix must be set at the beginning of each Command line. To terminate a
Command line enter .
Commands are usually followed by a response
that includes. ""
Throughout this document, only the responses are presented, are omitted
intentionally
Donc si j'ai bien compris, avec ceci:
void setup()
{
Serial.println("AT+CGPSSTATUS?");
}
void loop()
{
if (Serial.available())
{
Serial.write(Serial.read());
}
}
}
Je devrais pour voir lire la réponse de
Serial.println("AT+CGPSSTATUS?");
Je pense que je ne suis pas faux, n'es-ce pas?
Mainteant j'aimerais savoir comment je peux alors filtrer la réponse, ce qui se trouve entre
<CR><LF>et<CR><LF>
Es-ce que je suis sur la bonne voix?
Pourriez-vous m'éclairecir?
Ce qui m'intéresse alors, c'est comment mettre dans un buffer
<CR><LF><response><CR><LF>
Je vous remercie
Serial.read() te retourne le message en retour caractère par caractère. Il suffit de placer ceux-ci dans un tableau de char et d'interpréter le contenu.
Oui mais comment filtrer uniquement la réponse du commande AT.
Serial.read() m'affichera aussi bien
Serial.print("cocucou") et la réponse de
Serial.print("AT");
et je souhaite filtrer la réponse d'une comamnd AT
Enfin, je vois mal un modem répondre coucou à une commande AT.
Pour filtrer la réponse des commandes AT, après avoir émit la commande, il faut tester les caractères reçues pour trouver la suite + qui indique le début d'une réponse. Stocker les caractères qui arrivent jusqu'à rencontrer une nouvelle suite + qui indique la fin de la réponse.
Oui fdufnew, entre temps, j'ai chercher une solution, mais je ne suis pas sure. Pourrais-tu jeter un oeil et me corriger
#define BUFFERSIZE 200
chat buffer[BUFFERSIZE];
void setup()
{
Serial.begin(9600);
// JE NE METS PAS LE CODE QUI SUIT. CA FONCTION ET E PROBLEM EST AU MOMENT OU JE LANCE LA COMMANDE
// JE LANCE UNE COMMANDE AT
Serial.print("AT");
// JE RECUPERE UNIQUEMENT LA REPONSE DE LA COMMANDE AT
read_String(5000);
}
char read_String(int timeout)
{
unsigned long previous;
previous = millis();
Serial.println(F("DISPLAY BUFFER:"));
index=0;
do
{
if(Serial.available() > 0) // Don't read unless
// there you know there is data
{
Serial.println("1");
if (Serial.peek()==13) // check if CR (without reading)
{
Serial.println("13");
Serial.read(); // read and ignore
if (Serial.peek()==10) // then check if LF (without reading)
{
Serial.println("10");
if(index < Serial.readBytesUntil(13, buffer, BUFFERSIZE-1)) // One less than the size of the buffer array
{
Serial.println("b");
inChar = Serial.read(); // Read a character
buffer[index] = inChar; // Store it
index++; // Increment where to write next
buffer[index] = '\0'; // Null terminate the string
}
}
}
}
}while(((millis() - previous) < timeout));
Serial.println(buffer);
buffer[0]='\0';
Serial.println(F("END DISPLAY BUFFER"));
}
Merci beaucoup
En fait, dans la fonction read_String() ci-dessus, la commande AT est exécutée, la fonction read_String est appellée dans le but d'afficher le retour de la command. Soit ce qui se trouve entre CR+LF.
Après 5 seconde, il sort bien de la boucle. Mais en revanche
Serial.println("13");
Serial.println("10");
ne sont pas affiche, surement parce que les conditions if Serial.peek() retourn false
Pourrais-tu m'aiguiller?
Milles mercis
Il faut systématiquement faire un Serial.available() avant de faire un Serial.read().
Le code s'exécute beaucoup plus vite que l'arrivée des caractères. A 9600 bauds il arrive un caractère toutes les millisecondes environ. Le micro qui tourne à 16MHz exécute une instruction toutes les 63ns (une ligne de C c'est plusieurs instructions évidemment). A la grosse on va dire entre 10 et 20 instructions par ligne de C. Donc environ une ligne de C toutes les microsecondes. Soit un rapport mille entre l'exécution du code et l'arrivée des caractères.
Salut fdufnews,
Merci.
D'accord.
J'ai fait un fichier read_at_string.ino avec que les fonctions utilses et pour fournir le code complet.
Les fonctions qui concerne ce post, sont loop() et read_AT_string() (J'ai renomer read_String()).
Donc dans read_AT_string() il faudrait ajouter Serial.available(). OK. je vais ajouté ceci
Ce qui m'interpelle, ce qu'avant le Serial.read() il y a if (Serial.peek()==13) et cette condition retourne false parce que Serial.println("13"); n'affiche rine
Voici le code complet.
#include <SoftwareSerial.h>
int baud_rate = 9600;
int pin_gsm = 3;
int pin_gps = 4;
int pin_power = 5;
//int pin_dtr = 6;
boolean debug = true;
boolean raedy_to_go = false;
// Reading String
#define BUFFERSIZE 200
char buffer[BUFFERSIZE];
char inChar;
int index;
void setup()
{
Serial.begin(baud_rate);
delay(5000); // Wait for 5sec after begin
if(debug)
{
Serial.println(F("\n****************************"));
Serial.println(F("STARTING SYSTEM Read AT stream"));
Serial.println(F("******************************"));
}
pinMode(pin_gsm,OUTPUT); // Set the pins
pinMode(pin_gps,OUTPUT);
pinMode(pin_power,OUTPUT);
powerUpSim908:
if(powerUpSim908())
{
delay(1000);
if(gps_power()){
gsm_enable();
raedy_to_go = true;
if(debug)
{
Serial.println(F("\n****************************"));
Serial.println(F("READY TO GO\n"));
Serial.println(F("****************************\n"));
}
}
else
{
raedy_to_go = false;
if(debug)
{
Serial.println(F("\nNOT READY TO GO.\nGPS could not be power\nRestart the module\nor/and check the battery level.\n"));
}
goto powerUpSim908;
}
}
else
{
raedy_to_go = false;
if(debug)
{
Serial.println(F("\nNOT READY TO GO.\nCheck the battery level.\n"));
}
};
}
void loop()
{
/*
if (Serial.available())
{
Serial.print("Character received: ");
Serial.write(Serial.read());
Serial.println("");
}
*/
if(raedy_to_go)
{
read_AT_string("AT",5000);
delay(10000);
}
}
char read_AT_string(char* command, int timeout)
{
unsigned long previous;
previous = millis();
Serial.println(F("DISPLAY BUFFER:"));
index=0;
Serial.println(command);
do
{
if(Serial.available() > 0) // Don't read unless
// there you know there is data
{
Serial.println("1");
if (Serial.peek()==13) // check if CR (without reading)
{
Serial.println("13");
if (Serial.available() > 0) {
Serial.read(); // read and ignore
if (Serial.peek()==10) // then check if LF (without reading)
{
Serial.println("10");
if(index < Serial.readBytesUntil(13, buffer, BUFFERSIZE-1)) // One less than the size of the buffer array
{
Serial.println("b");
inChar = Serial.read(); // Read a character
buffer[index] = inChar; // Store it
index++; // Increment where to write next
buffer[index] = '\0'; // Null terminate the string
}
}
} // END OF THE SECOND Serial.available()
}
}
}while(((millis() - previous) < timeout));
Serial.println(buffer);
buffer[0]='\0';
Serial.println(F("END DISPLAY BUFFER"));
}
/* FUNCTION */
boolean powerUpSim908(void)
{
if(debug)
{
Serial.println(F("Powering up SIM908"));
}
boolean turnedON = false;
//uint8_t answer=0;
int cont;
for (cont=0; cont<3; cont++)
{
digitalWrite(pin_power,HIGH);
delay(1500);
digitalWrite(pin_power,LOW);
Serial.println(F("Checking if the module is up"));
if(sendATcommand("AT", "OK", 5000))
{
cont = 4; // Leave the loop
turnedON = true;
}
else
{
turnedON = false;
if(debug)
{
Serial.println(F("\nTrying agin to turn on SIM908"));
}
};
}
if(turnedON)
{
if(debug)
{
Serial.println(F("Module is tunrned up\n"));
}
}
else
{
if(debug)
{
Serial.println(F("Module is NOT tunrned ON\n"));
}
}
return turnedON;
}
boolean sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout)
{
uint8_t x=0;
bool answer=false;
//åchar response[100];
//buffer[0]='\0';
unsigned long previous;
//memset(response, '\0', 100); // Initialice the string
//Serial.println(response);
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
if (ATcommand[0] != '\0')
{
Serial.println(ATcommand); // Send the AT command
}
x = 0;
previous = millis();
index=0;
do
{
if(Serial.available() > 0)
// there you know there is data
{
if(index < BUFFERSIZE-1) // One less than the size of the array // Same as buffer size
{
inChar = Serial.read(); // Read a character
buffer[index] = inChar; // Store it
index++; // Increment where to write next
//Serial.println(index);
buffer[index] = '\0'; // Null terminate the string
}
}
}while(((millis() - previous) < timeout));
if(strstr(buffer,"NORMAL POWER DOWN") != NULL)
{
answer = false;
}
else if (strstr(buffer, expected_answer) != NULL) // check if the desired answer (OK) is in the response of the module
{
/*
Serial.println(F("### BUFFER"));
Serial.println(buffer);
Serial.println(F("### END BUFFER"));
*/
answer = true;
}
else
{
answer = false;
}
if(debug)
{
if(answer)
{
//Serial.println(F("Expected answer : OK!\n"));
}
else
{
//Serial.println(F("Expected answer : KO!\n"));
};
}
return answer;
}
void gps_enable(void)
{
if(debug)
{
Serial.println(F("\nEnabling GPS ..."));
}
digitalWrite(pin_gps,LOW); //Enable GPS mode
digitalWrite(pin_gsm,HIGH); //Disable GSM mode
delay(2000);
}
void gsm_enable(void)
{
if(debug)
{
Serial.println(F("\nEnabling GSM ..."));
}
digitalWrite(pin_gsm,LOW); //Enable GSM mode
digitalWrite(pin_gps,HIGH); //Disable GPS mode
delay(2000);
}
/* UTILISTIES */
/* GPS */
boolean gps_power(void) //turn on GPS power supply
{
/*
Serial.println("AT");
delay(2000);
*/
boolean gpspwr = false;
boolean gpsrst = false;
if(sendATcommand("AT+CGPSPWR=1","OK",2000))
{
gpspwr = true;
if(debug)
{
Serial.println("turn on GPS power supply => OK");
}
}
else
{
if(debug)
{
Serial.println("turn on GPS power supply => KO");
}
};
//delay(1000);
if(sendATcommand("AT+CGPSRST=1","OK",2000))
{
gpsrst = true;
if(debug)
{
Serial.println("reset GPS in autonomy mode => OK");
}
}
else
{
if(debug)
{
Serial.println("reset GPS in autonomy mode => KO");
}
}; //reset GPS in autonomy mode
delay(1000);
if(gpspwr && gpsrst)
{
return true;
}else
{
return false;
}
}
Merci