SerialCommand

De MCHobby - Wiki
Révision datée du 7 avril 2012 à 21:07 par Admin (discussion | contributions) (Page créée avec « == Description == SerialCommand est un projet simple visant à mettre en place un interpréteur de commande rudimentaire sur le port série. Le but est d'attendre que l'util... »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Sauter à la navigation Sauter à la recherche

Description

SerialCommand est un projet simple visant à mettre en place un interpréteur de commande rudimentaire sur le port série.

Le but est d'attendre que l'utilisateur entre un mot clé ("on" ou "off") sur le Moniteur Série d'Arduino et d'allumer la LED de la Pin 13 (visible sur la carte) en fonction de l'ordre reçu.

Cet exemple vraiment rudimentaire sera par la suite utilisé comme fondation dans autre exemple du GPRS shield.

Principe de fonctionnement

Une ligne de commande est composé d'un ensemble de caractères reçus via la liaison série.

Chaque caractère est empilé dans un buffer (mémoire tampon) de commande en attendant le caractère de fin de commande (celui qui exécutera la commande).

La fin de commande attendu est le caractère Carriage Return (aussi noté <CR>) correspondant à la touche de Entrée/Return/Enter sur un clavier. C'est la touche de retour chariot.

La valeur numérique de <CR> est 13.

Le retour chariot étant utilisé comme caractère de fin de commande vérifiez que le moniteur série d'Arduino IDE soit bien configuré sur Carriage return

Une fois le caractère de fin de commande détecté, la commande est envoyée à la fonction "ProcessCmd" en charge de la détection de la commande et de son exécution.

Code de démonstration

le code de démonstration ci-dessous envois également de nombreux messages de débogage sur le port série. Cela permet de se rendre compte assez facilement du fonctionnement interne du programme.

 
/* String Command In Serial Port using String 
  
    The program waits for a command from the serial monitor.
    Make sure than IDE Serial Monitor is configured on "Carriage Return"
      Entering 'on' will turn the led connected to pin 13.
      Entering 'off' will turn the led 13.
    This provides an alternative for Bitlash.
    
    This is not the best and only way to get a string command
    from the serial monitor. I just created this because I did
    not find any example. Any suggestions are welcome.
    
    The circuit:
    Pin 13 is connected to a 220-ohm resistor, which is then
    connected to a LED and then grounded. As an alternative,
    you may look at the  arduino in-built led in pin 13.
    
    created July 21, 2010, by Mark Aosawa. mark.aosawa@yahoo.com
    Widely Modified April 7, 2012 by MC Hobby, info@mchobby.be    
    
  */
  
  //String buffer
  String cmd = String("");
  
  // initialize the LED pin 2 as output
  // as an alternative, you may use int ledPin=13 if your
  // arduino has a built-in led (use the blink example to check
  int ledPin = 13;
  
  void setup() {
  // initialize serial communication (and baud rate of 9600):
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
  //prints a message when the serial monitor is opened
  //to check if the program is working
  Serial.println("Serial communication is ready.");
  }

  void loop() {

    //check if serial communication is available
    if (Serial.available() > 0) {
      
       //temporary variable to hold the current character
       char SerialInByte;
       //read the data
       SerialInByte = Serial.read();
      
       if(SerialInByte==13){ 
          ProcessCmd();
       }else{
          //store the current character in the string buffer, cmd
          cmd += String(SerialInByte);
          Serial.print( "SerialInByte: " );
          Serial.print( SerialInByte, DEC );
          Serial.print( ", Buffer " );
          Serial.println( cmd );
       }
    }
  }
  
  //echoes the command typed
  void printCmd(){
    Serial.println(cmd);
  }
  
  //resets the string buffer and the counter
  void clearCmd(){
    cmd="";
    Serial.flush();
  }
  
  //command interpreter
  void ProcessCmd() {
    Serial.print( "ProcessCmd for [" );
    Serial.print( cmd );
    Serial.println( "]" );
    
    if(cmd.equals("on")){
         printCmd();
         digitalWrite(ledPin, HIGH);
         clearCmd();
    }else if(cmd.equals("off")){
        printCmd();
        digitalWrite(ledPin, LOW);
        clearCmd();
    }else{
         //any other command is ignored
         Serial.println("unknown command!");
         clearCmd();
    }
}