Okay.
Here's SerialPreset.h
/*
SerialPreset.h - Library for serial packets.
Created by Aid Vllasaliu, 24 pril 2012.
Use it as you want :)
Found useful code in the Arduino Forum.
delphuino.blogspot.com
*/
#ifndef SerialPreset_h
#define SerialPreset_h
#include "Arduino.h"
class SerialPreset
{
public:
SerialPreset();
String SerialRead();
};
extern SerialPreset SerialPreset;
#endif
...and SerialPreset.cpp
#include "Arduino.h"
#include "SerialPreset.h"
#define SOP '<' //Start of packet
#define EOP '>' //End of packet
String SOCP = "([";
String EOCP = ")]";
String CPS = ",|";
bool started = false;
bool ended = false;
char inData[256];
byte index;
boolean Echo;
String HardwareCommands[4] = {"Analog", "Digital", "PWM", "Tx"};
SerialPreset::SerialPreset(){ }
String SerialPreset::SerialRead() {
while(Serial.available() > 0)
{
//Start serial thingy now!
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 256)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Return the packet
return inData;
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
... and finally the sketch:
#include <SerialPreset.h>
void setup()
{
//Set Serial Baudrate
Serial.begin(115200);
}
void loop(){
String InData = SerialPreset.SerialRead();
if (InData<>""){
//Do something
}
}