[[home/harland/serial.py]] Example to catch an exception in python if you have a line of code that can blow up and throw an exception. you can wrap it in a try/except block in python like the following. the "pass" line says, move along, these aren't the errors you're looking for. try: ser.foobar() except: print "oops. it blew up" pass Read one byte with a 5 second timeout from serial port This is a bit of a strange way to do it.. but we start by defining a byte array of length 1 This is the input buffer. if we pass in a byte array to the read method, we can also pass a time out. the ser.read(buff, timeoutMS) will return the number of chars read into the buffer buff = [0] print "Reading 1 byte" numread = ser.read(buff, 1000) print "Done waiting" Log in or register to post comments
Example to catch an exception in python if you have a line of code that can blow up and throw an exception. you can wrap it in a try/except block in python like the following. the "pass" line says, move along, these aren't the errors you're looking for. try: ser.foobar() except: print "oops. it blew up" pass
Read one byte with a 5 second timeout from serial port This is a bit of a strange way to do it.. but we start by defining a byte array of length 1 This is the input buffer. if we pass in a byte array to the read method, we can also pass a time out. the ser.read(buff, timeoutMS) will return the number of chars read into the buffer buff = [0] print "Reading 1 byte" numread = ser.read(buff, 1000) print "Done waiting"
Example to catch an exception in python
if you have a line of code that can blow up and throw an exception. you can wrap it in a try/except block in python like the following.
the "pass" line says, move along, these aren't the errors you're looking for.
Read one byte with a 5 second timeout from serial port
This is a bit of a strange way to do it.. but we start by defining a byte array of length 1
This is the input buffer. if we pass in a byte array to the read method, we can also pass a time out.
the ser.read(buff, timeoutMS) will return the number of chars read into the buffer