Automated Scripting for serial devices under Win32

126 views
Skip to first unread message

si...@mungewell.org

unread,
Jun 2, 2010, 9:53:54 PM6/2/10
to python...@googlegroups.com
So I've had a frustrating day... I've been trying to script up some serial
test routines on Win32 (Linux works), but have tripped across several
problems.

Under Linux I can use python-pexpect and pyserial.

However pexpect uses PTY which is not native supported under Win32 (could
use cygwin, but I'd rather not), and pyserial does not support 'fileno()'
under Win32.

Does anyone have a suggestion of:
1) An alternative to pexpect which works on Win32.

2) An alternative to pyserial which can treat the device via normal FileIO
calls, whilst maintaining some level of compatability with Linux.

Cheers,
Simon.

Greg Saunders

unread,
Jun 2, 2010, 10:06:15 PM6/2/10
to python...@googlegroups.com
> An alternative to pyserial which can treat the device via normal FileIO calls

I've only ever used the native python serial module (and I use is quite a bit) for reading and writing to serial devices. Only on Linux, never on Windows.

The serial module (standard library) treats serial devices as files:

device.read(NUM_BYTES)
device.write(DATA)

I'm not familiar with pyserial so I don't exactly understand your problem.

Greg




--
You received this message because you are subscribed to the Google Groups "calgary python user group" group.
To post to this group, send email to python...@googlegroups.com.
To unsubscribe from this group, send email to pythoncalgar...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pythoncalgary?hl=en.


si...@mungewell.org

unread,
Jun 3, 2010, 11:40:00 AM6/3/10
to python...@googlegroups.com

> The serial module (standard library) treats serial devices as files:
>

It seems that the 'recommended' way to communicate with a serial port is
with PySerial, which under Linux does use FileIO and supports FileNo() to
find out the file handles for the device.

However under Windows, PySerial uses 'win32file' actions which don't
support FileNo(). This incompatibility is noted in the API page:

http://pyserial.sourceforge.net/pyserial_api.html


My quest is to script up control of a serial device which can be done with
a combination of PySerial and python-pexpect (fdpexpect), and under Linux
this is simple and clean.

--
#!/usr/bin/env python

import serial
import fdpexpect

# Open serial port and bond to pexpect
device = serial.Serial(port="COM2", baudrate=115200)
dut = fdpexpect.fdspawn(device)

# Request MEID
dut.send("AT#MEID?\r")
dut.expect("OK")

print "MEID is ",dut.before

dut.close()
--

Under Window this fails on many layers:
1). pexpect requires PTY, which is not Win32. I found an alternate
'wexpect' which uses Wtty in which it runs an application, but doesn't
know about direct FileIO (object?) access.
2). fdexpect assumes that it's actioning on FileIO.
3). As noted above PySerial does not give you a way to find file hands
with FileIO.

I'm still trying to get my head around how this _should_ work cross
platform, and have a feeling that 'pexpect' should be able to work on a
objects directly, without having discover a file handle and then use
FileIO actions.

Any suggestions that people have would be very welcome.
Cheers,
Simon.

Greg Saunders

unread,
Jun 3, 2010, 1:27:16 PM6/3/10
to python...@googlegroups.com
Why not just open the serial device and read and write to it? You could do away with pexpect.


Cheers,
Simon.





si...@mungewell.org

unread,
Jun 3, 2010, 1:46:55 PM6/3/10
to python...@googlegroups.com
> Why not just open the serial device and read and write to it? You could do
> away with pexpect.
>

Expect is itself a nice, power full scripting language which is pretty
well suited to automated testing:
http://en.wikipedia.org/wiki/Expect

I was hoping that all the underlying stuff could be abstracted out, so
that the test scripts could boil down to send 'this', expect 'this' or
'this' and do 'this' on error/timeout.

Doing this via pexpect looked nicer than using the real (TCL based) Expect
that I started with. If I'm handing the code off to someone else at some
point, it is nicer if they can just adapt the 'send'/'expect' commands as
required without really having to learn python.

Simon.


Reply all
Reply to author
Forward
0 new messages