9-bit Serial wanted and Question on 9600,SERIAL_8E1

I want to decode 9-bit serial. It is 8bit data and the 9th bit is used for separate signal. (Not a Parity).
I found here on Teensy information about support for 9-bit by editing "HardwareSerial.h".
However I could not find the file under "Finder-Applications-controlclick Arduino-show package content" on Mac.
I use Ardino IDE 2.2.1 and Teensy 1.58.
I then started to play with an Arduino UNO Rev4 (M4chip) transmitting and an Teensy 4.0 receiving.

Tx: Seven different Serial.begin was tested. (9600), (9600,SERIAL_8N1), (9600,SERIAL_8N2), (9600,SERIAL_8N2),
(9600,SERIAL_8E1), (9600,SERIAL_8E2), (9600, SERIAL_8O1) and (9600,SERIAL_8O2).
DATA: 41HEX, 42HEX and 43HEX.

Rx on Teensy4: with HWSERIAL.begin(9600); All seven Tx above was received as 41HEX, 42HEX and 43HEX. OK.

Tx: Serial_8E1:
Rx on Teensy with HWSERIAL.begin(9600,SERIAL_8E1); showed 41HEX, 42HEX and 1 43 HEX.
(The parity bit for 41 is 0, for 42 is 0 but for 43 it is 1.

Is it so that HWSERIAL.begin(9600,SERIAL_8E1 is a 9.bit decoding???





Rx code:

// set this to the hardware serial port you wish to use
#define HWSERIAL Serial1
void setup() {
Serial.begin(115200);
// HWSERIAL.begin(9600); // OK received 41, 42, 43 in HEX
//HWSERIAL.begin(9600, SERIAL_8N1); // OK received 41, 42, 43 in HEX
//HWSERIAL.begin(9600, SERIAL_8N2); // OK received 41, 42, 43 in HEX
HWSERIAL.begin(9600, SERIAL_8E1); // received 41, 42, 1 43 HEX)
// HWSERIAL.begin(9600, SERIAL_8E2); // received 41, 42, 1 43 HEX)
//HWSERIAL.begin(9600, SERIAL_8O1); // received 1 41 HEX), 1 42 HEX), 43 HEX)
//HWSERIAL.begin(9600, SERIAL_8O2); // received 1 41 HEX), 1 42 HEX), 43 HEX)
//HWSERIAL.begin(9600, SERIAL_9N1); // No COMPILE

delay(1000);
Serial.println("Teensy_example_code_Serial_CAmod") ;
delay(1000);
}
void loop() {
int incomingByte;

if (HWSERIAL.available() > 0) {
incomingByte = HWSERIAL.read();
Serial.print("received: ");
Serial.print("DEC: ");Serial.print(incomingByte, DEC);
Serial.print(". HEX: ");Serial.println(incomingByte, HEX);
}
}
 
1 43 in HEX is correct . The Parity/9th bit is 1 and the 8bit data is 43 HEX.
My Keysight oscilloscope with Serial decode with settings (#bits=8, parity Even, Idle High, Bit order LSB) says Data : 43 HEX, Parity OK.
 
It would be great to have a confirmation that this is way to have 9-bit serial decode so the the code really reads the 9th bit.
 
In order to hold 9 bits of data, you need to enable that within HardwareSerial.h
Code:
// Uncomment to enable 9 bit formats.  These are default disabled to save memory.
//#define SERIAL_9BIT_SUPPORT

Without this, the Software queues are 8 bits, with it, they are 16 bites per entry. In the case of 8N1 or 2, does not
matter as there are just 8 bits. But for 8E1 or 8O1, again it may not matter, as TX will probably generate the parity bit
and on RX the hardware will probably check, although I am not sure how your code will see if there was something received
with parity error. As you would only receive the actual 8 bits of dat.
 
I saw that article in "PaulStroffregen / cores" about uncomment in the "HardwareSerial.h" .
It is also described where to find the files.
On MAC , Finder-Applications-controlclick Arduino-show packages-..
However I can't find the file !
 
If using Arduino 1.8.19, control-click on the program and "Show Package Contents".

If using Arduino 2.2.1, look in your Library folder. In may be hidden in MacOS Finder, but you can see it in Terminal.
 
On Mac with Arduino 2.1.0 I found 3 HardwareSerial.h.
my name>Documents>Arduino>hardware>teensy>avr>cores>teensy4
my name>Documents>Arduino>hardware>teensy>avr>cores>teensy3
my name>Documents>Arduino>hardware>teensy>avr>cores>teensy

On all I have uncommented the line so 9.bit should be OK. But still I get
"Compilation error: 'SERIAL_9N1' was not declared in this scope.......

Using Mac Terminal:
my name>Library>Arduino15>package_teensy_index.jason
my name>Library>Arduino15>package_adafruit_index.jason
my name>Library>Arduino15>package_rp2040_index.jason
...

I will do some more work during the weekend...

But still without 9 bit support enabled I wonder why a Tx with "9600,SERIAL_8E1"
with data 65, 66 and 67 DEC (=41, 42,43 HEX) will give a Rx on Teensy4.0 with settings
"9600,SERIAL_8E1" :
Rx: 65, 66 and 323 DEC (= 41, 42, 1 43 in HEX) ?
My oscilloscope with the same setting reports 41, 42 and 43, all with valid Parity indication.
So what is the "1" saying in 1 43 ?
is it an indication that there should be a Parity 1 here with that 8 bit data? ( but it has not been decoded)?
(which is correct)
 
On all I have uncommented the line so 9.bit should be OK. But still I get
"Compilation error: 'SERIAL_9N1' was not declared in this scope.......

Since you're using Teensy 4.0, you would need to edit the HardwareSerial.h inside the "teensy4" folder.

When editing core library files, a good first step is to intentionally add any syntax error. Then click Verify in Arduino. If you see the syntax error reported, then you know you're editing the correct file and Arduino IDE really is noticing you changed it and trying to recompile the code.

You can use the same trick to check if the define is really working. For example, inside HardwareSerial.h:

Code:
#define SERIAL_8E1_RXINV_TXINV 0x36
#define SERIAL_8O1_RXINV_TXINV 0x37
#ifdef SERIAL_9BIT_SUPPORT
blah blah blah
#define SERIAL_9N1 0x84
#define SERIAL_9E1 0x8E
#define SERIAL_9O1 0x8F

You can check whether you get a syntax error about "blah blah blah" right before the place where SERIAL_9N1 is supposed to be defined. If you click Verify and get no "blah blah blah" error, that's a sure sign your other editing didn't actually define SERIAL_9BIT_SUPPORT.
 
Just a report to say that finally everything works fine with 9 bit !
I have moved from Windows PC to MAC Studio (M1) and also "moved" Arduino to MAC. And also changed to Arduino 2.2.1 with the Boards manager.
So now for MAC the "HardwareSerial.h" was found under:
/user/...my name/../Library/Arduino15/packages/teensy/hardware/avr/1.58.1/cores/teensy4.
On MAC you must use Terminal to find it. The Apple-File-find does not find it.
For Tx I found out that you must use Serial1.write9bit() ;
Now I have one Teensy4.0 for Tx and one Teensy4.0 for Rx and also checking with oscilloscope with serial decode that all is OK !

Thank you for all help !!
 
Back
Top