Const char START_CHAR = '<'; const char END_CHAR = '\n';

hello,
I have determined the data coming from the serial port as "-----1[abcdefghijklmno] " with such data as "------2[abcdefgh hijklmno] "

"1[" start character "]" end character.

I want to take the part of this data starting with "1[" and send it to serial-1.
I also need to send the part starting with "2[" to Serial-2.
It didn't work somehow.

const char START_CHAR = '<';
const char END_CHAR   = '\n';       
const int BUF_SIZE = 32;

char message[BUF_SIZE];

// -----------------------------------------------------------------------------
void receive_message (HardwareSerial &serial)
{
    int rx_index = 0;
    char rx_char = '\0';

    Serial.println ("receive:\n");

    while (rx_char != START_CHAR)
    {
        while (!serial.available ()) {
        }
        rx_char = serial.read ();
    }

    // skip STX
    while (!serial.available ()) {
    }
    rx_char = serial.read ();

    // read until END_CHAR
    while (rx_char != END_CHAR)
    {
        if (rx_char != END_CHAR)
        {
            if (rx_index < BUF_SIZE - 1)
            {
                message[rx_index] = rx_char;
                rx_index++;
            }
        }

        while (!serial.available ()) {}
        rx_char = serial.read ();
    }
    message[rx_index] = '\0';

    Serial.print   (" receive: ");
    Serial.println (message);
}


// -----------------------------------------------------------------------------
void send_message (HardwareSerial &tx_serial)
{
    tx_serial.write (START_CHAR);

    for (int index = 0; index < strlen (message); index++) {
        tx_serial.write (message[index]);
    }
    tx_serial.write (END_CHAR);
}


// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin  (9600);
#ifdef Mega
    Serial1.begin (9600);
    Serial2.begin (9600);
#endif
}


// -----------------------------------------------------------------------------
void loop ()
{
    receive_message (Serial);

    if (strncmp (message, "1[") == 0)  {
#ifdef Mega
        send_message (Serial1);
#else
        Serial.print ("serial1 - ");
        send_message (Serial);
#endif
    }
    else if (strncmp (message, "2[") == 0) {
#ifdef Mega
        send_message (Serial2);
#else
        Serial.print ("serial2 - ");
        send_message (Serial);
#endif
    }
}

I've edited your code so that it compiles, and added some debug Serial.println()s to see what's going on. I've not fixed the final bug, which is looking for "1[" or "2[" in message after you already stripped them off in receive_message()

const char START_CHAR = '['; // Edited to agree with post text
const char END_CHAR   = '\n';       
const int BUF_SIZE = 32;

char message[BUF_SIZE];

// -----------------------------------------------------------------------------
void receive_message (HardwareSerial &serial)
{
    int rx_index = 0;
    char rx_char = '\0';

    Serial.println ("receive:\n");

    while (rx_char != START_CHAR)
    {
        while (!serial.available ()) {
        }
        rx_char = serial.read ();
        Serial.println("line 20 rx_char = " + String(rx_char)); // Edit added
    }

    // skip STX
    while (!serial.available ()) {
    }
    rx_char = serial.read ();
    Serial.println("line 27 rx_char = " + String(rx_char)); // Edit added

    // read until END_CHAR
    while (rx_char != END_CHAR)
    {
        if (rx_char != END_CHAR)
        {
            if (rx_index < BUF_SIZE - 1)
            {
                message[rx_index] = rx_char;
                rx_index++;
            }
        }

        while (!serial.available ()) {}
        rx_char = serial.read ();
        Serial.println("line 43 rx_char = " + String(rx_char)); // Edit added
    }
    message[rx_index] = '\0';

    Serial.print   (" receive: ");
    Serial.println (message);
}


// -----------------------------------------------------------------------------
void send_message (HardwareSerial &tx_serial)
{
    tx_serial.write (START_CHAR);

    for (int index = 0; index < strlen (message); index++) {
        tx_serial.write (message[index]);
    }
    tx_serial.write (END_CHAR);
}


// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin  (9600);
#ifdef Mega
    Serial1.begin (9600);
    Serial2.begin (9600);
#endif
}


// -----------------------------------------------------------------------------
void loop ()
{
    receive_message (Serial);
    Serial.println("Message received at line 79 = " + String(message)); // Edit added
    if (strncmp (message, "1[",2) == 0)  { // Added 3rd parameter
       Serial.println("line 81 strncmp (message, \"1[\",2) == 0)"); // Edit added
#ifdef Mega
        send_message (Serial1);
#else
        Serial.print ("serial1 - ");
        send_message (Serial);
#endif
    }
    else if (strncmp (message, "2[",2) == 0) { // Added 3rd parameter
      Serial.println("line 90 strncmp (message, \"2[\",2) == 0)"); // Edit added
#ifdef Mega
        send_message (Serial2);
#else
        Serial.print ("serial2 - ");
        send_message (Serial);
#endif
    } 
    else Serial.println("line 98 neither strncmp returned 0. message = \"" + String(message) + "\""); // Edit added 
}

Ran the code on an Uno R3. Serial monitor line ending is set to Newline. The serial monitor output for typing (without the quotes) "1[blah" and then "2[blah]" is as follows:

line 20 rx_char = 1
line 20 rx_char = [
line 27 rx_char = b
line 43 rx_char = l
line 43 rx_char = a
line 43 rx_char = h
line 43 rx_char = 

 receive: blah
Message received at line 79 = blah
line 98 neither strncmp returned 0. message = "blah"
receive:

line 20 rx_char = 2
line 20 rx_char = [
line 27 rx_char = b
line 43 rx_char = l
line 43 rx_char = a
line 43 rx_char = h
line 43 rx_char = 

 receive: blah
Message received at line 79 = blah
line 98 neither strncmp returned 0. message = "blah"
receive:

I think the strange 6 characters at the beginning spoil the job. I can't skip them.

I don't have any more time to look at this today. I might take another look tomorrow. In the mean time someone else might be able to help.
[Edit, what do you mean by "strange 6 characters"? I just saw 6 dashes in your post.]

yes the lines before my starting character

What is sending data to the Serial port, and do you have control over the format?
Why is the '1' or '2' before the start_char?

When sending data from the device to serial 1, I can put a start character in front of the data I send and a stop character at the end.
I found "1[" and "2[" suitable as starts.
I want to print the ones starting with 1 to serial1 and the ones starting with 2 to serial2. on mega. But it doesn't work.

Sorry, the device is connected to serial0.megamda

Why not have the message configured to be [1abcdefghijklmno] or [2abcdefghijklmno]. After the complete message is received, test for the first character, and if it is a '1' or '2' print the rest of the message to the correct serial port.

const char START_CHAR = '['; // Edited to agree with post text
const char END_CHAR   = ']';       
const int BUF_SIZE = 32;

char message[BUF_SIZE];

// -----------------------------------------------------------------------------
void receive_message (HardwareSerial &serial)
{
    int rx_index = 0;
    char rx_char = '\0';

    
    while (rx_char != START_CHAR)
    {
        while (!serial.available ()) {
        }
        rx_char = serial.read ();
       
    }

    // skip STX
    while (!serial.available ()) {
    }
    rx_char = serial.read ();
    

    // read until END_CHAR
    while (rx_char != END_CHAR)
    {
        if (rx_char != END_CHAR)
        {
            if (rx_index < BUF_SIZE - 1)
            {
                message[rx_index] = rx_char;
                rx_index++;
            }
        }

        while (!serial.available ()) {}
        rx_char = serial.read ();
       
    }
    message[rx_index] = '\0';

    
}


// -----------------------------------------------------------------------------
void send_message (HardwareSerial &tx_serial)
{
    tx_serial.write (START_CHAR);

    for (int index = 0; index < strlen (message); index++) {
        tx_serial.write (message[index]);
    }
    tx_serial.write (END_CHAR);
}


// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin  (9600);
#ifdef Mega
    Serial1.begin (2400);
    Serial2.begin (2400);
#endif
}


// -----------------------------------------------------------------------------
void loop ()
{
    receive_message (Serial);
    
    if (strncmp (message, "1[",2) == 0) 
     {
       
#ifdef Mega
        send_message (Serial1);

#endif
    }
    else if (strncmp (message, "2[",2) == 0) 
    {
      
#ifdef Mega
        send_message (Serial2);

#endif
    } 
  
}

stubbornly not working

When the serial port indicator needs to be inside the message which is received between the start and end chars. The message should look like [1abcdefg] or [2abcdefg].

Then test for the index at message[0].

Try this modification to loop() of your code.

void loop ()
{
    receive_message (Serial);
    
   
    //if (strncmp (message, "1[",2) == 0) 
    if(message[0] == '1')
     {
       Serial.println("message sent on Serial1");
#ifdef Mega
        send_message (Serial1);

#endif
    }
    //else if (strncmp (message, "2[",2) == 0) 
     else if(message[0] == '2')
    {
      Serial.println("message sent on Serial2");
#ifdef Mega
        send_message (Serial2);

#endif
    } 
  
}

While such code can be made more optimal, it's not very difficult to write a state machine to decode formal protocols ➜ you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

this could look like this (sending everything to Serial in this example but target port is identified)

enum {WAIT_FOR_DESTINATION, WAIT_FOR_START_MARKER, SENDING} state = WAIT_FOR_DESTINATION;
const char * validDestinations = "12";
const char startMarker = '[';
const char endMarker = ']';
int destination;

void checkInput() {
  if (Serial.available()) {
    char r = Serial.read();
    switch (state) {
      case WAIT_FOR_DESTINATION:
        if (strchr(validDestinations, r) != nullptr) { // check if the input is par of the valid destinations
          destination = r - '0';
          state = WAIT_FOR_START_MARKER;
        }
        break;

      case WAIT_FOR_START_MARKER:
        if (r == startMarker) {
          Serial.print("Sending to Serial"); Serial.print(destination);
          Serial.print("\t{");
          state = SENDING;
        } else {
          state = WAIT_FOR_DESTINATION; // ill formed entry
        }
        break;

      case SENDING:
        if (r == endMarker) {
          Serial.println("}");
          state = WAIT_FOR_DESTINATION; // entry finished
        } else {
          Serial.write(r);
        }
    }
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println("READY");
}

void loop() {
  checkInput();
}

try typing in

hello1hello[2those are false entries but this one is ok 1[this text will go to Serial 1]more crap2[this text will go to Serial 2]

if all goes well, the invalid stuff is discarded and you should see

Sending to Serial1	{this text will go to Serial 1}
Sending to Serial2	{this text will go to Serial 2}

I think the code is pretty self explanatory but if you have any question, feel free to ask.
Also this is totally non blocking so you deal with the data when it's there and you can do other stuff in the loop (as long as it's non blocking and does not take too long)

It's a bit beyond me. I'm confused. The part about sending to serial1 and serial2 is confusing.

 case SENDING:
        if (r == endMarker) {
          Serial.println("}");
          state = WAIT_FOR_DESTINATION; // entry finished
        } else {
          Serial.write(r);
        }
    }
  }
}

void setup() {
  Serial.begin(9600);
    Serial.println("READY");
    Serial1.begin(9600);
  Serial2.begin(9600);
}

void loop() {
  checkInput();
}

When I want to add serial3, I will continue with
const char * validDestinations = "123";

This is just text at this point - you would use the value of destination to decide where to send the bye.

      case SENDING:
        if (r == endMarker) {
          Serial.println("}");
          state = WAIT_FOR_DESTINATION; // entry finished
        } else {
          switch(destination) {
            case 1: Serial1.write(r ); break;
            case 2: Serial2.write(r ); break;
            case 3: Serial3.write(r ); break;
            default: break;
          }
        }

How do I get this into void loop()?

void loop() {
  if (r == endMarker) {
          Serial.println("}");
          state = WAIT_FOR_DESTINATION; // entry finished
        } 
          switch(destination) {
            case 1: Serial1.write(r); break;
            case 2: Serial2.write(r); break;
            case 3: Serial3.write(r); break;
            default: break;
   
  checkInput();
}}