Multiplatform Python Function to communicate 9-bit based RS-485 network
When I was working on one of my projects on RaspberryPI I met some difficulties of using 9-bit serial communication on Linux. The problem is that in POSIX based platforms there are no MARK and SPACE parity configuration bits like in Windows. However, there is workaround. It is possible to access CMSPAR flag (control mode flag for MARK/SPACE parity) using termios module for Python. And then we are able to control MARK/SPACE by changing PARODD flag. Excellent description of problem is given here http://www.lothosoft.ch/thomas/libmip/markspaceparity.php.
In code below I communicate using 9-bit based serial communication protocol. Code could be used both in Windows and Linux environment. 1 byte 0x55 is just for "line activation" or preamble used to clean up (discharch) any static charges on the line. Then the first "address" byte which is used to address dedicated external device, while others will just ignore all message. Address byte should have MARK parity to let external hardware to detect it as a "address" byte. All other bytes have SPACE parity and are actual data. At the and of message i usually put CRC8 control byte to eliminate any mistakes during communication.