SerialPreset.h
#ifndef SerialPreset_h
#define SerialPreset_h
#include "Arduino.h"
class SerialPreset
{
public:
SerialPreset();
String SerialRead();
};
extern SerialPreset SerialPreset;
#endif
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';
}
}
You'll probably recognize the above code, I found it here on the forum.
I just figured that my sketches would be more organized if I put the code in a serparate library.