View Full Version : USB Serial format

01-11-2014, 03:31 AM
Hi Paul,

I was wondering if you ever considered or if it's possible to implement the different Serial formats that the Hardware Serial ports have on the USB stack? If you do loopback test with say "SERIAL_7E1" like such:

// LoopBack
void setup() {
Serial1.begin(38400, SERIAL_7E1);

void loop() {
char incomingByte;

if (Serial.available() > 0) {
incomingByte = Serial.read();
if (Serial1.available() > 0) {
incomingByte = Serial1.read();

and send to the USB "-1234" i get "-≤3" printed back which makes sense but it would be nice to print to the usb with these different formats to debug.



01-12-2014, 06:31 PM
So is this possible, I haven't looked into it but willing to if it wouldn't be a huge project.

01-12-2014, 08:20 PM
This post applies to the hardware UARTs.
Using USB virtual serial ports on a PC... perhaps your application wants per-byte parity on each 7 bit datum. I'd think that would be redundant with the error detection/correction in USB itself.
But maybe you have some legacy app that uses a virtual serial driver and insists on parity?

Anyway, this inspired me to discover...

Looking at the teensy 3 source code for HardwareSerial.h, there are two forms of Serialx.begin (where x is one of the hardware UARTs)...

virtual void begin(uint32_t baud) { serial_begin(BAUD2DIV(baud)); }
virtual void begin(uint32_t baud, uint32_t format) {
serial_format(format); }

The second alternative to begin() has a format parameter. That can have values that are constants in Teensy 3 file HardwareSerial.h.
Likely, the same can be done for Teensy 2, using different files.
So I'd guess that this opens the hardware uart in 7 bits even parity mode:

Serial2.begin(9600, SERIAL_7E1);
Interesting: I see that you can specify inverted data polarity for TX and RX - to accommodate, I suppose, an RS232 converter that does/does-not invert.

In that .h file, in the UART section near line number 1472, you can find these and others...

#define SERIAL_7E1 0x02
#define SERIAL_7O1 0x03
#define SERIAL_8N1 0x00
#define SERIAL_8N2 0x04
#define SERIAL_8E1 0x06
#define SERIAL_8O1 0x07
#define SERIAL_7E1_RXINV 0x12
#define SERIAL_7O1_RXINV 0x13
#define SERIAL_8N1_RXINV 0x10
#define SERIAL_8N2_RXINV 0x14
#define SERIAL_8E1_RXINV 0x16
#define SERIAL_8O1_RXINV 0x17
#define SERIAL_7E1_TXINV 0x22
#define SERIAL_7O1_TXINV 0x23
#define SERIAL_8N1_TXINV 0x20
#define SERIAL_8N2_TXINV 0x24
#define SERIAL_8E1_TXINV 0x26
#define SERIAL_8O1_TXINV 0x27
#define SERIAL_7E1_RXINV_TXINV 0x32
#define SERIAL_7O1_RXINV_TXINV 0x33
#define SERIAL_8N1_RXINV_TXINV 0x30
#define SERIAL_8N2_RXINV_TXINV 0x34
#define SERIAL_8E1_RXINV_TXINV 0x36
#define SERIAL_8O1_RXINV_TXINV 0x37
#define SERIAL_9N1 0x84
#define SERIAL_9E1 0x8E
#define SERIAL_9O1 0x8F
#define SERIAL_9N1_RXINV 0x94
#define SERIAL_9E1_RXINV 0x9E
#define SERIAL_9O1_RXINV 0x9F
#define SERIAL_9N1_TXINV 0xA4
#define SERIAL_9E1_TXINV 0xAE
#define SERIAL_9O1_TXINV 0xAF

01-12-2014, 08:53 PM
I'm trying to debug a sensor that uses inverted 7E1 on the Hardware Serial ports and wanted to know if these formats will be implemented on the USB side. I think this would be one of the "just works" things that paul has mentioned before.

The formats for serial ports are implemented in hardware, no software tricks and I haven't looked at the USB documentation yet to see if that was possible also, I'll look into it, but guessing probably not but wanted to make sure before going forward with any software hacks.

01-12-2014, 09:07 PM
I'm trying to debug a sensor that uses inverted 7E1 on the Hardware Serial ports and wanted to know if these formats will be implemented on the USB side.

The USB serial protocol always transports 8 bit data. It doesn't support anything else like real hardware serial does.

Parity is NOT supported by the USB protocol, but USB does have excellent error detection and automatic retry at the lowest levels, automatically handled by the hardware.

but wanted to make sure before going forward with any software hacks.

Normally a project like this would be implement by getting the 8 bit data from USB and sending 7 of the bits to the hardware serial, and receiving the 7 bit data from hardware serial and sending it to the USB side in 8 bit bytes. If your hardware sends inverted bits, you can choose to invert them back to normal on Teensy or on the PC. It's up to you.

Just know that the USB serial protocol only does standard 8 bit bytes. It doesn't offer parity, inverted data or other translations. If you want that stuff, it's relatively simple to do yourself as you move the data between the interfaces.

01-12-2014, 09:17 PM
ok thats what I thought, i almost know nothing about the USB protocol so this clears some things up for me. I figured out how to convert 7E1 to 8N1, just need to figure out how to convert inverted to non inverted now. thanks.