本文整理汇总了Python中termios.PARENB属性的典型用法代码示例。如果您正苦于以下问题:Python termios.PARENB属性的具体用法?Python termios.PARENB怎么用?Python termios.PARENB使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类termios
的用法示例。
在下文中一共展示了termios.PARENB属性的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import termios [as 别名]
# 或者: from termios import PARENB [as 别名]
def __init__(self, device="/dev/ttyAMA0", baudrate=9600):
if not device.startswith("/dev/"):
device = "/dev/%s" % device
if isinstance(baudrate, str):
baudrate = int(baudrate)
aname = "B%d" % baudrate
if not hasattr(termios, aname):
raise Exception("Unsupported baudrate")
self.baudrate = baudrate
Bus.__init__(self, "UART", device, os.O_RDWR | os.O_NOCTTY)
fcntl.fcntl(self.fd, fcntl.F_SETFL, os.O_NDELAY)
#backup = termios.tcgetattr(self.fd)
options = termios.tcgetattr(self.fd)
# iflag
options[0] = 0
# oflag
options[1] = 0
# cflag
options[2] |= (termios.CLOCAL | termios.CREAD)
options[2] &= ~termios.PARENB
options[2] &= ~termios.CSTOPB
options[2] &= ~termios.CSIZE
options[2] |= termios.CS8
# lflag
options[3] = 0
speed = getattr(termios, aname)
# input speed
options[4] = speed
# output speed
options[5] = speed
termios.tcsetattr(self.fd, termios.TCSADRAIN, options)
示例2: _get_parity
# 需要导入模块: import termios [as 别名]
# 或者: from termios import PARENB [as 别名]
def _get_parity(self):
# Get tty attributes
try:
(_, _, cflag, _, _, _, _) = termios.tcgetattr(self._fd)
except termios.error as e:
raise SerialError(e.errno, "Getting serial port attributes: " + e.strerror)
if (cflag & termios.PARENB) == 0:
return "none"
elif (cflag & termios.PARODD) == 0:
return "even"
else:
return "odd"
示例3: _set_parity
# 需要导入模块: import termios [as 别名]
# 或者: from termios import PARENB [as 别名]
def _set_parity(self, parity):
if not isinstance(parity, str):
raise TypeError("Invalid parity type, should be string.")
elif parity.lower() not in ["none", "even", "odd"]:
raise ValueError("Invalid parity, can be: \"none\", \"even\", \"odd\".")
parity = parity.lower()
# Get tty attributes
try:
(iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(self._fd)
except termios.error as e:
raise SerialError(e.errno, "Getting serial port attributes: " + e.strerror)
# Modify tty attributes
iflag &= ~(termios.INPCK | termios.ISTRIP)
cflag &= ~(termios.PARENB | termios.PARODD)
if parity != "none":
iflag |= (termios.INPCK | termios.ISTRIP)
cflag |= termios.PARENB
if parity == "odd":
cflag |= termios.PARODD
# Set tty attributes
try:
termios.tcsetattr(self._fd, termios.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
except termios.error as e:
raise SerialError(e.errno, "Setting serial port attributes: " + e.strerror)
示例4: __init__
# 需要导入模块: import termios [as 别名]
# 或者: from termios import PARENB [as 别名]
def __init__(self, printer, prot, iomanager):
self.printer = printer
self.prot = prot
self.iomanager = iomanager
(master_fd, slave_fd) = os.openpty()
slave = os.ttyname(slave_fd)
master_flags = fcntl.fcntl(master_fd, fcntl.F_GETFL, 0)
fcntl.fcntl(master_fd, fcntl.F_SETFL, master_flags | os.O_NONBLOCK)
# switch to "raw" mode - these constants come from the manpage for termios under cfmakeraw()
master_attr = termios.tcgetattr(master_fd)
master_attr[0] &= ~(termios.IGNBRK | termios.BRKINT | termios.PARMRK | termios.ISTRIP
| termios.INLCR | termios.IGNCR | termios.ICRNL | termios.IXON)
master_attr[1] &= ~termios.OPOST
master_attr[2] &= ~(termios.CSIZE | termios.PARENB)
master_attr[3] &= ~(termios.ECHO | termios.ECHONL | termios.ICANON | termios.ISIG
| termios.IEXTEN)
master_attr[3] |= termios.CS8
termios.tcsetattr(master_fd, termios.TCSADRAIN, master_attr)
# Fun detail: master will always show as /dev/ptmx, but the kernel knows from
# the fd which PTY we're using. This means we have to use master_fd instead
# of opening master by name.
logging.info("Opened PTY for {} and got {}".format(prot, os.ttyname(slave_fd)))
self.pipe_link = "/dev/" + prot + "_1"
try:
os.unlink(self.pipe_link)
except OSError as e:
# file not found is fine to ignore - anythine else and we should log it
if e.errno != errno.ENOENT:
logging.error("Failed to unlink '{}': {}".format(self.pipe_link, e.strerror))
logging.info("linking {}".format(self.pipe_link))
os.symlink(slave, self.pipe_link)
os.chmod(self.pipe_link, 0o666)
logging.info("{} Pipe open. Use '{}' to communicate with it".format(self.prot, self.pipe_link))
self.rd = os.fdopen(master_fd, "r")
self.wr = os.fdopen(master_fd, "w")
self.send_response = True
self.iomanager.add_file(self.rd, self.get_message)