How to read a serial string from a certain start character?

Hello, I want to read a certain string from the serial buffer from a certain start character. I am currently using readStringUntil() for the first half of the message but I don't know how to read the second half. I do have a termination character on the first half of the message.

If you know how to use findUntil(), it's strange that you can't find readStringUntil() just slightly farther down the page. https://www.arduino.cc/en/Reference/Stream

Sorry that is a typo I meant readStringUntil()

but I don't know how to read the second half.

Read one character at a time until the end character arrives. Knowing what that happens is the key.

If you want to read all available data, and any that becomes available before a timeout happens, use readString().

Even better: Throw the crutch away and use char arrays.

econjack: Even better: Throw the crutch away and use char arrays.

We were going to get there...

Have a look at serial input basics.

It has example with start- and endmarker that might be applicable. And once you're stuck, don't forget to post a sample data and what needs to be extracted.

Neowaylight: Hello, I want to read a certain string from the serial buffer from a certain start character. I am currently using readStringUntil() for the first half of the message but I don't know how to read the second half. I do have a termination character on the first half of the message.

You can read serial chars as available (1 at a time) until you get the start char which you save and then the rest which you also save.

Like this, untested just to show how that would look

const char startChar = 'X', endChar = 0;
char ch;
char *bufPtr = buffer; // address of the buffer char array stored in char pointer bufPtr.
byte bufLen = 80;
char buffer[ 80 ];

loop()
{
  if ( Serial.available())
  {
    ch = Serial.read();
    if ( bufPtr == buffer ) // still haven't found startChar in serial
    {
      if ( ch == startChar )
      {
        *bufPtr++ = ch; // puts ch into memory at the current address in bufPtr then increments bufPtr
      }
    }
    else
    {
      if (( ch == endChar ) || ( bufPtr - buffer = bufLen - 1 ))
       {
        *bufPtr = 0;
        bufPtr = buffer; // reset the buffer
        Serial.println( buffer );
      }
      else
      {
        *bufPtr++ = ch; // puts ch into memory at the current address in bufPtr then increments bufPtr
      }
    }
  }
}

Note that this code can run other tasks, like blink a status led even as it runs.

Is this what you meant to write:

_ if (( ch == endChar ) || ( bufPtr - buffer = bufLen - 1 ))_

Did you mean to use the assignment operator?

Also, wouldn't it be more clear and better document your intent if you used the null termination character?

const char startChar = 'X', endChar = '\0'; // *bufPtr = '\0';

Code previously posted by somebody.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

econjack: Is this what you meant to write:

_ if (( ch == endChar ) || ( bufPtr - buffer = bufLen - 1 ))_

Did you mean to use the assignment operator?

Also, wouldn't it be more clear and better document your intent if you used the null termination character?

const char startChar = 'X', endChar = '\0'; // *bufPtr = '\0';

No, that should be ==. I should have made a sketch while I was at it

How to break this to you? The NULL char is zero whether it's 0 or '\0'. All the \ is for is that the next digit(s) determine what numeric value the experssion '###' evaluates to.

sterretje:
Have a look at serial input basics.

It has example with start- and endmarker that might be applicable. And once you’re stuck, don’t forget to post a sample data and what needs to be extracted.

Ok got it thanks.