Identifying the last character in char to store that in array

I am trying to store a string in an array. So, basically, I'm using C to receive data from an esp32 and send data to an esp32. Because an array is three-dimensional, I'll send a data like this:

        SerialPort.print(maclist[i][0] +"|"+"\t");
        SerialPort.print(maclist[i][1] +"_"+"\t");
        SerialPort.print(maclist[i][2] +"]"+"\t"+"\n");

so this programme output will be like this
00005e0053af| 59_ 1]
and data will be continuously received. I have added a _,|,] for a reference.
While receiving this, I am getting it in char, so every char is coming as a single data.
So I'm using a while loop to store the data I'm getting in a variable. -

char a = char(SerialPort.read());

However, I want to save this character, which is receiving a single piece of data, on a variable as a string .

something like this

00005e0053af
59
1

All the references (_, |,]) I have used for this are to identify when this occurs, stop, and store the result as a string, but I am not getting how to do it. I was doing this, but it wasn't working.

This code is from the receiving end.

String maclist[20][3]; 
for (int i =0 ;i<20;++i) {
      char a = char(SerialPort.read());
      if (!(a== '|')) {
        datamacuser+=a;
        maclist[i][0] = datamacuser;
      }else {
        break;
      }
    }

Hello

First problem is that you need to use Serial.available() (instead of your for loop), to make sure there are characters to be read from the input buffer

You can have a look at Robin's updated Serial Input Basics to get an idea how to approach serial communication.

It covers the receiving end, you can write the sender that matches the receiver.

yes i have that i didn't posted here but .

Exactly What I was looking for. Thank You

consider

Output:

00005e0053af,59,1
      5  00005e0053af
     59  59
      1  1

Code:

char s [80];

// -----------------------------------------------------------------------------
#define MaxTok  10
char *toks [MaxTok];
int   vals [MaxTok];

int
tokenize (
    char       *s,
    const char *sep )
{
    unsigned n = 0;
    toks [n] = strtok (s, sep);
    vals [n] = atoi (toks [n]);

    for (n = 1; (toks [n] = strtok (NULL, sep)); n++)
        vals [n] = atoi (toks [n]);

    return n;
}

// -----------------------------------------------------------------------------
void dispToks (
    char * toks [])
{
    char s [40];
    for (unsigned n = 0; toks [n]; n++)  {
        sprintf (s, " %6d  %s", vals [n], toks [n]);
        Serial.println (s);
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    if (Serial.available ())  {
        int n = Serial. readBytesUntil ('\n', s, sizeof(s)-1);
        s [n] = 0;      // terminate string

        Serial.println (s);
        tokenize (s, ",");
        dispToks (toks);
    }
}

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.