Can Python do serial port stuff?

1754 views
Skip to first unread message

Bill Bartley

unread,
Dec 23, 1997, 3:00:00 AM12/23/97
to

I know it's sort of a "legacy" type of thing, but can Python
access the serial port, including setting the baud, parity,
etc.? I can't find anything about this in the tutorial or
the reference docs.

Thanks,

Bill

Andrew Kuchling

unread,
Dec 23, 1997, 3:00:00 AM12/23/97
to

co...@loop.com (Bill Bartley) wrote:
>I know it's sort of a "legacy" type of thing, but can Python
>access the serial port, including setting the baud, parity,

On what platform? There's a Win95 serial communication module
at http://www.python.org/ftp/python/contrib/System/siomodule.README .
For Unix-like systems, you could probably use os.open() to open
/dev/whatever, and then use the ioctl module to set various
parameters. I don't know if the Mac Python port has any serial port
interfaces; probably...

This question seems to come up a fair amount; could someone
add an entry for serial port I/O to Aaron's PyModules FAQ? (I'm
rather disappointed in the poor response to that FAQ.)


Andrew Kuchling
a...@magnet.com
http://starship.skyport.net/crew/amk/


Steven D. Majewski

unread,
Dec 23, 1997, 3:00:00 AM12/23/97
to

On Tue, 23 Dec 1997, Andrew Kuchling wrote:

> [...] I don't know if the Mac Python port has any serial port
> interfaces; probably...

There is a high level interface thru the communications toolbox
in the standard distribution, and I have a low level interface to
the device manager. ( Written for another hardware device, but
I used a serial loopback to test the basics. )


---| Steven D. Majewski (804-982-0831) <sd...@Virginia.EDU> |---
---| Department of Molecular Physiology and Biological Physics |---
---| University of Virginia Health Sciences Center |---
---| P.O. Box 10011 Charlottesville, VA 22906-0011 |---
All power corrupts and obsolete power corrupts obsoletely." - Ted Nelson


Mitch Chapman

unread,
Dec 23, 1997, 3:00:00 AM12/23/97
to

Andrew Kuchling wrote:
>
> co...@loop.com (Bill Bartley) wrote:
> >I know it's sort of a "legacy" type of thing, but can Python
> >access the serial port, including setting the baud, parity,
>
> On what platform? There's a Win95 serial communication module
> at http://www.python.org/ftp/python/contrib/System/siomodule.README .
> For Unix-like systems, you could probably use os.open() to open
> /dev/whatever, and then use the ioctl module to set various
> parameters. I don't know if the Mac Python port has any serial port
> interfaces; probably...

Yep, you can do all kinds of serial-port manipulations on Unix. The
code frag which follows is in use on Solaris 2.5, using Python (1.4
and 1.5b2) configured with the termios module. And I just tried it
out w. 1.5b2 on Red Hat 5.0, where it passed the "Well, it didn't
throw any exceptions" test. (Sorry, I don't have a DB9-DB25 cable
handy, so can't hook it up to my usual data source.)

> This question seems to come up a fair amount; could someone
> add an entry for serial port I/O to Aaron's PyModules FAQ? (I'm
> rather disappointed in the poor response to that FAQ.)

I'll try to add an entry.

-snip--snip--snip--snip--snip--snip--snip--snip--snip--snip--snip-

import sys, os, fcntl, FCNTL, termios, TERMIOS
...

##################################################################
# Establish a serial-port connection w. required settings.
##################################################################
def openSerial(self, portName="/dev/term/a"):
# The open attempt may fail on account of permissions or on
# account of somebody's already using the port.
# Pass such exceptions on to our client.
try:
# You probably just want to use the builtin open(), here...
fd = os.open(portName, FCNTL.O_RDWR, 0)

# Set up symbolic constants for the list elements returned by
# tcgetattr.
[iflag, oflag, cflag, lflag, ispeed, ospeed, cc] = range(7)

# Set the port baud rate, etc.
settings = termios.tcgetattr(fd)
# Set the baud rate.
settings[ospeed] = TERMIOS.B9600 # Output speed
settings[ispeed] = TERMIOS.B0 # Input speed (B0 => match output)
# Go for 8N1 with hardware handshaking.
settings[cflag] = (((settings[cflag] & ~TERMIOS.CSIZE) |
TERMIOS.CS8) & ~TERMIOS.PARENB)
# NOTE: This code relies on an UNDOCUMENTED
# feature of Solaris 2.4. Answerbook explicitly states
# that CRTSCTS will not work. After much searching you
# will discover that termiox ioctl() calls are to
# be used for this purpose. After reviewing Sunsolve
# databases, you will find that termiox's TCGETX/TCSETX
# are not implemented. *snarl*
settings[cflag] = settings[cflag] | TERMIOS.CRTSCTS
# Don't echo received chars, or do erase or kill input processing.
settings[lflag] = (settings[lflag] &
~(TERMIOS.ECHO | TERMIOS.ICANON))
# Do NO output processing.
settings[oflag] = 0

# When reading, always return immediately, regardless of
# how many characters are available.
settings[cc][TERMIOS.VMIN] = 0
settings[cc][TERMIOS.VTIME] = 0

# Install the modified line settings.
termios.tcsetattr(fd, TERMIOS.TCSANOW, settings)

# Set it up for non-blocking I/O.
fcntl.fcntl(fd, FCNTL.F_SETFL, FCNTL.O_NONBLOCK)

except os.error, info:
# If any of this fails, mention the port name in the
# exception.
raise os.error, "Can't open %s: %s" % (portName, info))

-snip--snip--snip--snip--snip--snip--snip--snip--snip--snip--snip-

--
Mitch Chapman | 4105 Executive Drive
Ohio Electronic Engravers, Inc. | Beavercreek, OH 45430
mcha...@OhioEE.com | import StandardDisclaimer

Jack Jansen

unread,
Dec 24, 1997, 3:00:00 AM12/24/97
to

Aside from the Windows stuff I have a set of modules to access the
serial ports on Unix and Macintoshes. I'm not distributing it yet
because I want to have a look at the windows stuff (and hopefully make
the three compatible to some extent), but if you're seriously longing
for serial port access on unix or mac drop me a note.

--
--
Jack Jansen | ++++ stop the execution of Mumia Abu-Jamal ++++
Jack....@cwi.nl | ++++ if you agree copy these lines to your sig ++++
http://www.cwi.nl/~jack | see http://www.xs4all.nl/~tank/spg-l/sigaction.htm

morse

unread,
Dec 24, 1997, 3:00:00 AM12/24/97
to

Bill Bartley wrote:
>
> I know it's sort of a "legacy" type of thing, but can Python
> access the serial port, including setting the baud, parity,
> etc.? I can't find anything about this in the tutorial or
> the reference docs.
>
> Thanks,
>
> Bill

I'm working on a - don't laugh - DOS serial port module.
Another person is working on a unix module and we are collaborating
to ensure a common interface. What platform are you intersted in?

Reply all
Reply to author
Forward
0 new messages