# Split char readings from TCP client

**URL:** <https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260>\
**Category:** Programming\
**Created:** [May 18, 2021, 9:09am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260 "2021-05-18T09:09:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zawislak](https://avatars.discourse-cdn.com/v4/letter/z/34f0e0/32.png) [@Zawislak](https://forum.arduino.cc/u/Zawislak)\
**Post date:** [May 18, 2021, 9:09am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/1 "2021-05-18T09:09:25Z")

</div>

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:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/f/9/6/f96392cb28a11d071f6ba760be130a3c7aefa4cc.png)  
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:

```cpp
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.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 18, 2021, 9:12am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/2 "2021-05-18T09:12:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zawislak](https://avatars.discourse-cdn.com/v4/letter/z/34f0e0/32.png) [@Zawislak](https://forum.arduino.cc/u/Zawislak)\
**Post date:** [May 18, 2021, 9:16am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/3 "2021-05-18T09:16:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zawislak](https://avatars.discourse-cdn.com/v4/letter/z/34f0e0/32.png) [@Zawislak](https://forum.arduino.cc/u/Zawislak)\
**Post date:** [May 18, 2021, 10:16am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/4 "2021-05-18T10:16:20Z")

</div>

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..

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

```

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [May 18, 2021, 10:38am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/5 "2021-05-18T10:38:09Z")

</div>

this is what Bob was suggesting

```cpp
    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

```cpp

#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;
}

```

---

<div class="post-metadata">

**Author:** ![Zawislak](https://avatars.discourse-cdn.com/v4/letter/z/34f0e0/32.png) [@Zawislak](https://forum.arduino.cc/u/Zawislak)\
**Post date:** [May 18, 2021, 11:09am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/6 "2021-05-18T11:09:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [May 18, 2021, 11:17am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/7 "2021-05-18T11:17:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zawislak](https://avatars.discourse-cdn.com/v4/letter/z/34f0e0/32.png) [@Zawislak](https://forum.arduino.cc/u/Zawislak)\
**Post date:** [May 18, 2021, 11:20am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/8 "2021-05-18T11:20:33Z")

</div>

Okay update:  
It prints something like this:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/9/c/a/9caa29a2db9b578b2eb8b82fcad8ec88d5338063.png)

```cpp
 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.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [May 18, 2021, 11:24am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/9 "2021-05-18T11:24:05Z")

</div>

> [@Zawislak](#):
>
> Tried to put it to some big value like "50" and it printed nothing then

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.

---

<div class="post-metadata">

**Author:** ![Zawislak](https://avatars.discourse-cdn.com/v4/letter/z/34f0e0/32.png) [@Zawislak](https://forum.arduino.cc/u/Zawislak)\
**Post date:** [May 18, 2021, 11:38am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/10 "2021-05-18T11:38:43Z")

</div>

> [@Zawislak](#):
>
> ```arduino
> byte indx = 0;
> char d[10];
> 
> ```

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

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/4/4/1/4418d0cf3f26200b43c6672651048db4aa9dd59e.png)  
So I receive the message from the server, there is a problem with "char d"

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [May 18, 2021, 11:44am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/11 "2021-05-18T11:44:09Z")

</div>

this code works for me

```cpp

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

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

```

---

<div class="post-metadata">

**Author:** ![Zawislak](https://avatars.discourse-cdn.com/v4/letter/z/34f0e0/32.png) [@Zawislak](https://forum.arduino.cc/u/Zawislak)\
**Post date:** [May 18, 2021, 11:59am UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/12 "2021-05-18T11:59:00Z")

</div>

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

```cpp
   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 !

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 18, 2021, 12:04pm UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/13 "2021-05-18T12:04:05Z")

</div>

> [@Zawislak](#):
>
> I do not know why but \n doesn't work here. I must use \x0d instead

'\n' is Newline, ie 0x0A

0x0d is Carriage Return, ie '\r'

---

<div class="post-metadata">

**Author:** ![Zawislak](https://avatars.discourse-cdn.com/v4/letter/z/34f0e0/32.png) [@Zawislak](https://forum.arduino.cc/u/Zawislak)\
**Post date:** [May 18, 2021, 12:06pm UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/14 "2021-05-18T12:06:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 18, 2021, 12:16pm UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/15 "2021-05-18T12:16:41Z")

</div>

That's because strtok() takes a C style string as its second parameter. See [https://www.cplusplus.com/reference/cstring/strtok/](https://www.cplusplus.com/reference/cstring/strtok/)

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

---

<div class="post-metadata">

**Author:** ![Zawislak](https://avatars.discourse-cdn.com/v4/letter/z/34f0e0/32.png) [@Zawislak](https://forum.arduino.cc/u/Zawislak)\
**Post date:** [May 18, 2021, 12:27pm UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/16 "2021-05-18T12:27:39Z")

</div>

Okay i try to implement your code to mine:

```cpp
#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++);

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 18, 2021, 12:50pm UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/17 "2021-05-18T12:50:31Z")

</div>

Start by fixing this

```cpp
while (incomingByte = 'a')

```

```cpp
strtok (NULL, ','));

```

still a char instead of a string

---

<div class="post-metadata">

**Author:** ![Zawislak](https://avatars.discourse-cdn.com/v4/letter/z/34f0e0/32.png) [@Zawislak](https://forum.arduino.cc/u/Zawislak)\
**Post date:** [May 18, 2021, 2:01pm UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/18 "2021-05-18T14:01:54Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 18, 2021, 2:10pm UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/19 "2021-05-18T14:10:00Z")

</div>

Please post your complete sketch

---

<div class="post-metadata">

**Author:** ![Zawislak](https://avatars.discourse-cdn.com/v4/letter/z/34f0e0/32.png) [@Zawislak](https://forum.arduino.cc/u/Zawislak)\
**Post date:** [May 18, 2021, 2:16pm UTC](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260/20 "2021-05-18T14:16:55Z")

</div>

```cpp
 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;

```

[Next page](https://forum.arduino.cc/t/split-char-readings-from-tcp-client/863260.md?page=2)
