char array splitting with strtok

Hello!

I'm trying to split a char array at commas with strtok. When I run my code and enter 12,34 in serial monitor, then I get just 1234 (or the same vertically when I use Serial.println) but I want to get 12 and 34 separately. Am I printing the 12 and 34 just side-by-side? But adding lines with additional println statements also doesn't separate the two groups...

Does anybody know where is the mistake in the code? I would like to split the array and define two int-variables with the parts I get this way (using atoi for converting the subarrays to int).

strtokArduF.ino (591 Bytes)

Actually I wanted the code to be directly visible here... Sorry for that mess

char data[10];
//char * d;

void setup() {
 Serial.begin(9600);
 Serial.setTimeout(50);
}

void loop() {

 while (Serial.available()==0) {             //Wait for user input
 
 }

 int availableBytes = Serial.available();

 for(int i=0; i<availableBytes; i++)
 {
   data[i] = Serial.read();
   data[i+1] = '\0'; // Append a null
 }

 //Serial.print(data);

 char* d = strtok(data, ","); 

   while (d != NULL) { 
//        Serial.println(" ");
       Serial.print(d); 
//        Serial.println(" ");
       d = strtok(NULL, ","); 
   } 

}

the following code works for me

void
myStrtok (void)
{
    char data [] = "12,34";
    Serial.println (data);

    char* d = strtok(data, ",");
    while (d != NULL) {
        Serial.println (d);
        d = strtok(NULL, ",");
    }
}

it produces

12,34
12
34

are you correctly read the string from the serial interface w/o checking if available for each character? it takes ~1ms for each character to be transmitted.

i would read a string, checking available() for each character, and waiting from some terminator character

Here is another version of code that works for me. You must keep the line ending tab of Serial Moniotr (Fig-1) at Newline option.

char data[10];
int i = 0;
//char * d;

void setup()
{
  Serial.begin(9600);
  // Serial.setTimeout(50);
}

void loop()
{
  byte n = Serial.available();
  if (n != 0)
  {
    char x = Serial.read();
    if (x != '\n')//newline character
    {
      data[i] = x;
      i++;
    }
    else
    {
      data[i] = '\0'; //null character
      Serial.println(data); //shows: 12,34
      data[2]  = '\0'; //nill chaaracter
      int x1 = atoi(data);
      Serial.println(x1); //shows: 12
      //----------------------------
      memset(data, 0x30, 3);  //replace by 0s
      int x2 = atoi(data);
      Serial.println(x2);  //shows: 34
      //-----------------------------
      i = 0;
    }
  }
}

sm110.png
Figure-1:

SerialMonitor.png
Figure-2:

sm110.png

SerialMonitor.png

It seems to work now. The problem was related to building the char array from the input from serial monitor. When I defined the char array directly like in gcjr's example then the code worked as expected, but when the array was formed from the input from the serial monitor then the result was different.
When I just tried to echo the char array back to serial monitor then it was written as a singe word when I was using Serial.print(), but with Serial.println() it was printed vertically, i.e. every char on it's own line. It was not like an array but a row of separate chars.
But when I created the array like it was done in Golam's code then it worked properly.

But unfortunately (regarding the memory usage) I had to introduce the strings anyway because my application will receive the input numbers from a Java application. In Java, they are taken from a JTextField and sent over a serial connection to Arduino. But after receiving the string in Arduino I convert it into a char array and still use strtok for picking values separated by commas, and everything works.

But thanks for your examples and advice, it helped me to get to the destination, step by step.