Monday, April 9, 2012

perl with python

I am not sure how many times i have come across to a point where I had to use a perl script with python. And I realized that this could be a case for few of you guys too. thought why not put a post for this. here is a simple example of how to do it.


import os
os.system("perl ~perlfile~")

you could use sys.exit() to see if the execution was a success. This is the way i am using it. could solve problems at least for a few of you guys.

Friday, April 6, 2012

write/read to serial port

Well, when you have pyserial installed on your pc writing to serial port is fairly simple. here is the how you do it..

import serial
ser = serial.Serial(, ,timeout=1)
print ser.portstr
ser.setWriteTimeout(1)
ser.write("hello" + "\n")
ser.read(9999)

the above sample code best describes how to write or read from a serial port using python - pyserial library. This is not the best of the solutions available out there but def simple (probably the simplest :) ). you could do more digging into the serial library too if you would like.

Like I always say happy scripting !!!

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