Mods to HardwareSerial to handle 9-bit data

Wow Nick! You have been on a role lately with all of the things you have been doing! I find myself referring to your posts and your blog quite a lot lately.

I am curious would it be difficult to change to 7-bit data? In some automotive applications I am lead to believe they use 7-bit but, don't quote me on that.

When I was doing that change I noticed there didn't seem to be any provision for different bit lengths, let alone 9 which was the trickiest.

Changing to other bit lengths should be trivial.

However for 7-bits you can "fudge" that by setting the high-order bit of the data you are sending. Since the high-order bit is sent last, and a stop bit is a 1, if you make sure that the high-order bit is a 1, then 8 bits of data with the high-order bit set, would look like 7 bits followed by a stop bit (well, 2 stop bits).

For receiving, just mask out the high-order bit (eg. "and" it with 0x7F).

Ok, masking bits would be a lot simpler than changing library definitions. Thanks Nick!

Nick,
could you check the download url? I get looped back to this page.

Oops, the loopback was so fast, I didn't realize that the file had downloaded. thank you so much Nick.

As allways great inspiring job Nick,
thanks

cyclegadget:
I am curious would it be difficult to change to 7-bit data? In some automotive applications I am lead to believe they use 7-bit but, don't quote me on that.

If you want 5 - 8 bits of serial data, have you considered this library?

It replaces the existing HardwareSerial and it's quite a bit faster and has configurable buffer sizes (transmit and receive).

http://arduino.cc/forum/index.php/topic,85207.0.html

Iain

I have downloaded that library. I forgot some of the things it is capable of. Thanks!

Make sure you have the latest version. I'm not sure the variable bit stuff was in the early versions.

Iain

this is exactly what i am looking for, but i am not successful in getting it to work.
using arduino 1.01

I am using an Ardino UNO and have a max232 connected to pins 0 and 1

at the device i can see the bytes i am sending from the UNO but none of them have the ninth bit set

i have download and installed the revised hardwareserial files

and am using the following in the setup
Serial.begin(19200,true);

and

in the loop
Serial.write9bit(0x03); //wakeup device at address
delay (10);
Serial.write(0x17); // command
delay (10000);

It looks like those amendments didn't work perfectly with 1.0.1 of the IDE.

The files above (same names) have been amended now and should work with 1.0.1. This test checked out on the logic analyzer:

void setup ()
{
  Serial.begin (115200, true);  // 9 bit mode
  Serial.println ("--- starting ---");
}  // end of setup

int i;

void loop () 
{

  for (i = 0; i <= 0x1FF; i++)
    Serial.write9bit (i);  // send another byte
  
}  // end of loop

thanks i will test it in my code tommorrow

Hi Nick et al.,
I know this topic is fairly old, but figured it was best to ask the question here vs. starting a new thread?

I recently got involved in a project where 9-bit serial data was required, and not really knowing what to do I was pretty excited to see Nick's work here (thanks, Nick!).

So my quick question is whether or not the mods to the library are specific to AVR-based micros, or more specifically will the mod work with ARM-based micros? I just don't know enough about how libraries work (yet) to be able to answer this question myself.

Thanks in advance for any pointers, and thanks again Nick for your work here!

Cheers,
David

You are welcome.

The mod was specific to the way that the hardware on this particular processor worked. It probably can be carried over to similar ones in the same line (eg. the Mega2560).

As for ARM ones, they would have different hardware interaction. Quite possibly they might support some sort of 9-bit serial, however I just don't know.

Brilliant, this has saved me a lot of time, many thanks for taking the time to post this. It's great that we can share stuff like this online these days.

Ok, I get the Serial.write9bit but how do I set UCSZn = 7 in order to enable 9 bit function? Do have to turn it off in order to send an 8 bit or will Serial.write do the switch?

Sorry for asking such a basic question but I am kind of new at this.

THANKS in advance!

Which board are you using?

Uno with the 328P SMD R2

I guess the version I published for Teensy 3.1, based on Nick's code, but with changes to use Arduino newer Serial.begin(baud, format)... isn't going to help you much.

I'm getting an error what says it cannot find Serial.write9bit. Not sure why, I've installed the 9bit library properly.

Mike