Split char readings from TCP client

Hello,
In my app I read the results of industrial camera inspections.
I set the format of inspections in a camera dedicated app. For example for testing I put the inspections in that way:


So I have 6 inspections here
I1 , I2 ,I3, I4,I5
where,
I1-I5 are the results which are 0 - correct inspections or '-1' - not correct
I6 is correlation so for example 84 means 84% of correlation.

My part of code which reads that is:

while (ModeInt == 1) {
          if (client.available())   {
  
            char d = client.read();
            Serial.print(d);
            
          }
        }

Now I need to use these results to make something like:
For inspections 1-5
If inspection 1 is correct (0) - i turn on the LED (output)
If inspection 1 is incorrect (-1) I turn off the LED (output)
etc.
For inspection 6
I convert the result to analog output to use the signal later.
So i need to split that results to separate variables which I can convert to int to use them and I have no idea how I can do it.

Add each character to an array of chars as it arrives. When the whole message has been received terminate the array with a '\0' to turn it into a C string (not a String) and split it up using the strtok() function

I understand what do you mean but have no idea how i can realise it Can you help me with some functions/example?

Okay i think i changed the code so the result is in array of char but have problems to split it and use the splitted data..

if (client.available())   {
          for (int i = 0 ; i < client.available(); i++) {
            d [indx] = client.read();
            d[indx + 1] = 0;
            Serial.print(d);
          }
        }

this is what Bob was suggesting

    if (client.available())   {
        char c = client.read();
        d [indx++] = c;

        if ('\n' == c)
            Serial.print(d);
            indx = 0;
        }
    }

and here's some code that demonstrates using strtok() to extract the sub-fields


#include <stdio.h>
#include <string.h>

int
substr (
    char  *s,
    char  *toks [],
    int    maxToks)
{
    int  i = 0;
    toks [i] = strtok (s, " ");
    for (i++ ; i < maxToks && (toks [i] = strtok (NULL, " ")); i++)
        ;
    return i;
}

#define MAX_TOKS    10

int
main ()
{
    char msg [30];
    char *toks [MAX_TOKS];

    strcpy (msg, "1, 2, 3, 4");

    int n = substr((char*) msg, toks, MAX_TOKS);
    for (int i = 0; i < n; i++)
        printf ("   %s\n", toks [i]);

    return 0;
}

I used ur code and it prints nothing.
Maybe i have to use some other terminator at the end instead of '\n'?

i posted two pieces of code

the first was for reading a string and printing it when a '\n' is received.

i don't know how you tested this when you said "it prints nothing". if you're using the serial monitor, there's a setting on the bottom for selecting the line terminator. you should set it to Newline

the 2nd piece of code demonstrates the use of strtok(). it is a complete program that i tested on my laptop.

i presumed you would study it and integrate substr() into your code.

Okay update:
It prints something like this:

 byte indx = 0;
char d[10];
if (client.available())   {
         char c = client.read();
        d [indx++] = c;

        if ('\n' == c){
            Serial.print(d);
            indx = 0;}
          }

I don't know what size of d should i put at the beggining maybe that is a problem.
Tried to put it to some big value like "50" and it printed nothing then.
For now i just want to print a correct value after i will try with splitting it.

changing it from 10 to 50 made it fail? ??

it needs to be large enough to hold all the characters being input. i would set it to 80.

Changed it to 80 but it still prints nothing.
When i added Serial.print(c);
it prints the whole message:


So I receive the message from the server, there is a problem with "char d"

this code works for me


char d [80];
int  indx = 0;

void loop() {
    if (Serial.available())   {
        char c = Serial.read();
        d [indx++] = c;

        if ('\n' == c)  {
            Serial.println (d);
            indx = 0;
        }
    }
}

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

this came from the serial monitor

now is the time for all good men to come to the aid of their party

Okay i found the error, I do not know why but \n doesn't work here. I must use \x0d instead :smiley:

   case 'a':
      while (incomingByte = 'a') {
        if (client.available())   {
         char c = client.read();
        d [indx++] = c;
        if ('\x0d' == c){
            Serial.print(d);
            indx = 0;}
          }
        }


      
      break;

It prints well, now i will try to split it !

'\n' is Newline, ie 0x0A

0x0d is Carriage Return, ie '\r'

Excactly i forgot that it ends with carriage return.
But trying to split it by strtok (d , ','); it gives me :confused:
invalid conversion from 'char' to 'const char*' [-fpermissive]

That's because strtok() takes a C style string as its second parameter. See strtok - C++ Reference

Such a string is in double quotes. You are using single quotes so it is a char not a string

Okay i try to implement your code to mine:

#setup

int indx = 0;
char d[80];
char* toks;
char* s;
int maxToks = 0;
#define MAX_TOKS    10

#loop
case 'a':
      while (incomingByte = 'a') {
        if (client.available())   {
          char c = client.read();
          d [indx++] = c;
          if ('\x0d' == c) {
            Serial.print(d);
            //            char* wynik=strtok(d,',');
            int substr (
              char  *s,
              char  *toks [],
              int    maxToks);

            int  i = 0;
            toks [i] = strtok (s, ',');
            for (i++ ; i < maxToks && (toks [i] = strtok (NULL, ',')); i++);

            return i;
            char *toks [MAX_TOKS];

            strcpy (d, "1, 2, 3, 4");

            int n = substr((char*) d, toks, MAX_TOKS);
            for (int i = 0; i < n; i++)
              Serial.print (toks [i]);
            indx = 0;
          }
        }
      }



Still the same error:
invalid conversion from 'char' to 'const char*' [-fpermissive]
in line:
for (i++ ; i < maxToks && (toks [i] = strtok (NULL, ',')); i++);

Start by fixing this

while (incomingByte = 'a')
strtok (NULL, ','));

still a char instead of a string

Changed it to "," and :
exit status 1
invalid conversion from 'char*' to 'char' [-fpermissive]
Update:
Okay i changed the : char* wynik=strtok(d,",");
Before it was (d, ',');
Now i will try to sepearte it into 6 "values"

Please post your complete sketch

 switch (incomingByte) {

    case 'a':
      while (incomingByte = 'a') {
        if (client.available())   {
          char c = client.read();
          d [indx++] = c;
          while ('\x0d' == c) {
            Serial.print(d);
            char* logs = strtok(d, ",");
            char* separator = strchr(d, ",");
            if (separator != 0;)
            {
              //here i need to make for example 6 values
            }
          }
        }
      }
      break;