开发者

'bytesize' in PySerial module

开发者 https://www.devze.com 2023-01-28 15:02 出处:网络
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.EIGHTBIT

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消