Sending the data coming to the serial port to serial 3

void loop()
{
send_message(Serial3);
// put your main code here, to run it over and over:
{
if (strncmp(message, "1", 1) == 0)
{
send_message(Serial1);
}
else if (strncmp(message, "2", 1) == 0)
{
send_message(Serial2);
} } }

Apart from these conditions, I want to send all the data in Serial to Serial 3 without any rules.
I couldn't do it. const char START_CHAR = '\x02';
const char END_CHAR = '\x0A';

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 = 2; 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);
Serial3.begin(9600);

Serial1.begin(2400);
Serial2.begin(2400);

}

void loop()
{
send_message(Serial3);
// put your main code here, to run repeatedly:
{
if (strncmp(message, "1", 1) == 0)
{
send_message(Serial1);
}
else if (strncmp(message, "2", 1) == 0)
{
send_message(Serial2);
} } }

``

First you need to post your code so that is readable.
In the IDE click on Edit, then Copy for Forum, that will copy your code for posting on the forum. Then come back here and just do a simple paste.

Hi @bykz1351 ,

I did some formatting for you:

Post #1 reformatted

void loop()
{
  send_message(Serial3);
  // put your main code here, to run it over and over:
  {
    if (strncmp(message, "1", 1) == 0)
    {
      send_message(Serial1);
    }
    else if (strncmp(message, "2", 1) == 0)
    {
      send_message(Serial2);
    }
  }
}

Apart from these conditions, I want to send all the data in Serial to Serial 3 without any rules. I couldn't do it.

const char START_CHAR = '\x02';
const char END_CHAR = '\x0A';

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 = 2; 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);
  Serial3.begin(9600);

  Serial1.begin(2400);
  Serial2.begin(2400);

}

void loop()
{
  send_message(Serial3);
  // put your main code here, to run repeatedly:
  {
    if (strncmp(message, "1", 1) == 0)
    {
      send_message(Serial1);
    }
    else if (strncmp(message, "2", 1) == 0)
    {
      send_message(Serial2);
    }
  }
}

Did you get this sketch from a guy called "ChatGPT"?

const char START_CHAR = '\x02';
const char END_CHAR = '\x0A';

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 = 2; 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);Serial3.begin(9600);

  Serial1.begin(2400);
  Serial2.begin(2400);
  
}

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

Sending the data coming to the serial port to serial 3.

I want to send a one-to-one serial to serial 3.

(Serial.available() > 0);{
Serial3.write(Serial.read());
}

if and else after if did not work. neither before nor after
(Serial.available() > 0);{
Serial3.write(Serial.read());
}

You may try this

if (Serial.available() > 0) {
  char c = Serial.read();
  Serial3.write(c);
}
void loop()


if (Serial.available() > 0) {
  char c = Serial.read();
  Serial3.write(c);
}

{
  
 
  // put your main code here, to run repeatedly:
 {
  receive_message(Serial);
  
  if (strncmp(message, "1", 1) == 0)
  {
    send_message(Serial1);
  }
  else if (strncmp(message, "2", 1) == 0)
  {
    send_message(Serial2);
  
  }}} 
    
 
 

I wonder if it was because of the other ifs?

Does your Arduino actually have 3 serial ports?

That's not working because you receive data from Serial at different places in your sketch!

Hi @jim-p ,

I assume it's a MEGA ...

Regards
ec2021

const char START_CHAR = '\x02';
const char END_CHAR = '\x0A';

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 = 2; 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);Serial3.begin(9600);

Serial1.begin(2400);
Serial2.begin(2400);

}

void loop()

// put your main code here, to run repeatedly:
{
receive_message(Serial);

if (strncmp(message, "1", 1) == 0)
{
send_message(Serial1);
}
else if (strncmp(message, "2", 1) == 0)
{
send_message(Serial2);

}}

I assume nothing.

mega

Still wrong!
See post #2

I did what you said, copy and paste.

I need to send all the data coming to serial in the loop.

Did you get the code from ChatGPT?

You have two places where data from Serial are collected:

  • In the "if (Serial.available()>0)" clause and
  • in "receive_message(Serial);"

If you want all data written to Serial3 you could do it in the if clauses:

 if (strncmp(message, "1", 1) == 0)
  {
    send_message(Serial1);
    send_message(Serial3);
  }
  else if (strncmp(message, "2", 1) == 0)
  {
    send_message(Serial2);
    send_message(Serial3);
 }

P.S.: Just to make sure: Remove the "while (Serial.available()>0){...}"! It does not work in this context!