Hey guys,
I'm writing a data parser that is converting and transmitting a large text files of comma-separated instructions (100-10,000 instructions per file) to an Arduino for execution on phsyical hardware. The instructions each occupy a line in the text file, ending with a '\n' but also starting and ending with special characters. FYI they look like this '<4,4294,8269,4181,9150,0,;'
I've written a Processing program that segments the file into chunks of 4-10 instructions which are transmitted over Serial and interterpreted by the Arduino. The processing part is going fine but I'm running into some issues in Arduino.
Essentially, I do my parsing by reading Serial data into a char array buffer while using the start and end characters of each instruction to copy the contents into entries in a string array, this action resets the buffer. This is convienient as I can later split and execute the contents of each array entry in another section of the code and can keep the buffer relatively small in the case that I need to transmit a bunch of instructions in one go.
This sounds like it should work but when I try and print the string array out of the arduino serial buffer I just get empty lines. I know that the data is being received as the buffer shows the correct contents and the arduino is counting the number of instructions correctly.
I'm wondering if there are some issues copying from the char array to a string (i.e., I can't use strcpy()) and was considering using a 2 dimensional char arrays (a list of arrays is what I'm after), but couldn't seem to get the syntax correct (or find an example) for copying the data in the form.
char c2dArray[30][200];
char buf[200] = "<4,4294,8269,4181,9150,0,;";
c2dArray[1][1:200] = buf; // copy buffer contents to entry one, using all the characters necessary
Any help and suggestions appreciated. This is driving me nuts!
Here's my parsing function:
String lines[30]; // array of instructions from incoming serial data
char buf[200]; // a place to store incoming data
case 0: // waiting for data / recieving data mode
while (Serial.available()>0 && i < 200) { // if there is data and we're not exceeding the index
char incomingByte = Serial.read(); // read the first byte of the message
// store the incoming char in buffer
buf[i] = incomingByte;
buf[i+1] = '\0'; // keeps the string null terminated
if (incomingByte == '>'){ // start of normal instructions
num_commands++; // increment number of instructions
}
if (incomingByte == '<'){ // start of last instuction in group
num_commands++; // increment number of instructions
last_line = true; // set last line flag as high
}
if (incomingByte == ';'){ // last character of each line
lines[num_commands-1] = buf; // store buffer in string array
i = -1; // reset position at start of buf array
// if end of last line in current instruction block reached
if (last_line == true){
Serial.write("\n num_commands = ");
delay(10);
Serial.println(num_commands);
delay(10);
Serial.println(buf); // print current buffer contents
// repeat contents of string array
for (int j=0; j<num_commands;j++){
Serial.println(lines[j]); // returns empty strings over serial
}
Mode = 1; // change to data splitting mode
}
} // incomingByte = ';'
i++;
}
break;