Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Can Python do serial port stuff?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Mitch Chapman  
View profile  
 More options Dec 23 1997, 3:00 am
Newsgroups: comp.lang.python
From: Mitch Chapman <mchap...@ohioee.com>
Date: 1997/12/23
Subject: Re: Can Python do serial port stuff?

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
mchap...@OhioEE.com           |     import StandardDisclaimer


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.