-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
141 lines (132 loc) · 3.49 KB
/
test.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import argparse
import atexit
import serial
import sys
import time
import threading
import traceback
magicpacket = [0xde, 0xad, 0xbe, 0xef]
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", help="Serial port name")
parser.add_argument("-b", "--baud", help="Baud rate for serial port", type=int)
args = parser.parse_args()
portname = args.port
baud = args.baud
serial_reader_run = True
def serial_reader():
flush = True
while (serial_reader_run):
try:
#print(port.inWaiting())
if port.inWaiting() > 0:
flush = True
inbytes = port.read()
for b in inbytes:
if b >= 0x20 and b <= 0x255:
sys.stdout.write(chr(b));
elif b == 0x00:
sys.stdout.write("{NUL}")
elif b == 0x01:
sys.stdout.write("{SOH}")
elif b == 0x02:
sys.stdout.write("{STX}")
elif b == 0x03:
sys.stdout.write("{ETX}")
elif b == 0x04:
sys.stdout.write("{EOT}")
elif b == 0x05:
sys.stdout.write("{ENQ}")
elif b == 0x06:
sys.stdout.write("{ACK}")
elif b == 0x07:
sys.stdout.write("{BEL}")
elif b == 0x08:
sys.stdout.write("{BS}")
elif b == 0x09:
sys.stdout.write("\t")
elif b == 0x0a:
sys.stdout.write("\n")
elif b == 0x0b:
sys.stdout.write("\v")
elif b == 0x0c:
sys.stdout.write("\f")
elif b == 0x0d:
sys.stdout.write("\r")
elif b == 0x0e:
sys.stdout.write("{SO}")
elif b == 0x0f:
sys.stdout.write("{SI}")
elif b == 0x10:
sys.stdout.write("{DLE}")
elif b == 0x11:
sys.stdout.write("{DC1}")
elif b == 0x12:
sys.stdout.write("{DC2}")
elif b == 0x13:
sys.stdout.write("{DC3}")
elif b == 0x14:
sys.stdout.write("{DC4}")
elif b == 0x15:
sys.stdout.write("{NAK}")
elif b == 0x16:
sys.stdout.write("{SYN}")
elif b == 0x17:
sys.stdout.write("{ETB}")
elif b == 0x18:
sys.stdout.write("{CAN}")
elif b == 0x19:
sys.stdout.write("{EM}")
elif b == 0x1a:
sys.stdout.write("{SUB}")
elif b == 0x1b:
sys.stdout.write("{ESC}")
elif b == 0x1c:
sys.stdout.write("{FS}")
elif b == 0x1d:
sys.stdout.write("{GS}")
elif b == 0x1e:
sys.stdout.write("{RS}")
elif b == 0x1f:
sys.stdout.write("{US}")
else:
sys.stdout.write("{WTF}")
else:
if flush:
sys.stdout.flush()
flush = False
time.sleep(0.05)
except SystemExit:
sys.stdout.write('SERIAL exiting due to SystemExit\n')
break
except OSError:
e = sys.exc_info()
sys.stderr.write('SERIAL: {0}: {1}\n'.format(e[0],e[1]))
traceback.print_tb(e[2])
break
except:
e = sys.exc_info()
sys.stderr.write('SERIAL: {0}: {1}\n'.format(e[0],e[1]))
traceback.print_tb(e[2])
port = serial.Serial(port=portname, baudrate=baud, rtscts=True, timeout=3)
sys.stdout.write("SERIAL: opened {0} @ {1}\n".format(portname, baud))
serial_thread = threading.Thread(target=serial_reader, args=[])
serial_thread.start()
def allstop():
global serial_reader_run
sys.stdout.write("Allstop called\n")
serial_reader_run = False
serial_thread.join()
port.close()
def ehook(type, value, traceback):
sys.stderr.write("I found an ehook!\n")
if type is SystemExit:
allstop()
sys.__excepthook__(type, value, traceback)
else:
sys.__excepthook__(type, value, traceback)
sys.excepthook = ehook
def sendmagic():
if port.is_open:
port.write(bytearray(magicpacket))
else:
sys.stderr.write("SERIAL: Port cannot write as it is closed\n")