youeyg96:
Thank you all for the help thus far. It is greatly appreciated. Further input on this issue would be fantastic.
you've gone and mixed up synchronous and asynchronous Serial handling and then some!
also, don't mix apples and oranges.... parse your message as one function and then handle the message with another function.
here is an example, using your data, of capturing the Serial message and putting it into a struct that you can then pass to a function. With this structured data, you can then develop a function that handles the data:
const size_t MAX_MESSAGE_BUFFER_LENGTH = 64;
// message looks like this: <i,j,k,roll,pitch,yaw>
//tested using this data: <1.1,2.2,3.3,4.4,5.5,6.6>
struct Command {
float i;
float j;
float k;
float roll;
float pitch;
float yaw;
};
template<class T> inline Print &operator << (Print &object, T argument)
{
object.print(argument);
return object;
}
void setup()
{
Serial.begin(9600);
if (Serial)
{
Serial << (F("let's go!\n"));
}
}
void loop()
{
if (const char* packet = checkForNewPacket(Serial, '<', '>')) // checks for message on Serial with start and end marker
{
Serial << (F("\nThis just in...\n")) << (packet) << (F("\n\n"));
Command newCommand;
if (parsePacket(newCommand, packet)) {
Serial << (F("i = ")) << newCommand.i << (F("\n"));
Serial << (F("j = ")) << newCommand.j << (F("\n"));
Serial << (F("k = ")) << newCommand.k << (F("\n"));
Serial << (F("roll = ")) << newCommand.roll << (F("\n"));
Serial << (F("pitch = ")) << newCommand.pitch << (F("\n"));
Serial << (F("yaw = ")) << newCommand.yaw << (F("\n"));
processCommand(newCommand);
} else {
Serial << (F("Recieved Bad Packet\n"));
}
}
}
void processCommand(const Command& cmd) {
// process your command here!!!
}
bool parsePacket(Command& cmd, const char* packet) {
// error check:
size_t commaCount = 0;
char* ptr = packet;
while (ptr) {
if(ptr = strchr(ptr, ',')){
ptr++;
commaCount++;
}
}
if(commaCount !=5) {
return false;
}
ptr = packet;
cmd.i = atof(ptr);
ptr = strchr(ptr, ',');
ptr++;
cmd.j = atof(ptr);
ptr = strchr(ptr, ',');
ptr++;
cmd.k = atof(ptr);
ptr = strchr(ptr, ',');
ptr++;
cmd.roll = atof(ptr);
ptr = strchr(ptr, ',');
ptr++;
cmd.pitch = atof(ptr);
ptr = strchr(ptr, ',');
ptr++;
cmd.yaw = atof(ptr);
return true;
}
const char* checkForNewPacket(Stream& stream, const char startMarker, const char endMarker)
{
static char incomingMessage[MAX_MESSAGE_BUFFER_LENGTH] = "";
static unsigned char idx = 0;
static bool gotStartMarker = false;
if (stream.available()) {
if (!gotStartMarker) {
if (stream.read() == startMarker) {
gotStartMarker = true;
}
} else {
incomingMessage[idx] = stream.read();
if (incomingMessage[idx] == endMarker) {
incomingMessage[idx] = '\0';
idx = 0;
gotStartMarker = false;
if (!strlen(incomingMessage)) { //startMarker followed immediatly by endMarker (i.e.no data)
//stream << (F("{\"error\":\"no data\"}\n")); // you can return an error to sender here
return nullptr;
}
//stream << (F("{\"success\":\"")) << (incomingMessage) << (F("\"}\n")); // or return a success messaage
return incomingMessage;
} else {
idx++;
if (idx > MAX_MESSAGE_BUFFER_LENGTH - 1) {
stream << (F("{\"error\":\"message exceeded ")) << (sizeof(incomingMessage) - 1) << (F(" chars\"}\n")); // another error to sender here
idx = 0;
gotStartMarker = false;
incomingMessage[idx] = '\0';
}
}
}
}
return nullptr;
}