23 TIOCINQ = hasattr(termios,
'FIONREAD')
and termios.FIONREAD
or 0x541B
24 TIOCM_zero_str = struct.pack(
'I', 0)
27 def __init__(self, device="/dev/ttyAMA0", baudrate=9600):
28 if not device.startswith(
"/dev/"):
29 device =
"/dev/%s" % device
31 if isinstance(baudrate, str):
32 baudrate = int(baudrate)
34 aname =
"B%d" % baudrate
35 if not hasattr(termios, aname):
36 raise Exception(
"Unsupported baudrate")
39 Bus.__init__(self,
"UART", device, os.O_RDWR | os.O_NOCTTY)
40 fcntl.fcntl(self.
fd, fcntl.F_SETFL, os.O_NDELAY)
43 options = termios.tcgetattr(self.
fd)
51 options[2] |= (termios.CLOCAL | termios.CREAD)
52 options[2] &= ~termios.PARENB
53 options[2] &= ~termios.CSTOPB
54 options[2] &= ~termios.CSIZE
55 options[2] |= termios.CS8
60 speed = getattr(termios, aname)
66 termios.tcsetattr(self.
fd, termios.TCSADRAIN, options)
75 s = fcntl.ioctl(self.
fd, TIOCINQ, TIOCM_zero_str)
76 return struct.unpack(
'I',s)[0]
87 if isinstance(data, str):
88 self.
write(data.encode())