Bonsoir à tous,
Voila un programme que j'ai trouvé sur ce forum il y à quelque mois, ce programme était réalisé par un certain "luj" si mes souvenirs son bon. Ce programme je l'utilisais sans cesse sans même me demander comment il fonctionne. J'ai donc décidé de l'analyser mais quelques questions renstent en suspent.
Ce programme permet de lire des chaines de caractères envoyés par port série:
// numerical value to be decoded after specific commands
int value = 0;
// where we currently are in the sequence construction or decoding
int indice = 0;
// the sequence of characters where is stored what the user wants
char commande[40]; // reduce the length to save memory (if necessary)
/**
*
*/
void setup()
{
Serial.begin(9600);
}
/**
*
*/
void loop()
{
// Start a new reception, reinit the browser index of the array
indice = 0;
// Fill-in the sequence of chars with all received characters
while (Serial.available())
{
commande[indice++] = Serial.read();
}
// End the array with a specific character (E=End)
// (means that you can't choose 'E' as a user meaning command
commande[indice] = 'E';
// Don't continue the loop if no command (adapt to your own code)
if (indice == 0) return;
// DEBUG Display that a new sequence has been found
//Serial.print("> Received command, length=");
//Serial.println(indice);
// Start parsing the sequence array
indice = 0;
while (commande[indice] != 'E')
{
// Reinit the numerical value
value = 0;
//
switch (commande[indice])
{
case 'S':
Serial.println("> Start command.");
break;
case 'G':
// Continue to the next char (could contain the numerical value
indice++;
while (commande[indice] >= 48 && commande[indice] <= 57)
{
value = value * 10 + (commande[indice]-48);
// next digit
indice++;
}
// Back if no more digit to be added to numerical value
indice--;
Serial.print("> servo gauche : ");
Serial.println(value,DEC);
break;
case 'D':
// Continue to the next char (could contain the numerical value
indice++;
while (commande[indice] >= 48 && commande[indice] <= 57)
{
value = value * 10 + (commande[indice]-48);
// next digit
indice++;
}
// Back if no more digit to be added to numerical value
indice--;
Serial.print("> servo droit : ");
Serial.println(value,DEC);
break;
default:
//Serial.print("> unrecognized command : ");
//Serial.println(commande[indice]);
break;
}
indice++;
}
}
Mes questions sont :
-Pourquoi initialiser "char commande[40]" et non pas à 0?
-Pourquoi "while (Serial.available()) " et non pas "while (Serial.available() > 0") ?
- Et pourquoi "while (commande[indice] >= 48)" et non pas 40, 41, 42 ou 43...?
Mes questions vous sembleront peut être bêtes et inutiles mais je désire vraiment comprendre ce programme.
Par avance merci pour l'aide que vous m'apporterai !!
Maxime