2

I'm currently writing a small utility in python to monitor the communications on a serial line. This is being used to debug some hardware that is connected via rs232 so being able to see exactly what's going over the line is extremely important. How do I check for parity errors using pyserial?

Specifically I'm wondering if there is a platform independent way of finding the value of the parity bit using pyserial. I'd strongly prefer to not need termios to do this as this is used on some windows machines.

shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • How would you define a "parity error"? What have you tried so far? – Dan Lenski Oct 08 '14 at 16:25
  • @DanLenski, specifically I want to see what the value of the parity bit in the serial frames are. – shuttle87 Oct 08 '14 at 16:31
  • I am not sure exactly what your hardware setup looks like, but I believe that in standard PC serial ports the parity checking occurs at the hardware level so it's not going to be visible to the software. If you wish to implement a "[bit-banging](https://en.wikipedia.org/wiki/Bit_banging)" serial port which would let you inspect the individual bits on the line, then you'll probably have to use different hardware... making some kind of GPIO interface? – Dan Lenski Oct 08 '14 at 17:00
  • I could set up a microcontroller to receive the serial then send that over to the computer along with the parity bits and any frame error information by sending 2 bytes for every byte received. That output would then be parsed by the script. However that's exactly the effort I wanted to avoid by just using a couple of usb-to-serial convertors along with pyserial. I guess if it's not possible I'll look into the microcontroller based approach again. – shuttle87 Oct 08 '14 at 17:11
  • I found a couple other threads that might help you out. These discuss various intricacies of reading raw data from serial ports: http://stackoverflow.com/questions/12437593, http://stackoverflow.com/questions/8606291 – Dan Lenski Oct 08 '14 at 18:40

1 Answers1

0

I monitored the parity bit by bit banging with the GPIO4 on my Pi.

Inspiration here

My solution is outputting the parity bit in a second byte and writing all into a file:

import time
import pigpio # http://abyz.me.uk/rpi/pigpio/python.html

RXD=4 # number of GPIO pin

pi = pigpio.pi()

if not pi.connected:
    exit(0)

pigpio.exceptions = False # Ignore error if already set as bit bang read.

handle = pi.file_open("/home/pi/Documents/bit_bang_output.txt",pigpio.FILE_WRITE) #assuming that the file /opt/pigpio/access (yes without extension) contains a line /home/pi/Domcuments/* w

pi.bb_serial_read_open(RXD, 9600,9) # Set baud rate and number of data bits here. Reading 9 data bits will read the parity bit.

pigpio.exceptions = True

stop = time.time() + 5.0 # recording 5.0 seconds

while time.time() < stop:

    (count, data) = pi.bb_serial_read(RXD)
    if count:
        #print(data.hex(),end="")
        pi.file_write(handle, data.hex())

pi.bb_serial_read_close(RXD)

pi.stop()
Loulli
  • 21
  • 2