Don't catch SerialException at pySerial inWaiting()

1.2k Views Asked by At

I work with Python3. My purpose is to write simple script, which may handle connect/disconnect and read data. I use pySerial library.

At initial stage of the script I have

ser = serial.Serial('/dev/rfcomm0')

Later I have the next code:

def readAndPrint():
    try:
       waitingChar = ser.inWaiting()
    except serial.SerialException as e:
         print("Got serial exception")
         portOpenFlag = False
         readException = True
    if (waitingChar > 0):
        print("Got some data")
        data_str = ser.read(ser.inWaiting())
        print(data_str)

Everything is fine till I read data, but when I close Bluetooth connection form another side I got

s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
OSError: [Errno 5] Input/output error

and never actually arrive into except serial.SerialException case. What is wrong?

EDIT: This is the traceback:

Traceback (most recent call last):
  File "python_scripts/serialTest.py", line 43, in <module>
    readAndHandleException()
  File "python_scripts/serialTest.py", line 36, in readAndHandleException
    readAndPrint()
  File "python_scripts/serialTest.py", line 22, in readAndPrint
    waitingChar = ser.inWaiting()
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 435, in inWaiting
    s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
OSError: [Errno 5] Input/output error
0

There are 0 best solutions below