UART with 9 bits

Hello,

I am trying to make my Arduino Mega a receiver of 9 bits and 1 stop bit via UART.
I read a bit and saw that the Serial.available() etc library only supports 5-8 bits.
Will appreciate help and if someone can explain me the process of UART in arduino, when it receives the data bits does it automatically first send a start-bit and then the data following a stop bit? I also want to sample the bits at the middle which is the most accurate, I will have to add a delay for 1/2 time of 1 bit after the start-bit for that right?

Thanks.

See the data sheet for using 9 bit UART mode. Libraries exist, but mostly for sending only. Eventually you can use the SERIAL_8E1 (or O1) config and recover the 9th bit from the parity? See Serial.begin()

9-bit on AVR is tricky little bit. Arduino lib does not support it due to complications (probably) with another register need for bit#8. See the datasheet for details.

Is this possible to do?

Set the port up for 8 data bits with parity checking enabled, set as either odd or even.
Read one byte at a time.
If I get any parity errors, I know that the 9th bit is the opposite of what I set.

The arduino code doesn’t check or buffer parity bits, either, so you still need to rewrite the same code that you’d rewrite to support an actual 9bit mode...

Doesn't check parity.... perhaps Arduino is just a toy.

If we had DEC USARTs we could surely do 12 bit data and have trouble making it do 8.

The parity can be checked using serialEvent().

Came across this thread: [Solved] 9 bit serial problem - Programming Questions - Arduino Forum

Just wanted to test the Serial.begin(115200, Serial_8E1); and I had compilation error -> 'Serial_8E1' was not declared in this scope.

Was wondering why this happens.

Try Serial1.begin(...). If the error persists, the Mega seems not to be supported - perhaps in a newer IDE version?

DrDiettrich:
Try Serial1.begin(...). If the error persists, the Mega seems not to be supported - perhaps in a newer IDE version?

I'm actually using an Arduino Uno right now, does it make any difference in that aspect?
p.s tried with Serial1, same error.

One very old thread that I found (might still be applicable) that you can try
https://forum.arduino.cc/index.php?topic=91377.0

"Just wanted to test the Serial.begin(115200, Serial_8E1); and I had compilation error -> 'Serial_8E1' was not declared in this scope."

That's because you aren't typing it in correctly.

It's -- Serial.begin(115200, SERIAL_8E1);
In other words, the config parameter should be "all caps".

MichaelThe2nd:
I'm actually using an Arduino Uno right now, does it make any difference in that aspect?
p.s tried with Serial1, same error.

Uno has only 1 USART. It is used for Serial, a C++ class object.

Mega has 4 USARTs, Serial, and Serial1 to Serial3.

ATmega1284 chips have 2 USARTs, there are compatible boards with those and you can roll your own.

My question remains, how can I receive 9 bits with a stop bit at the end on Arduino Uno, I have installed this library -> [Solved] 9 bit serial problem - Programming Questions - Arduino Forum (last reply on that thread).

Reply #4 was written by the guy who makes the current for the last few years bootloaders.
What he knows means more than asking over and over for a cookie.

If you don't understand the 9 bit library, you may also be incapable of writing the processing of the received values. If every character has 9 bits, it has to be stored in an int, not a char, so that none of the char handling functions can be used for buffering or further processing of the input. Also no debug information can be sent to the Serial Monitor from an Uno.

You can start receiving characters by

 int myIntChar = Serial.read();

after the usual setup and check for available characters. All further processing is up to you.

Do you already have an idea what you want to do with those received int's? Have you added a display to your Uno for showing what 9 bit data has arrived?

MichaelThe2nd:
Hello,

I am trying to make my Arduino Mega a receiver of 9 bits and 1 stop bit via UART.
I read a bit and saw that the Serial.available() etc library only supports 5-8 bits.
Will appreciate help and if someone can explain me the process of UART in arduino, when it receives the data bits does it automatically first send a start-bit and then the data following a stop bit? I also want to sample the bits at the middle which is the most accurate, I will have to add a delay for 1/2 time of 1 bit after the start-bit for that right?

Thanks.

You could forget about the UART and modify SoftwareSerial or write your own especially if the baud rate will not be high.

Sigh. I was intrigued. See if this works. I don't actually have any 9bit devices to test with, but it behaves the way I'd expect when talking to the normal 8bit things...

General strategy:
Let the user initialize the UART with Serial.begin(), and then use
this library to "disconnect" the UART from the normal Arduino function,
reset it to 9-bit mode (keeping any other settings), and then access the
RX and TX registers directly (no interrupts, no buffering beyond what is
provided by the hardware (2 bytes.)

Because "reasons", this is not a "stream" subclass, so the .print() methods
are not available. Just various varieties of .write()

write(uint16_t byte) // Write a 9bit byte.
write(const char *str) // quoted strings are written with bit9 = 0
write(const uint8_t *buf, size_t size); // 8bit arrays also wrirten with bit9=0
write(const char *buffer, size_t size)
write(const uint16_t *buffer, size_t size); // 16bit arrays write 9 bits from each word

Edit: oops; I had a bug in the .read() function that would cause it to not work right if called when there was no data available.
Attached zip updated. Or check out the latest version here: Duino-hacks/Serial9_test at master · WestfW/Duino-hacks · GitHub

Serial9_test_v0.2.zip (3.36 KB)

westfw:
Sigh. I was intrigued. See if this works. I don't actually have any 9bit devices to test with, but it behaves the way I'd expect when talking to the normal 8bit things...Edit: oops; I had a bug in the .read() function that would cause it to not work right if called when there was no data available.
Attached zip updated. Or check out the latest version here: Duino-hacks/Serial9_test at master · WestfW/Duino-hacks · GitHub

Thanks a lot for that, how do I declare pins as RX and TX in the code with your library, do I need to include the "SoftwareSerial" and do it regularry or is it different now with this lib?

This lib only works on the hardware UART ports.