Converting 9600 Baud to 4800 Baud

setup () {
   Serial1.begin (9600);
   Serial2.begin (4800);
}

loop () {
   if (Serial1.available())
       Serial2.write(Serial1.read());
}

I have TTL digital signal that is supposed to be sent through a transmitter which operates at 4800 baud. Will this program work to successfully convert the 9600 baud signal to 4800 baud?

Were I in your shoes, I'd use while instead of if (and braces)...

loop () {
   while (Serial1.available())
   {
       Serial2.write(Serial1.read());
   }
}

What do you expect to happen when data arrives too fast?

Depending on the board, which you didn't mention, you may have an output buffer size of 64 bytes. Clearly you are writing at half the reading speed so you would easily overflow the output buffer, after which the code would hang.

You can make your own internal buffer (up to available RAM) if the data arrives in bursts. If it doesn't arrive in bursts this isn't going to work.

I'm planning on using an ATmega328 or an atTiny85

jeff77789:
I'm planning on using an ATmega328 or an atTiny85

Neither of those two parts has 2 hardware UARTs,
so that means that one of those ports will have to use SoftSerial
if you are sticking with Arduino supplied libraries for the serial support.

Even if you select a part with two hardware serial ports, the real challenge is as mentioned earlier,
what to do when the data comes in too fast for too long and the buffers overflow.

If the 9600 baud transmitter supports flow control, then you could add some code to
buffer things up and flow control the transmitter, but then the loop isn't nearly as
clean and simple.

--- bill

I'm reading this topic to learn stuff ...

Am I correct in my understanding that you are effectively saying the problem is how to know if the output buffer is full before you add another char to it?

Also, am I correct to assume that the 64byte receive buffer is protected by handshaking between the Arduino and the PC so that the PC's transmission is halted if the receive buffer is full.

Looking at the -328 datasheet it seems there is a bit that marks when the data register is empty. But, of course, this is not much use if there is only one USART.

...R

Robin2:
Also, am I correct to assume that the 64byte receive buffer is protected by handshaking between the Arduino and the PC so that the PC's transmission is halted if the receive buffer is full.

There is no handshaking. I'm not sure exactly how you would work out how full the (output) buffer is, either.

You can make your own buffer (eg. 500 bytes) and pull out a byte at a time and send it to the serial output.

Robin2:
Also, am I correct to assume that the 64byte receive buffer is protected by handshaking between the Arduino and the PC so that the PC's transmission is halted if the receive buffer is full.

That is incorrect for most "Arduino" boards.
There is no flow control between the Arduino and the PC on the boards
that use a chip like FTDI and now Atmel chip to perform the virtual com port to serial function
over the USB.
While USB does have flow control built in, there is no flow control on the output
side of the FTDI or Atmel chip that sits between the USB and the AVR.
The chip just keeps blasting out the serial data.
While the FTDI chip does support h/w flow control on the serial output,
the pin is not hooked up to the AVR.

Boards like the Teensy which use native USB inside the AVR do have flow control
since the AVR is processing the USB.
When using that board, the PC would be flow controlled properly.

But in reality, there is no actual baud rate when using a virtual com port
over USB to something like an AVR with native USB. The baud rate is meaningless since there
is no actual serial port involved.

--- bill

Thank you both.

As I said I have been reading this topic to learn from it.

…R