serial input with switch case

Hi all, I am new to the forum and Arduino - so apologies for any mistakes ahead of time.

I am trying to write a small program that will control a set of relays. I would like it to listen for any incoming characters in the serial port. when one arrives put it into a buffer and wait for the next one until it has a total of 6 characters. It would be triggered by an end of frame character to check 6 characters back that there was a valid start of frame character. this would validate that all the information made it.

At this point I would like to process the frame using switch case statements. The only twist I have is that based on the first two characters of the frame, I may want to echo back the remaining 3 characters back to the serial port. does this seam possible?

could you guys please suggest some pointers or sample programs that I might be able to expand to accomplish this?


It is all possible. This will give you a good starting point

Simple serial test code that might be of use.

//zoomkat 3-5-12 simple delimited ',' string 
//from serial port input (via serial monitor)
//and print result out serial port
//send on, or off, from the serial monitor to operate LED

int ledPin = 13;
String readString;

void setup() {
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial LED on/off test with , delimiter"); // so I can keep track

void loop() {

  if (Serial.available())  {
    char c =;  //gets one byte from serial buffer
    if (c == ',') {
      Serial.println(readString); //prints string to serial port out
      //do stuff with the captured readString
      if(readString.indexOf("on") >=0)
        digitalWrite(ledPin, HIGH);
        Serial.println("LED ON");
      if(readString.indexOf("off") >=0)
        digitalWrite(ledPin, LOW);
        Serial.println("LED OFF");
      readString=""; //clears variable for new input
    else {     
      readString += c; //makes the string readString