Friday, April 6, 2012

pyserial - usb serial port scan

good to be back after a break. I have been using pyserial for performing serial automation on hardware. I faced a peculiar problem . Thought it will be a good idea to post it. I have usb-serial cable connected to pc which is bluetooth capable. Now, when i do a port scan , it also lists the serial ports from bluetooth too. this was causing problems in my script execution. so I had to modify the port scan script to take care of this problem . the script is below ..

print "Checking for the Serial Port....."

for i in range(256):
try:
ser = serial.Serial(i, 115200,timeout=1)
print ser.portstr
ser.setWriteTimeout(1)
ser.write("hello" + "\n")
data = ser.read(9999)
print data
ser.close()
except serial.SerialException:
print "exception",sys.exc_info()[1]
pass

Just copy this code and use it. keep in mind , the true validation can be based on the data in my script or the time it takes to respond to such a request over serial.

i have also added the exception print info which could turn in handy .

Happy Scripting

No comments:

Post a Comment