DKWatson:
pretty simple to set up fixed length character arrays.
Not that I think CSV field parsing is the OP's issue... but you don't need extra field storage, either.
I've removed String from my keyboard, had to borrow one to type this message.
LOL, here's a version you can type:
char fullMsg[ 120 ];
const uint8_t MAX_FIELD_COUNT = 25;
char *field[ MAX_FIELD_COUNT ];
uint8_t fieldCount;
void getFields( const char *delim = "," )
{
fieldCount = 0;
field[ fieldCount ] = strtok( fullMsg, delim );
while (field[ fieldCount ]) {
fieldCount++;
if (fieldCount >= MAX_FIELD_COUNT)
break;
field[ fieldCount ] = strtok( nullptr, delim );
}
}
It's short, it's completely safe, and it doesn't require additional storage for the fields.
Oh yes... it's ten times faster, saves about 1500 bytes of program space, and the sketch will run forever.
Cheers,
/dev
P.S. A complete sketch for your testing pleasure:
char fullMsg[ 120 ];
const uint8_t MAX_FIELD_COUNT = 25;
char *field[ MAX_FIELD_COUNT ];
uint8_t fieldCount;
void getFields( const char *delim = "," )
{
fieldCount = 0;
field[ fieldCount ] = strtok( fullMsg, delim );
while (field[ fieldCount ]) {
fieldCount++;
if (fieldCount >= MAX_FIELD_COUNT)
break;
field[ fieldCount ] = strtok( nullptr, delim );
}
}
void setup()
{
Serial.begin( 9600 );
}
void loop()
{
if (lineReady()) {
getFields();
printFields();
}
}
void printFields()
{
for (auto i = 0; i < fieldCount; i++) {
Serial.print( F("field[ ") );
Serial.print( i );
Serial.print( F(" ] = '") );
Serial.print( field[i] );
Serial.print( F("', as int: ") );
Serial.print( atoi( field[i] ) );
Serial.print( F(", or as float: ") );
Serial.println( atof( field[i] ) );
}
Serial.println();
}
const size_t MAX_CHARS = sizeof(fullMsg);
size_t msgLen = 0;
bool lineReady()
{
bool ready = false;
const char endMarker = '\n';
while (Serial.available()) {
char c = Serial.read();
if (c != endMarker) {
// Only save the printable characters, if there's room
if ((' ' <= c) and (msgLen < MAX_CHARS-1)) {
fullMsg[ msgLen++ ] = c;
}
} else {
// It's the end marker, line is completely received
fullMsg[msgLen] = '\0'; // terminate the string
msgLen = 0; // reset for next time
ready = true;
break;
}
}
return ready;
} // lineReady