termios question

Carey Evans c.evans at clear.net.nz
Tue Feb 29 06:28:32 EST 2000

"Allen W. Ingling" <ingling at ctrvax.vanderbilt.edu> writes:

> I can't seem to set the serial port baud rate using the termios
> module.   Permissions on ttyS0 are both read and write for root, and
> I run Python as root.   I'm running  Python 1.5.2 on RedHat 6.1, if
> that's any help.

[snip non-working code]

I seem to have fallen behind in reading clp...  Better late than
never, though.

You should be setting the bps using the numbers at indexes 4 and 5 in
the list returned by tcgetattr, which tcsetattr will then use in calls
to tcsetospeed() and tcsetispeed(), instead of trying to change

For example:

>>> import os, termios, TERMIOS
>>> fd = os.open('/dev/ttyS1', os.O_RDWR | os.O_NOCTTY | os.O_NDELAY)
>>> a = termios.tcgetattr(fd)
>>> a[4:6]
[4098, 4098]  # = B115200, B115200
>>> b = a[:]
>>> b[4:6] = [0, TERMIOS.B1200]
>>> termios.tcsetattr(fd, TERMIOS.TCSANOW, b)
>>> c = termios.tcgetattr(fd)
>>> c[4:6]
[9, 9]  # = B1200, B1200
>>> termios.tcsetattr(a)  # Leave things as we found them.

	 Carey Evans  http://home.clear.net.nz/pages/c.evans/

"*South Park* is another movie straight from the smoking pits of Hell."
                       - http://www.capalert.com/capreports/southpark.htm

More information about the Python-list mailing list