-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialconsole.py
executable file
·97 lines (76 loc) · 2.47 KB
/
serialconsole.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os,serial
import thread
import select,sys
import tty
import termios
import time
class NonBlockingConsole(object):
def __enter__(self):
self.old_settings = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin.fileno())
return self
def __exit__(self, type, value, traceback):
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, self.old_settings)
def get_data(self):
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
return sys.stdin.read(1)
return False
# Does serial IO and key commands, quits if any files written locally (e.g. src code changed)
class MyTerminal(object):
def __init__(self,port,baud,watch_files):
self.hex_mode=False
self.watch_files=watch_files
self.last_mod={}
self.console=NonBlockingConsole()
self.port= serial.Serial(
port=port,
baudrate=baud,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=0,
)
def check_files(self):
changed=False
for f in self.watch_files:
now=os.path.getmtime(f)
lm=self.last_mod.get(f,None)
if lm<now:
self.last_mod[f]=now
changed=True
return changed
def keyboard_char(self,char):
self.port.write(char)
echo=False
if char=='h':
self.hex_mode=~self.hex_mode
return
if echo:
sys.stdout.write(">%s" % char)
def start(self,):
with(self.console):
last_check=time.time()
self.check_files()
while(1):
did=False
now=time.time()
if now-last_check > 1:
if self.check_files():
return
t=self.port.read()
if len(t):
did=True
if self.hex_mode:
t="%02x " % ord(t)
sys.stdout.write(t)
c=self.console.get_data()
if c:
self.keyboard_char(c)
if ord(c)==27: #esc
return
did=True
if not did:
sys.stdout.flush()
time.sleep(0.05)
b=MyTerminal(port=sys.argv[1],baud=sys.argv[2],watch_files=sys.argv[3:])
b.start()