parity/databit/stopbits

Is it possible to change the serial port settings (parity/databit/stopbits) of the extra 3 serial ports of the Mega2560?

See:

https://www.arduino.cc/en/Serial/Begin

Thanks Larry,

I should have explained further.

I need to adjust the each element by itself and not with the baud rate.

I have found for example the changing to one stop bit I use this code. –
UCSR0C = UCSR0C | B00000100;

But as far as I can tell this only applies to the main serial port, not the additional ports.

Find the datasheet for the 2560, look up the serial ports and check which registers are used by each :slight_smile: Might be UCSR1C, UCSR2C and UCSR3C.

Mind to share why you want to change those settings on the fly?

Thanks Ster,

I’ll have a look for the data sheet in a minute, but your example’s look reasonable.

Basically I work in the field a lot testing equipment that primarily deals with the RS232 serial protocol.

Generally we used field laptops and test with Hyperterminal, but this isn’t always practical so I’m building a handheld terminal device.

OK, but I do not really see a need to just set only the parity, databits and baudrate.

If you want to change in loop(), you can also use e.g. Serial1.end() followed by Serial1.begin(oldbaudrate, newconfig) when something needs to change.

Just my view.

But that wouldn't work.

I have a system in which 8 buttons scroll baud up/down, databits up/down, parity up/down, stopbits up/down.

If for example I scrolled through from baud 9600 to 38400, then wanted to change the parity to odd, I would need to know that I'm on baud 38400 when I set serial1.begin(oldbaudrate, new config).

Therefore I need change only one parameter at a time.

Unless there is a function to call up the old (previously set) baud/parity etc that I don't know about....?

Unless there is a function to call up the old (previously set) baud/parity etc that I don't know about....?

I do believe that the key there is "previously set". YOU set the parameters. Why can't YOU remember what you set them to? Or have the Arduino remember.

I'll have a look for the data sheet in a minute,

The sooner you do that, the better informed you will be.

dstark0011:
But that wouldn't work.

I have a system in which 8 buttons scroll baud up/down, databits up/down, parity up/down, stopbits up/down.

If for example I scrolled through from baud 9600 to 38400, then wanted to change the parity to odd, I would need to know that I'm on baud 38400 when I set serial1.begin(oldbaudrate, new config).

Therefore I need change only one parameter at a time.

Unless there is a function to call up the old (previously set) baud/parity etc that I don't know about....?

Of course it will work.

You can write your code in such a way that it remembers the selected settings (as @PaulS already indicated). And if you can set something via the registers directly (parity etc), you can read those same registers back; you use that principle already in the code you showed.