Strncmp I'm having trouble

STUDENT STUDENT
Sending this line to serial-1.
other 4TEACHER-3
I am trying to send the line to serial-2.
No matter what I tried, it didn't work. When I sent these characters to ext String to Hex Code Converter x
I think the value is space, hex value is 20.
If I send it to serial -2, the value of 4 at the end is 34 in hex.
add it to the code
void loop()
{
// put your main code here, to run repeatedly:

receive_message(Serial);
if (strncmp(message, "20", 2) == 0) {
send_message(Serial1); }
else if (strncmp(message, "34", 2) == 0) {
send_message(Serial2); }}

it doesn't work at all`const char START_CHAR = '\x0F';
const char END_CHAR = '\x03';
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(){
// put your setup code here, to run once:

Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);}
void loop(){
// put your main code here, to run repeatedly:
receive_message(Serial);
if (strncmp(message, "20", 2) == 0){
send_message(Serial1); }
else if (strncmp(message, "34", 2) == 0) {
send_message(Serial2); }}


`

have a read of how-to-get-the-best-out-of-this-forum

in particular the code in post 1 is unreadable
select Edit>Copy for Forum then select < CODE/ > and paste the code where it says “type or paste code here”

also avoid posting images of screen shots if possible - they waste space, are difficult to read and cannot be copied from

EDIT: what microcontroller are you using ? e.g. Mega, ESP32, RP2040??

this is search for the ASCII chars '3' and '4', not a hex value of 4

your code works for me

  • tested on Uno
  • instead of sending to Serial1 or 2 i prefaces the print with "serial1" or "serial2"
  • defined the missing START_CHAR as '<'
  • changed the END_CHAR to linefeed, '\n'
receive:

 receive: 20 greg
serial1 - <20 greg
receive:

 receive: 34 jean
serial2 - <34 jean
receive:

code

const char START_CHAR = '<';
const char END_CHAR   = '\n';       // '\x03';
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, "20", 2) == 0)  {
#ifdef Mega
        send_message (Serial1);
#else
        Serial.print ("serial1 - ");
        send_message (Serial);
#endif
    }
    else if (strncmp (message, "34", 2) == 0) {
#ifdef Mega
        send_message (Serial2);
#else
        Serial.print ("serial2 - ");
        send_message (Serial);
#endif
    }
}

your post is unreadable

Please correct your post and add code tags around your code.

There is a small pencil image below your existing posts.

  • click on this pencil ➜ that will let you edit your post.
  • Select the part of the text that corresponds to the code
  • Click on the <code/> icon in the toolbar to indicate that it is code
  • click image Save Edit

(Also make sure to properly indent the code in the IDE before copying and pasting it here. This can be done by pressing ctrlT on a PC or cmdT on a Mac)

look the code below over, i think it's a simpler approach

serial1 - <20 greg
serial2 - <34 jean
invlalid - <55 bob
discard - 20 greg

code

const char START_CHAR = '<';
const char END_CHAR   = '\n';       // '\x03';

const int BUF_SIZE    = 32;
char message[BUF_SIZE];

// -----------------------------------------------------------------------------
void loop ()
{
    if (! Serial.available ())
        return;

    int n = Serial.readBytesUntil (END_CHAR, message, sizeof(message)-1);
    message [n] = '\0';

    if ('<' != message [0]) {
        Serial.print ("discard - ");
        Serial.println (message);
        return;
    }

    if (strncmp (message, "<20", 3) == 0)  {
#ifdef Mega
        send_message (Serial1);
#else
        Serial.print ("serial1 - ");
        Serial.println (message);
#endif
    }
    else if (strncmp (message, "<34", 3) == 0) {
#ifdef Mega
        send_message (Serial2);
#else
        Serial.print ("serial2 - ");
        Serial.println (message);
#endif
    }
    else {
        Serial.print ("invlalid - ");
        Serial.println (message);
    }
}

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

if the received message ends with END_CHAR but doesn't start with START_CHAR, you could search for the START_CHAR in it and process the message from that point instead of discarding it

you could also instead of just calling readBytesUntil(), check if the char that is available is the START_CHAR and ignore it if not

serial2 - 34 jean
ignore - k
ignore - h
ignore - k
serial1 - 20 greg
ignore - k
ignore - j
ignore - n
invlalid - 55 bob
const char START_CHAR = '<';
const char END_CHAR   = '\n';       // '\x03';

const int BUF_SIZE    = 32;
char message[BUF_SIZE];

// -----------------------------------------------------------------------------
void chkSerial ()
{
    if (! Serial.available ())
        return;

    char c = Serial.read ();
    if ('<' != c)  {
        Serial.print ("ignore - ");
        Serial.println (c);
        return;
    }

    int n = Serial.readBytesUntil (END_CHAR, message, sizeof(message)-1);
    message [n] = '\0';

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

// -----------------------------------------------------------------------------
void loop ()
{
    chkSerial ();
}

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

Thank you, I tried. It didn't work. I guess I can't analyze the starting characters well. I watched it from the serial port with Arduino as shown in the picture, but it shows square-shaped characters. When I scan it and paste it into Google, strange characters appear.

please don't make a mess of our forum...

Please correct your first post and add code tags around your code.

There is a small pencil image below your existing posts.

  • click on this pencil ➜ that will let you edit your post.
  • Select the part of the text that corresponds to the code
  • Click on the <code/> icon in the toolbar to indicate that it is code
  • click image Save Edit

(Also make sure to properly indent the code in the IDE before copying and pasting it here. This can be done by pressing ctrlT on a PC or cmdT on a Mac)

there's no point in doing anything complciated if you can't successfully output to the Serial monitor.

i get the following output using the code posted in the image when entered thru the serial monitor

ready
hello world
how now brown cow
void setup ()
{
    Serial.begin    (9600);
    Serial.println ("ready");
}
void loop ()
{
    if (Serial.available ()) {
        int c = Serial.read ();
        Serial.write ((char) c);
    }
}

The result of the last serial port reading you sent

  I'm confused about how to separate these characters and send them to series 1 and series 2.

i don't understand the screen shot you're posting, i don't know what those boxes are at the bottom of the screen.

it looks like the Arduino IDE. does the IDE have connections to Serial1, 2, 3?

There is a space, the next one has 4, the next one has .
others are meaningless squares

i'm not sure what your showing me.

that program should send back to Serial (not Serial1, 2, 3) whatever it receives thru the serial monitor on Serial

The lines coming from the program. The beginning of each line. The data I sent. The last one is the stop information sent by the program.

For testing, I connected the program to Arduino, I will analyze the incoming data and send it to serial 1 and 2.

doubt i can help because i don't understand.
the Arduino has a single Serial interface, and as far as i know the IDE has just a single serial interfaces as well

the code i posted showed how message IDs can be compared and with a prefex identified on a single output (Serial)

There are 3 megas, one sends and the other 2 receive.

According to the signals sent by the program, I send information to the other Arduinos in series 1 and series 2 and reflect it on the screen from there. The problem is parsing the data sent by the program.