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"
You probably want:
except serial.SerialException:
...
in python, Exception
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With