pexpect - Python - In Windows, how to intertact with the serial port? -


under linux, use fdpexpect module interact serial port, such as:

 fd = os.open(tty, os.o_nonblock|os.o_rdwr|os.o_noctty)  child = fdpexpect.fdspawn(fd) 

in windows, how implement above?

i got working on weekend on windows 7. here how did it:

first, fdpexpect module seem way "chat" serial port in python 2.7. latest python pexpect module docs says can take integer (int) file descriptor (like fdpexpect), doesn't work on ubuntu 12.10 install. seems fdpexpect way go. if from:

http://www.opensource.apple.com/source/lldb/lldb-69/test/pexpect-2.4/fdpexpect.py

second, fdpexpect module requires file descriptor input. although python pyserial module ("import serial") cross-platform, use fdpexpect 1 must use serial.fileno() method int file descriptor serial port. serial.fileno() method not exist in windows python; exists in posix python, integer file descriptors used.

fortunately, can made work using cygwin. cygwin free posix-like environment windows os. run cygwin setup.exe , select following cygwin packages:

python nano wget 

then run following commands @ cygwin bash shell prompt:

# install 'distribute', can use install 'pip': wget.exe http://python-distribute.org/distribute_setup.py  # execute downloaded script: python distribute_setup.py  # 'pip' installer: wget --no-check-certificate https://raw.github.com/pypa/pip/master/contrib/get-pip.py  python get-pip.py  # install pyserial serial comms w/pexpect support via serial.fileno() pip install pyserial 

now, if run python script under cygwin python install (and not windows native python), can pass output of serial.fileno() fdpexpect, , communicate sendline() , expect(). communicating 2 different embedded systems under windows using method.

note serial.serial() constructor takes string "/dev/ttys0" under unix, under windows (incl. cygwin) needs integer. use int(2) com3, int(3) com4, , on. device manager tell com port numbers should use.

...

one final note if talking arduino uno... on windows 7 system, plugging in arduino causes /dev/ttys## show immediately, 1 expect. however, serial port not work until use arduino software serial terminal, or putty, or cygwin 'screen' command open arduino serial port. once you've opened in 1 of programs, works fine until unplugged. don't know why; seems bug in arduino driver. (i not have problem using ftdi driver non-arduino device.)


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -