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:
/usr/local/bin/arduino-1.6.9/hardware/arduino/avr/cores/arduino
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:
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).