Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined variable: SerialException

I am using the pySerial library to have a Python script log data from an Arduino. I am trying to handle the SerialException when the script cannot connect to the port you provided, and Eclipse says "Undefined variable: SerialException". Is there something I forgot to import?

Code:

try:
    ser = serial.Serial(port, 9600)
    connected = 1
except SerialException:
    print "No connection to the device could be established"
like image 223
J3RN Avatar asked Dec 11 '22 18:12

J3RN


1 Answers

You probably want:

except serial.SerialException:
   ...

in python, Exceptions are classes derived from Exception. So, when a module/package defines it's own custom exceptions, they usually get imported in the module/packages's namespace just like the other classes/functions. This said, putting a:

from serial import SerialException

at the top of your file would probably also do the trick.

like image 173
mgilson Avatar answered Dec 28 '22 08:12

mgilson