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
}
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.
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.
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.