pythonserial-portparity

RS232 word size with or without parity


The bytesize attribute in the serial class is defined as being the number of data bits used for that connection. If I enable odd parity, does it convert one of the defined data bits to signify parity? Or does it just add another bit between the start and stop bits?

import serial

# Define a serial instance with 8 databits and no parity
my_com = serial.serial(bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE)
# My rs232 frame would now look something like:
[ START_BIT, DB0, DB1, DB2, DB3, DB4, DB5, DB6, DB7, STOP_BIT ]

# Change the parity settings
my_com.parity = serial.PARITY_ODD

# Do my frames now look like this
[ START_BIT, DB0, DB1, DB2, DB3, DB4, DB5, DB6, DB7, PARITY, STOP_BIT ]
# or do they look like this?
[ START_BIT, DB0, DB1, DB2, DB3, DB4, DB5, DB6, PARITY, STOP_BIT ]

Any help would be greatly appreciated. Thanks


Solution

  • The parity bit is added as an extra bit after the DB7.

    See this illustration on RS232 timing from Wikipedia. It has some German bits, but it nicely shows how the parity is an extra bit in addition to data bits 0-7.

    Note that usually you will not receive the parity information as some extra data bit from your serial port driver, but your UART will calculate the sum on all data bits + parity bit, and report a parity error if the result is not as expected by the protocol (even bit sum for "even parity", odd bit sum for "odd parity").

    There are some exotic 9-bit applications like Multidrop bus (see also Stackoverflow 14212660), where a 9th bit exists not for the purpose of error checking ("parity bit"), but for some mode switching / addressing. You can use temporary parity changes as a workaround to simulate or monitor such 9-bit applications with standard "8 data bit" RS232/RS485 hardware and drivers.