Go Down

Topic: Mods to HardwareSerial to handle 9-bit data (Read 36979 times) previous topic - next topic


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!




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.



I've installed the 9bit library properly.
Obviously, you haven't.


Jun 16, 2016, 07:45 am Last Edit: Jun 16, 2016, 08:12 am by sniffi
Hi @ all,

i want use the HardwareSerial document´s from Nick(thank´s Nick for the help) but i must send a framing and overrun error flag with my 16-bit short.

I think it´s the easyest way to set it at bit position 10 and 11, but i´m new at arduino and didn´t know where i must write it in the code. It will be nice when someone can help me and explain where can i change it or what must i do.

with frindly wishes sniffi


Sep 29, 2016, 02:55 pm Last Edit: Sep 30, 2016, 02:48 pm by mohammad-ali
Thanks, Nick Gammon for sharing the library, can you please tell library is capable of receiving 9 bits data. As I want to read 9bit commands and have to create its response accordingly. If activating 9 bit on Arduino how will I monitor or send the bits as the computer only supports 8 bits of serial communication?

Thanking you in anticipation.


Feb 01, 2017, 01:33 am Last Edit: Feb 01, 2017, 01:34 am by Nick Gammon
This code has been reworked to compile under version 1.6.9 of the IDE. The earlier version for 1.0.1 is available from http://gammon.com.au/Arduino/HardwareSerial9bit_IDE_1.0.1.zip

The newer version is at http://gammon.com.au/Arduino/HardwareSerial9bit.zip.

This is a zip archive with 3 files in it:

  • HardwareSerial.cpp
  • HardwareSerial_private.h
  • HardwareSerial.h

You need to replace the existing files in your Arduino distribution for them to work. I strongly suggest saving the existing files first. In my case (Ubuntu) the appropriate folder was:

Code: [Select]


Yours will probably be different, try going to the Arduino IDE directory and then doing a "find" for HardwareSerial.cpp.

This version uses more RAM as the transmit and receive buffers are now two bytes per character rather than one (to fit in the 9th bit).

Example code:

Code: [Select]

void setup ()
 Serial.begin (115200);  // debugging prints
 Serial1.begin (115200, SERIAL_8N1, true);  // 9 bit mode
 Serial2.begin (115200, SERIAL_8N1, true);  // 9 bit mode
 Serial.println ("--- starting ---");
}  // end of setup

int i;

void loop ()

 Serial1.write9bit (i++);  // send another byte
 // display incoming on Serial2
 if (Serial2.available ())
   Serial.println ((int) Serial2.read (), HEX);
 // check if we have sent all possible characters
 if (i >= 0x200)
   delay (100);
   while (Serial2.available ())
     Serial.println ((int) Serial2.read (), HEX);
   delay (5000);
   i = 0;
   }  // end of sent 512 bytes
}  // end of loop

I tested on a Mega so I could use normal serial for debugging prints, and transmit from Serial1 to Serial2 for testing. To do this jumper together TX1 to RX2 (pins 17 and 18 on the Arduino Mega board).
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics


Mar 02, 2017, 01:14 pm Last Edit: Mar 02, 2017, 01:28 pm by mohammad-ali
@Nick Gammon, I have tested the library and it's helpful.

I was wondering, is it possible if I set the AVR internal register for 9-bit communication using util/setbaud.h and set the UCSRB & UCSRB for 9-bit communication will I be able to send/receive 9-bit data using Arduino functions Serial.Write and Serial.Read ? or I will have to receive the data while monitoring the registers UCSRA.


I don't understand that. Can you explain in more detail?
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics


Hello Nick,

thank you for sharing your library. If I select Mega as board in actual Arduino IDE it seems to work for me, too. But I like to use the lib for board "ESP8266 Generic Modul".
With this board selected compiler comes with error messsage "no matching function for call to 'HardwareSerial::begin(int, SerialConfig, bool)' "
I thought actually that ESP8266 also uses the same HardwareSerial as Mega/Uno.
But it seems they are different. Do you think it's easy possible to adapt the lib for ESP8266, too?
To be honest I don't know how to adapt it.
I have also the HardwareSerial lib only in Arduino installation folder


Indeed I've found on


the lib. But I don't know what exactly to do. It would be great if you can help me with this.
Thank you very much in advance!



Hi Nick, I'm interested in carrying on the experimentation in



trying to fool an HU with DMS (Blaupunkt) protocol


to add an external audio source through cd changer connector.

Do you think your library that is a complete solution can handle that communication ? I guess Yes but pls confirm I'm pretty new in handling low lev comms. THANKS


Hi Nick,

I've recently been trying to read 9-bit data from a Raymarine ST60. I've built bi-directional circuit as detailed by Mr Knauf here:


The circuit seems to be fine and operates as expected. I've been trying to use the Arduino 1.6.9 IDE with your software locate here:


The first problem that I encountered was the two missing manifests TXB80 and RXB80. To resolve this I defined them in HarwareSerial.h as:

#define TXB80 0
#define RXB80 1

At this point the code compiles cleanly with the board type set up "Arduino/Genuino Micro".

I'm opening the serial port as:


and I have a simple read loop that loops on:

if (true == Serial1.available ())

    // Read the character.
    int v = Serial1.read() & 0x1FF;

    for(int i = 8; i >= 0; i--)
      if (v & (1 << i))
    Serial.print(" ");
    Serial.print(" ");

The software seems to be reacting as expected. With the ST60 unplugged it is silent when  I plug the ST60 in I get the following and it looks wrong compared to any details of Seatalk that I have seen!

000000000 0 0
110100111 1A7 423
111111111 1FF 511
111111111 1FF 511
000000000 0 0
001110111 77 119
011111111 FF 255
111111111 1FF 511
000000000 0 0
001111111 7F 127
011111101 FD 253
101101100 16C 364
101011011 15B 347
110101101 1AD 429

I was wondering if I was doing anything obviously wrong, or whether indeed, the board that I have chosen is supported?

Many thanks


Go Up