Creating a serial protocol

Hi Everybody! :slight_smile:

I found the following code on todbots blog:

 char* cmdbuf; char c; int i;
 while( Serial.available() && c!= '\n' ) {  // buffer up a line
   c = Serial.read();
   cmdbuf[i++] = c;
 }

 int i = 0;
 while( cmdbuf[++i] != ' ' ) ; // find first space
 cmdbuf[i] = 0;          // null terminate command
 char* cmd = cmdbuf;     //
 int cmdlen = i;         // length of cmd

 int args[5], a;         // five args max, 'a' is arg counter
 char* s; char* argbuf = cmdbuf+cmdlen+1;
 while( (s = strtok(argbuf, " ")) != NULL && a < 5 ) {
   argbuf = NULL;
   args[a++] = (byte)strtol(s,NULL,0); // parse hex or decimal arg
 }
 int argcnt = a;         // number of args read

I am trying to make a simple program where I can turn on or off 8 different LEDs based on input like "led 1 off". This code is a little out of my league, could someone who understands this give me some sort of working example? thanks in advance!

Bitlash was designed for just this kind of use. It will do all the serial input and command parsing for you so you can focus on the hardware and the behavior you want.

For your application you could send "d6=1" to turn on the LED on pin 6, for example.

More at http://bitlash.net

-br

http://entropymouse.com

thank you so much! its been a rough start for me but I am beginning to get this. here is what im working on. its just a shift register for lights for now:

#include "WProgram.h"
#include "bitlash.h"

// Declare a user function named "timer1" returning a numeric Bitlash value
//
//Pin connected to ST_CP of 74HC595
int latchPin = 44;
//Pin connected to SH_CP of 74HC595
int clockPin = 46;
////Pin connected to DS of 74HC595
int dataPin = 48;

numvar timer1(void) { 
      return TCNT1;       // return the value of Timer 1
}

void blinkie(numvar val) {

  //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, val);   
    //return the latch pin high to signal chip that it 
    //no longer needs to listen for information
    digitalWrite(latchPin, HIGH);
  
}

void setup(void) {
      initBitlash(57600);            // must be first to initialize serial port
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

      // Register the extension function with Bitlash
      //            "timer1" is the Bitlash name for the function
      //            0 is the argument signature: takes 0 arguments
      //            (bitlash_function) timer1 tells Bitlash where our handler lives
      //
      addBitlashFunction("timer1", 0, (bitlash_function) timer1);
      addBitlashFunction("blinkie", -1, (bitlash_function) blinkie);
}

void loop(void) {
      runBitlash();
}

its still not taking my hex values and displaying what i want bit its a start.