String Command In Serial Port using WString Lib


i've been developing sort of console arduino without use of bitlash. used wstring library instead.

entering 'on#' turn led on, while 'off#' turn led off. other commands ignored (but must still terminated #).

the terminating character problem. goal replace '#' '\n' or '\r', instead of typing 'on#', have type 'on'. not work on arduino serial monitor.

any comments/suggestions appreciated. thank you.

this code.

code: [select]
 /* string command in serial port using wstring library
 
   the program waits command serial monitor.
   entering 'on#' turn led connected pin 2.
   entering 'off#' turn led off.
   this provides alternative bitlash.
   
   this not best , way string command
   from serial monitor. created because did
   not find example. suggestions welcome.
   
   the circuit:
   pin 2 connected 220-ohm resistor, then
   connected led , grounded. alternative,
   you may change code in setup if arduino has
   an in-built led in pin 13.
   
   created july 21, 2010
   by mark aosawa
   
   for comments or questions, feel free contact me at
   mark.aosawa@yahoo.com
   
 */
 
 #include <wstring.h>
 
 //the length of string buffer
 int cmdlength=20;
 //string buffer
 string cmd = string(cmdlength);
 //counter signifies current letter in string buffer, cmd
 int cmdindex = 0;
 
 // initialize led pin 2 output
 // alternative, may use int ledpin=13 if your
 // arduino has built-in led (use blink example check
 int ledpin = 2;
 
 void setup() {
 // initialize serial communication (and baud rate of 9600):
 serial.begin(9600);

 pinmode(ledpin, output);
 //prints message when serial monitor opened
 //to check if program working
 serial.println("serial communication ready.");
 }

 void loop() {

   //check if serial communication available
   if (serial.available() > 0) {
     
      //temporary variable hold current character
      char serialinbyte;
      //read data
      serialinbyte = serial.read();
     
      if(serialinbyte=='#'){ //for reason, serialinbyte=='\r' or serialinbyte=='\n' not work in arduino serial monitor
         processcmd();
      }else{
         //store current character in string buffer, cmd
         cmd[cmdindex]=serialinbyte;
         //adjust counter next character
         cmdindex++;
      }
   }
 }
 
 //echoes command typed
 void printcmd(){
         serial.println(cmd);
 }
 
 //resets string buffer , counter
 void clearcmd(){
   cmd=0;
   cmdindex=0;
 }
 
 //command interpreter
 void processcmd() {
   if(cmd.equals("on")){
        printcmd();
        digitalwrite(ledpin, high);
        clearcmd();
   }else if(cmd.equals("off")){
       printcmd();
       digitalwrite(ledpin, low);
       clearcmd();
   }else{
        //any other command ignored
        printcmd();
        clearcmd();
   }
}


the serial monitor not add terminating carriage return/line feed data entered in "send" field. so, can't rely on them being there.

you develop own application on pc add them, or there patch posted time ago modified serial monitor add them.

there have been many requests change behavior of serial monitor, @ least optionally, this, have been ignored far.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > String Command In Serial Port using WString Lib


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums