Question

I want to send messages through the serial port using PySerial. One of the parameters for the serial constructor is 'bytesize'. I have been trying serial.SEVENBITS and serial.EIGHTBITS and haven't noticed a difference. The documentation is a bit vague and I am new to both Python and serial communication. Does this just set the maximum value a byte can hold or is it something to do with signed bytes? Can anyone clear up why I'd use 7 bits over 8? I have been searching but haven't found an answer.

Thank you

Was it helpful?

Solution

This refers to the number of data bits in each transmitted character. From wikipedia:

Data bits

The number of data bits in each character can be 5 (for Baudot code), 6 (rarely used), 7 (for true ASCII), 8 (for any kind of data, as this matches the size of a byte), or 9 (rarely used). 8 data bits are almost universally used in newer applications. 5 or 7 bits generally only make sense with older equipment such as teleprinters.

Most serial communications designs send the data bits within each byte LSB (Least Significant Bit) first. This standard is also referred to as "little endian". Also possible, but rarely used, is "big endian" or MSB (Most Significant Bit) first serial communications. (See Endianness for more about bit ordering.) The order of bits is not usually configurable, but data can be byte-swapped only before sending.

OTHER TIPS

Historically, it was common to only send ASCII text over a serial connection, which fit into seven bits, and an eighth bit would be used as a parity marker which could indicate if the data was being transmitted correctly.

Since the parity check doesn't catch errors on an even number of bits, and can't correct the data at all, it's not that valuable, and modern practice is to use 8-bit data and do error detection and correction at a higher layer of protocol.

Short answer is you probably want 8-bit, but it depends on what the other end of the serial connection is expecting.

Update: From your other question it sounds like you're programming both ends of the connection, and checksumming your messages, so it's definitely most straightforward to use 8-bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top