1

I am trying to figure out how to use the termios calls to configure the tty. Below I am playing with baudrate. I am able to call tcgetattr, change the values, see that they have changed in a print before I send them. However tcsetattr followed by tcgetattr shows that the hardware is not getting them.

I tried changing to su in case it was a permissions thing but that made no difference. I see that it is possible for the termios structure to be locked and unlocked by writing a special termios structure. How do I test it to see if it is locked, and if so, how do I unlock it?

import os
import termios

def TestTTY(tty):
    fd = os.open(tty,os.O_RDWR | os.O_NONBLOCK)
    if fd:
        if os.isatty(fd):
            tios = termios.tcgetattr(fd)
            print("From tcgetattr")
            print(tios)
            #try to set the baud to B38400
            tios[2] &= ~termios.CBAUD
            tios[2] |= termios.B38400
            print("To tcsetattr")
            print(tios)
            termios.tcsetattr(fd,termios.TCSANOW,tios)
            print("Readback tcgetattr")
            print(termios.tcgetattr(fd))

        os.close(fd)

TestTTY("/dev/ttyS0")

From tcgetattr [0, 0, 6321, 0, 4097, 4097, [b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', 0, 0, b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00']]

To tcsetattr [0, 0, 2239, 0, 4097, 4097, [b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', 0, 0, b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00']]

Readback tcgetattr [0, 0, 6321, 0, 4097, 4097, [b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', 0, 0, b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00']]

DougM
  • 9
  • 2
  • If you have more information to add, please do so. You don't need to say that it's not letting you in the post itself. If it's giving you the majority code error, then you need to include more info. – jhpratt Apr 29 '18 at 00:45

1 Answers1

0

This seems to work OK. It is clear that termios contains baudrates beyond what ttyS0 will handle, but my ttyUSB0 can handle them. Have to call tcgetattr after tcsetattr to see if the values changed.

It also seems that the driver does the CBAUD and CBAUDEX twiddling based on the ispeed and ospeed values.

import os
import termios
import re
from functools import cmp_to_key

OffsetTermiosFlags = [
    'iflag',
    'oflag',
    'cflag',
    'lflag',
    'ispeed',
    'ospeed'
]

def CompareBaudrateAttributes(b1,b2):
    p = re.compile("^B(\d{1,})$")
    return int(p.match(b1).group(1))-int(p.match(b2).group(1))

def BuldListOfBaudrateAttributes():
    attrs = dir(termios)
    bauds = []
    for attr in attrs:
        if re.search("^B\d{1,}$",attr):
            bauds.append(attr)
    bauds = list(sorted(bauds,key=cmp_to_key(CompareBaudrateAttributes)))
    return bauds

def GetBaudrateAttributeValue(name):
    return getattr(termios,name)

def cfsetspeed(tios,speed):
    tios[OffsetTermiosFlags.index('ispeed')] = speed
    tios[OffsetTermiosFlags.index('ospeed')] = speed
    return tios

def TestTTY(tty):
    fd = os.open(tty,os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
    if fd:
        tios = termios.tcgetattr(fd)
        print(tios)
        bauds = BuldListOfBaudrateAttributes()
        for baud in bauds:
            if hasattr(termios,'cfsetspeed'):
                termios.cfsetspeed(tios,getattr(termios,baud))
            else:
                cfsetspeed(tios,getattr(termios,baud))
            termios.tcsetattr(fd,termios.TCSAFLUSH,tios)
            tios = termios.tcgetattr(fd)
            print(tios)
        os.close(fd)

TestTTY("/dev/ttyUSB0")

[4, 0, 1214, 0, 14, 14, [b'\x03', b'\x1c', b'\x7f', b'\x15', b'\x01', 0, 1, b'\x00', b'\x11', b'\x13', b'\x1a', b'\x00', b'\x12', b'\x0f', b'\x17', b'\x16', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00']]

[4, 0, 1215, 0, 15, 15, [b'\x03', b'\x1c', b'\x7f', b'\x15', b'\x01', 0, 1, b'\x00', b'\x11', b'\x13', b'\x1a', b'\x00', b'\x12', b'\x0f', b'\x17', b'\x16', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00']]

[4, 0, 5297, 0, 4097, 4097, [b'\x03', b'\x1c', b'\x7f', b'\x15', b'\x01', 0, 1, b'\x00', b'\x11', b'\x13', b'\x1a', b'\x00', b'\x12', b'\x0f', b'\x17', b'\x16', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00']]

[4, 0, 5298, 0, 4098, 4098, [b'\x03', b'\x1c', b'\x7f', b'\x15', b'\x01', 0, 1, b'\x00', b'\x11', b'\x13', b'\x1a', b'\x00', b'\x12', b'\x0f', b'\x17', b'\x16', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00']]

[4, 0, 5299, 0, 4099, 4099, [b'\x03', b'\x1c', b'\x7f', b'\x15', b'\x01', 0, 1, b'\x00', b'\x11', b'\x13', b'\x1a', b'\x00', b'\x12', b'\x0f', b'\x17', b'\x16', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00']]

[4, 0, 5300, 0, 4100, 4100, [b'\x03', b'\x1c', b'\x7f', b'\x15', b'\x01', 0, 1, b'\x00', b'\x11', b'\x13', b'\x1a', b'\x00', b'\x12', b'\x0f', b'\x17', b'\x16', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00', b'\x00']]

DougM
  • 9
  • 2