我试图找出如何使用termios调用来配置tty。下面我正在玩波特率。我能够调用tcgetattr,更改值,看到它们在发送之前已经在打印中发生了变化。然而,tcsetattr后跟tcgetattr表明硬件没有得到它们。
我尝试更改为su,以防它是权限,但没有任何区别。我看到通过编写一个特殊的termios结构可以锁定和解锁termios结构。如何测试它是否被锁定,如果是,我该如何解锁?
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")
来自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']] < / p>
to tcsetattr [0,0,2239,0,0697,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']] < / p>
回读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']] < / p>
答案 0 :(得分:0)
这似乎工作正常。很明显,termios包含超出ttyS0处理范围的波特率,但我的ttyUSB0可以处理它们。必须在tcsetattr之后调用tcgetattr以查看值是否已更改。
似乎驱动程序根据ispeed和ospeed值执行CBAUD和CBAUDEX。
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&#39; \ x03&#39;,b&#39; \ x1c&#39;,b&#39; \ x7f&#39;,b&#39; ; \ x15&#39;,b&#39; \ x01&#39;,0,1,b&#39; \ x00&#39;,b&#39; \ x11&#39;,b&#39; \ x13&#39; ,b&#39; \ x1a&#39;,b&#39; \ x00&#39;,b&#39; \ x12&#39;,b&#39; \ x0f&#39;,b&#39; \ x17&#39; ,b&#39; \ x16&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;]]
[4,0,1215,0,15,15,[b&#39; \ x03&#39;,b&#39; \ x1c&#39;,b&#39; \ x7f&#39;,b&#39; ; \ x15&#39;,b&#39; \ x01&#39;,0,1,b&#39; \ x00&#39;,b&#39; \ x11&#39;,b&#39; \ x13&#39; ,b&#39; \ x1a&#39;,b&#39; \ x00&#39;,b&#39; \ x12&#39;,b&#39; \ x0f&#39;,b&#39; \ x17&#39; ,b&#39; \ x16&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;]]
[4,0,529,0,0897,4097,[b&#39; \ x03&#39;,b&#39; \ x1c&#39;,b&#39; \ x7f&#39;,b&#39 ; \ x15&#39;,b&#39; \ x01&#39;,0,1,b&#39; \ x00&#39;,b&#39; \ x11&#39;,b&#39; \ x13&#39; ,b&#39; \ x1a&#39;,b&#39; \ x00&#39;,b&#39; \ x12&#39;,b&#39; \ x0f&#39;,b&#39; \ x17&#39; ,b&#39; \ x16&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;]]
[4,0,529,0,4098,4098,[b&#39; \ x03&#39;,b&#39; \ x1c&#39;,b&#39; \ x7f&#39;,b&#39; ; \ x15&#39;,b&#39; \ x01&#39;,0,1,b&#39; \ x00&#39;,b&#39; \ x11&#39;,b&#39; \ x13&#39; ,b&#39; \ x1a&#39;,b&#39; \ x00&#39;,b&#39; \ x12&#39;,b&#39; \ x0f&#39;,b&#39; \ x17&#39; ,b&#39; \ x16&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;]]
[4,0,5299,0,4099,4099,[b&#39; \ x03&#39;,b&#39; \ x1c&#39;,b&#39; \ x7f&#39;,b&#39; ; \ x15&#39;,b&#39; \ x01&#39;,0,1,b&#39; \ x00&#39;,b&#39; \ x11&#39;,b&#39; \ x13&#39; ,b&#39; \ x1a&#39;,b&#39; \ x00&#39;,b&#39; \ x12&#39;,b&#39; \ x0f&#39;,b&#39; \ x17&#39; ,b&#39; \ x16&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;]]
[4,0,5300,0,4100,4100,[b&#39; \ x03&#39;,b&#39; \ x1c&#39;,b&#39; \ x7f&#39;,b&#39; ; \ x15&#39;,b&#39; \ x01&#39;,0,1,b&#39; \ x00&#39;,b&#39; \ x11&#39;,b&#39; \ x13&#39; ,b&#39; \ x1a&#39;,b&#39; \ x00&#39;,b&#39; \ x12&#39;,b&#39; \ x0f&#39;,b&#39; \ x17&#39; ,b&#39; \ x16&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39;,b&#39; \ x00&#39; ,b&#39; \ x00&#39;,b&#39; \ x00&#39;]]