Monday, August 31, 2009

Notes on using the serial ports within python


I am using one computer for image processing (running the scanner) and another computer for film transport. The two computers both run python and can communicate over their respective serial ports. I am running python 2.5 and pyserial for this. I made a null modem cable by cutting up an old printer cable and a new 9 pin plug for the other end. A null modem requires that pins 2 and 3 be crossed. My cable uses the above pinout. The idea is that after the very first scan computer A must inform computer B to advance the film. When that is done, Computer B must tell A to scan another image. And so on. The serial port looks perfect for this. Computer B is an old "expendable" computer that I can experiment with, adding circuits to the parallel port. Whereas Computer A is my workday computer that I don't want to burn it out. That is why I am using two computers. Aside from that, there is no real advantage to using two computers.
The below code snippet show all you need to write to send data down you serial port - pretty easy.
_______________________________________
import serial
import os
ser=serial.Serial(0)
print ser.portstr # shows what port is opened
x=0
while x < 10000: #just a big loop for testing - this runs a long time
ser.write("hellowwwwww")
ser.write("\n") # new line - not sure about this!
print x
x=x+1
ser.close

__________________________________________

No comments:

Post a Comment