Hi everyone, I am new to the Arduino community and I'm in need of a little help, I Have tried to implement a function which splits an incoming string into 5 arguments using the strtok_r function, the problem I am facing, when I get the first argument or the command, it separates fine using an ' ' (space) as the delimiter then on the second call to strtok_r it first checks the next character in the original array to see if its a '"' (quote) or not, if its a quote it sets the delimiter as a quote and calls strtok_r, and places the quoted string into 1 argument, then i change the delimiter back to a space and before calling strtok_r again it checks if the next character is an quote and if true does the same again except it does not work as expected on the third call, from testing it has either output the third argument including the space if not quoted and with the quote if it was enclosed in quotes,
heres the code any help is much appreciated.
#include <SPI.h>
#include <SdFat.h>
#include <SdStream.h>
#include <SdFatUtil.h>
#include <SdBaseFile.h>
#include <SdFatmainpage.h>
#include <SdFatStructs.h>
#include <bufstream.h>
#include <SdFatConfig.h>
#include <SdInfo.h>
#include <ios.h>
#include <istream.h>
#include <ostream.h>
#include <SdFile.h>
#include <ArduinoStream.h>
#include <SdVolume.h>
#include <Sd2PinMap.h>
#include <Sd2Card.h>
#include <iostream.h>
#include <string.h>
#define SD_CS 53
#define SD_SS 4
unsigned short int serIn;
char serInString[100];
unsigned short int i;
unsigned short int serInIndx = 0;
unsigned short int serOutIndx = 0;
char *str1, *cmd, *arg1, *arg2, *arg3, *arg4;
unsigned short int cmdLen = 0, arg1Len = 0, arg2Len = 0, arg3Len = 0, arg4Len = 0;
String cmdStr, arg1Str, arg2Str, arg3Str, arg4Str;
unsigned short int argc = 0;
char copiedstr[100];
char *sscpy;
unsigned short int copiedstrIndx;
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
void setup() {
pinMode(SD_CS, OUTPUT);
Serial.begin(57600);
Serial.println("Hello World");
if(!card.init(SPI_FULL_SPEED, SD_SS))
{
Serial.println("SD Card Initialisation Failed");
return;
}
}
void readSerialString ()
{
argc = 0;
int sb;
if(Serial.available()) {
sb = Serial.read();
if(sb == '@')
{
while (Serial.available())
{
sb = Serial.read();
serInString[serInIndx] = sb;
serInIndx++;
}
for(copiedstrIndx = 0; copiedstrIndx < serInIndx; copiedstrIndx++)
{
copiedstr[copiedstrIndx] = serInString[copiedstrIndx];
}
sscpy = copiedstr;
}
}
}
void findArguments(char *sstring)
{
char delim = ' ';
char *saveptr1, *saveptr2, *saveptr3, *saveptr4, *saveptr5;
str1 = sstring;
cmd = strtok_r(str1, &delim, &saveptr1);
cmdStr = String(cmd);
cmdLen = cmdStr.length();
if(cmd != NULL)
{
argc++;
}
str1 = saveptr1;
if(serInString[cmdLen+1] == '"')
{
delim = '"';
}
arg1 = strtok_r(str1, &delim, &saveptr2);
arg1Str = String(arg1);
arg1Len = arg1Str.length();
if(arg1 != NULL)
{
argc++;
}
str1 = saveptr2;
delim = ' ';
if(serInString[cmdLen+1+arg1Len+2] == '"')
{
delim = '"';
}
arg2 = strtok_r(str1, &delim, &saveptr3);
arg2Str = String(arg2);
arg2Len = arg2Str.length();
if(arg2 != NULL)
{
argc++;
}
str1 = saveptr3;
delim = ' ';
if(serInString[cmdLen+1+arg1Len+2+arg2Len+2] == '"')
{
delim = '"';
}
arg3 = strtok_r(str1, &delim, &saveptr4);
arg3Str = String(arg3);
arg3Len = arg3Str.length();
if(arg3 != NULL)
{
argc++;
}
str1 = saveptr4;
delim = ' ';
if(serInString[cmdLen+1+arg1Len+2+arg2Len+2+arg3Len+2] == '"')
{
delim = '"';
}
arg4 = strtok_r(str1, &delim, &saveptr5);
arg4Str = String(arg4);
arg4Len = arg4Str.length();
if(arg4 != NULL)
{
argc++;
}
}
void printSerialString() {
if( serInIndx > 0) {
Serial.print("you said: ");
for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++) {
Serial.print( serInString[serOutIndx] );
}
serOutIndx = 0;
serInIndx = 0;
Serial.println();
str1 = NULL;
cmd = NULL;
arg1 = NULL;
arg2 = NULL;
arg3 = NULL;
arg4 = NULL;
}
}
void loop () {
readSerialString();
if(serInIndx > 0)
{
findArguments(sscpy);
}
if(argc > 0)
{
Serial.println(cmdStr);
Serial.println(cmdLen);
Serial.println(arg1Str);
Serial.println(arg1Len);
Serial.println(arg2Str);
Serial.println(arg2Len);
argc = 0;
}
printSerialString();
delay(1000);
}