Undefined variable: SerialException

I am using pySerial library to get Python script log data from Arduino. I am trying to handle SerialException when the script cannot connect to the port you provided and Eclipse says "Undefined variable: SerialException". Did I forget to import something?

code:

try:
    ser = serial.Serial(port, 9600)
    connected = 1
except SerialException:
    print "No connection to the device could be established"

      

+3


source to share


1 answer


You probably want:

except serial.SerialException:
   ...

      

in python, Exception

are classes derived from Exception

. So when a module / package defines its own custom exceptions, they usually get imported in the module / package namespace just like other classes / functions . This suggests that:



from serial import SerialException

      

at the top of your file will probably do the trick as well.

+11


source







All Articles