-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyping.py
executable file
·52 lines (43 loc) · 1.24 KB
/
typing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
"""
Send raw unbuffer data from stdin to ttyUSB0
"""
import sys
import tty
import contextlib
import subprocess
@contextlib.contextmanager
def raw_stdin(stdin=None):
# Get stdin file descriptor
if stdin is None:
stdin = sys.stdin
fd = stdin.fileno()
# Backup the current TTY configuration
old_tty = tty.tcgetattr(fd)
try:
# Set stdin to raw and cbreak
tty.setraw(fd)
tty.setcbreak(fd)
# Set stdin to non-blocking mode
yield stdin
# Restore stdin original configuration
finally:
tty.tcsetattr(fd, tty.TCSADRAIN, old_tty)
def main():
subprocess.run("""\
stty -F /dev/ttyUSB0 \
speed 115200 \
cs8 cstopb parenb -parodd -cmspar \
-hupcl clocal cread crtscts \
-ocrnl -ofdel -ofill -olcuc -onlcr -onlret -onocr -opost \
raw -echo""", shell=True, check=True, capture_output=True)
with raw_stdin():
with open("/dev/ttyUSB0", "wb", buffering=0) as serial:
while True:
char = sys.stdin.buffer.read(1)
if char in b'\x03\x04':
break
print(f"> {char!r}", end="\r\n", file=sys.stderr)
serial.write(char)
if __name__ == "__main__":
main()