This code should copy the messages received from Serial to Serial3 and either Serial1 or Serial2
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()
{
Serial.begin(9600);
Serial3.begin(9600);
Serial1.begin(2400);
Serial2.begin(2400);
}
void loop()
{
receive_message(Serial);
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);
}
}
Still pending: Have you received the sketch from ChatGPT? ![]()