10

I have code which read data from serial port.

try: 
  dataIn = self.port.read(100) 
except serial.SerialException: 
  #do some work
  return None

If I disconnect device I catch exception which I can't handle someway.

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/serial/serialposix.py", line 475, in read
    raise SerialException('device reports readiness to read but returned no data (device disconnected or multiple access on port?)')
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/katkov/work/obd2rds/obd2rds/testing/python/main.py", line 48, in <module>
    main()
  File "/home/katkov/work/obd2rds/obd2rds/testing/python/main.py", line 41, in main
    dataIn = serPort.read()
  File "/home/katkov/work/obd2rds/obd2rds/testing/python/uart.py", line 55, in read
    dataIn = self.port.read(100)
  File "/usr/local/lib/python3.4/dist-packages/serial/serialposix.py", line 480, in read
    if e[0] != errno.EAGAIN:
TypeError: 'SerialException' object does not support indexing

How can I catch exception that it is processed properly. Thanks!

Yuriy
  • 377
  • 1
  • 2
  • 10
  • As far as I'm aware, pySerial does not support python 3.4, which you are using here. So you'll need to either use a different library for your needs or rewrite your code in python 2.7. I recommend finding a different library. – Lawrence Dean Vanderpool Feb 13 '15 at 21:57
  • 1
    As noted [here](http://pyserial.sourceforge.net/pyserial.html#requirements) python3.x is available. – Yuriy Feb 13 '15 at 22:25

2 Answers2

10

Thanks for Jonathan Eunice! Your advice fixed my issue.

Now I use following code:

try:
    dataIn = self.port.read()
except serial.SerialException as e:
    #There is no new data from serial port
    return None
except TypeError as e:
    #Disconnect of USB->UART occured
    self.port.close()
    return None
else:
    #Some data was received
    return dataIn
Community
  • 1
  • 1
Yuriy
  • 377
  • 1
  • 2
  • 10
3

You aren't sharing your complete code, but if you're trying to index out your error number, that's not going to work AFAIK. Try:

try: 
  dataIn = self.port.read(100) 
except serial.SerialException as e:
  # ...
  if e.errno != errno.EAGAIN:
  # ...

Also, if you are doing work inside an exception handler that may lead to further exceptions, then nest the handlers. E.g.:

try: 
  dataIn = self.port.read(100) 
except serial.SerialException as e:
  try:
    # more dangerous stuff
  except serial.SerialException as e2:
    # handle nested expression
Jonathan Eunice
  • 21,653
  • 6
  • 75
  • 77
  • Unfortunately no one of your advice helped. – Yuriy Feb 13 '15 at 22:28
  • The code you posted still has `if e[0] != errno.EAGAIN:`. Your error message says `TypeError: 'SerialException' object does not support indexing`. `e[0]` is still indexing. – Jonathan Eunice Feb 13 '15 at 22:43
  • Sorry, that I haven't noted that read(self, size=1) function is from pyserial package: file serialposix.ry. It isn't mine. – Yuriy Feb 13 '15 at 23:23
  • Well, according to the error message, some code is trying to index into a non-indexable type (`SerialException`). Find where that happens, work backwards. – Jonathan Eunice Feb 13 '15 at 23:35
  • This happens in pyserial file serialposix.ry AFAIK. – Yuriy Feb 14 '15 at 00:25
  • Then trap it in a `try: ... except TypeError as e: ...` block. If the exception is flowing out of a black box, you can still capture it. – Jonathan Eunice Feb 14 '15 at 01:22