Im trying to parse a comma delimited serialread string to use as variables or act on the result.
In my searching around I found a post that seems to be helpful and while not exactly what I was looking should work until I figure out how to use a variable from a function outside that function and will cleanup the code.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231961642
this test snippet works fine.
#include <string.h>
char *record = "name:bob";
char *p, *i;
void setup() {
Serial.begin(9600);
Serial.println("Starting..");
// First strtok iteration
p = strtok_r(record,":",&i);
Serial.print(p);
Serial.print(" = ");
// Second strtok iteration
p = strtok_r(NULL,":",&i);
Serial.print(p);
Serial.println("");
}
void loop () {
}
Now when I Frankenstein it into my current sketch I get compiler errors because I'm mixing types char*, char and int. Problem being I'm about 2 steps over my head on this and I havent been able to find the syntax to convert it.
p = strtok(record,":",&i);
g:/dev/arduino/arduino-0022/hardware/tools/avr/lib/gcc/../../avr/include/string.h: In function 'void doSensorcheckanddisplay()':
g:/dev/arduino/arduino-0022/hardware/tools/avr/lib/gcc/../../avr/include/string.h:145: error: too many arguments to function 'char* strtok(char*, const char*)'
GroPi5000v0_2:228: error: at this point in file
GroPi5000v0_2:233: error: cannot convert 'int*' to 'char**' for argument '3' to 'char* strtok_r(char*, const char*, char**)'
GroPi5000v0_2:236: error: ISO C++ forbids comparison between pointer and integer
GroPi5000v0_2:248: error: jump to case label
GroPi5000v0_2:227: error: crosses initialization of 'char* record'
GroPi5000v0_2:252: error: jump to case label
GroPi5000v0_2:227: error: crosses initialization of 'char* record'
int availableBytes = Serial.available();
if (availableBytes > 0)
{
for(int i=0; i<availableBytes; i++)
{
int ch = Serial.read();
switch(ch)
{
case '#':
char *record = string;
p = strtok(record,":",&i);
Serial.print(p);
Serial.print(" = ");
// Second strtok iteration
p = strtok_r(NULL,",",&i);
Serial.print(p);
Serial.println("");
if (CO2LevelValue < p)
{
digitalWrite(co2relaypin, HIGH); // turn the co2 on
}
else {
digitalWrite(co2relaypin, LOW); // set the LED on
}
Serial.println(string);
curWritePos=0;
string[curWritePos]=0;
break;
case '
** edited for viewing pleasure
what this does is get check for a string with $ startbit and # stopbit and parses the csv in between. eg $123,112.4,1000,1,2,6.5#
And then each strtok iteration lets me act on that value.
Of course ideally I would just be creating variables with each strtok but I could never get a variable from inside a function to work outside it or in another function. But alas a topic for another day.
I think I gave enough code and detail but let me know if more clarification is needed.
:
curWritePos=0;
string[curWritePos]=0;
break;
default:
if (curWritePos < sizeof(string)-1)
{
string[curWritePos++]=ch;
string[curWritePos]=0;
}
else
{
// Serial.println("Overflow\n");
}
break;
}
}
}
** edited for viewing pleasure
what this does is get check for a string with $ startbit and # stopbit and parses the csv in between. eg $123,112.4,1000,1,2,6.5#
And then each strtok iteration lets me act on that value.
Of course ideally I would just be creating variables with each strtok but I could never get a variable from inside a function to work outside it or in another function. But alas a topic for another day.
I think I gave enough code and detail but let me know if more clarification is needed.