-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinycom.py
112 lines (107 loc) · 3.52 KB
/
tinycom.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
import sys
import time
import thread
import serial
import termios
import tty
import traceback
print '[[[Opening', sys.argv[1], ']]]'
f = serial.Serial(sys.argv[1], int(sys.argv[2]))
FD = f.fileno()
print '[[[FD=', FD, ']]]'
old = termios.tcgetattr(sys.stdin.fileno())
print '[[[Setting cbreak...]]]'
tty.setraw(sys.stdin.fileno())
print '[[[...ready]]]\r'
def write_thread(fd):
while True:
buffer = f.read(1)
#print 'READ: %r'%(buffer,)
sys.stderr.write(buffer)
thread.start_new(write_thread, (FD,))
cflag = False
cintro = '[[[^C{h,H(elp),q,Q,e,E,f,F,^C}?]]]'
cblank = '\b'*len(cintro)+' '*len(cintro)+'\b'*len(cintro)
cunknown = '\b'*(len(cintro)-5)+' '*(len(cintro)-5)+'\b'*(len(cintro)-5)+'?!]]]'
lecho = False
delay = 0.001
chelptext = '''
[[[^C mode commands:
-h,H: this help text
-q,Q: quit
-e,E: toggle local echo
-F: send local file
-f: send local file, throttled
-t, T: view/set throttle rate
-^C: send ^C]]]
'''.replace('\n', '\r\n')
try:
while True:
buffer = sys.stdin.read(1)
#sys.stderr.write('[[[IN:%r]]]'%(buffer,))
if cflag:
if buffer in ('q', 'Q'):
raise SystemExit()
elif buffer in ('e', 'E'):
sys.stderr.write(cblank)
lecho = not lecho
elif buffer in ('h', 'H'):
sys.stderr.write(cblank)
sys.stderr.write(chelptext)
elif buffer in ('t', 'T'):
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old)
res = raw_input('[[[Period (currently %f), blank to keep: '%(delay,))
if not res:
print 'No change.]]]'
else:
delay = float(res)
print 'Set.]]]'
tty.setraw(sys.stdin.fileno())
elif buffer in ('f', 'F'):
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old)
if buffer == 'F':
stime = 0
else:
stime = delay
fname = raw_input('[[[File: ')
try:
fp = open(fname, 'rb')
except Exception:
print 'File open failed:'
traceback.print_exc()
else:
fp.seek(0, 2) #EOF
sz = fp.tell()
fp.seek(0, 0)
while True:
data = fp.read(1)
if fp.tell()%32 == 0 or not data:
print '\r'+' '*50+'\rProgress: %d/%d (%.2f%%)'%(fp.tell(), sz, 100*float(fp.tell())/sz),
if not data:
break
f.write(data)
time.sleep(stime)
print 'Done.]]]'
tty.setraw(sys.stdin.fileno())
elif buffer == '\x03':
sys.stderr.write(cblank)
f.write(buffer)
if lecho:
sys.stderr.write(buffer)
else:
sys.stderr.write(cunknown)
cflag = False
else:
if buffer == '\x03':
if not cflag:
sys.stderr.write(cintro)
cflag = True
else:
f.write(buffer)
if lecho:
sys.stderr.write(buffer)
except Exception:
traceback.print_exc()
finally:
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old)