Arduino Nano receiving and sending at different baud rates

I need to read an serial ascii data packet that terminates with a CR at 9600 baud and transmit a slightly modified version of that same ascii packet at 19200 baud. Then repeat indefinitely.

With a Nano, will changing the receive and transmit packet speeds work?

byte dumpdata = 0;
String Data = "";

void setup() {
  delay(2000);
}

void loop() {

  Serial.begin(9600);
  while (Serial.available() >= 1) { dumpdata = Serial.read(); }  // clear Serial input buffer
  Serial.readStringUntil('\n');                                  // read and discard until see CR as beginning of complete packet
  while (Serial.available() < 1) { ; }
  Data = Serial.readStringUntil('\n');  // read packet

  Serial.begin(19200);  // send data packet at higher baud rate
  Serial.println(Data);
  Serial.flush();  // wait for transmission to complete
}

You should use Serial.end() before you do another begin

It might. Obviously, if you’re receiving the next packet while the previous is transmitting, then no, it won’t work. But if there’s a long enough delay between the incoming packets for both the outgoing previous packet, and any turn-around time for Serial.begin, it would be worth a try.

As @jim-p points out, you should also end both the 9600 and 19200 instances; not sure if “flush()” is implicit in an “end()”, so you’ll have to do some reading.

  • Do you need this to happen using the same RX/TX pins ?

Use software serial for rx (on spare pin) and hardware serial for tx.

The nano only has one set of RX/TX pins. I want to RX at 9600 baud and TX at 19200.

Does this code look better:

byte dumpdata = 0;
String Data = "";

void setup() {
  delay(2000);
}

void loop() {

  Serial.begin(9600);
  while (Serial.available() >= 1) { dumpdata = Serial.read(); }  // clear Serial input buffer
  Serial.readStringUntil('\n');                                  // read and discard until see CR as beginning of complete packet
  while (Serial.available() < 1) { ; }
  Data = Serial.readStringUntil('\n');  // read packet
  Serial.end();

  Serial.begin(19200);  // send data packet at higher baud rate
  Serial.println(Data);
  Serial.flush();  // wait for transmission to complete
  Serial.end();
}
  • Is it guaranteed there is no RX while you will be transmitting ?

No. The RX data is a continuous process. So there is 9600 baud data coming in while I am trying to TX at 19200 baud.

So you have a device that transmits at one baud rate and receives at a different baud rate?

Quite unusual

The problem I am solving is that an older version of a device transmits CSV data at 9600 baud, while the newer version of a different device it talks to now expects to get data at 19200 baud. So I need to read the CSV data at 9600 baud and retransmit it to the other device at 19200 baud. I'm trying to use a single nano to do this. I know a mega with multiple serial ports can do this, as I built a prototype using one that works fine. But I want to use a nano due to space issues. And it only has a single serial port.

  • Get an Arduino that has two hardware serial ports.

Then use Software Serial for one of the devices.

https://docs.arduino.cc/learn/built-in-libraries/software-serial/

Esp32 board has "nano" size and 3 serial ports.

Great ideas. Didn't know about the Esp32 board. Will give it and SW Serial a try.

Thanks!

That will work only if there is NO error checking and recovery being done!

Another option, if you need the same footprint, would be the Nano Every.

The big problem with ESP32 is that the I/Os are 3.3V and not 5V so you will need a voltage translator along wit it

I'm going to try SW serial to receive the data first. That also solves another problem in that I can then use the HW RX port to receive programming over the USB port without having to disconnect my RS232 TTL converter from the the nano board.

For a UART, there is usually no problem with connecting many receivers to a single transmitter, however, connecting many transmitters to a single receiver can cause problems.

Any USB communications is both sending and receiving so messages can be acknowledged and errors can be corrected. It needs both to work!