-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswrite.py
executable file
·168 lines (164 loc) · 4.47 KB
/
swrite.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
from __future__ import print_function
import serial
import time
from sys import argv
from sys import exit
from sys import stderr
from sys import stdin
def help():
print("usage: swrite [<options>] <device> [<input>]", file=stderr)
print(" -u <path> serial device to write to", file=stderr)
print(" -b <int> baud rate", file=stderr)
print(" -d <int> data bits (5, 6, 7, 8)", file=stderr)
print(" -p <str> parity (none, even, odd, mark, space)", file=stderr)
print(" -s <num> stop bits (1, 1.5, 2)", file=stderr)
print(" -t <num> timeout (seconds)", file=stderr)
print(" -x enable XON/XOFF", file=stderr)
print(" -r enable RTS/CTS", file=stderr)
print(" -h enable DSR/DTR", file=stderr)
print(" -i <path> path to read from", file=stderr)
print(" -w <num> delay before writing (seconds)", file=stderr)
print(" -l <int> number of bytes to write", file=stderr)
def main():
port = None
baudrate = 9600
bytesize = serial.EIGHTBITS
parity = serial.PARITY_NONE
stopbits = serial.STOPBITS_ONE
timeout = None
xonxoff = False
rtscts = False
dsrdtr = False
input = None
delay = None
length = None
i = 1
while i < len(argv):
arg = argv[i]
i += 1
if arg == "--help":
help()
exit()
elif arg == "-u" and i < len(argv):
port = argv[i]
i += 1
elif arg == "-b" and i < len(argv):
try:
baudrate = int(argv[i])
except:
print(argv[i] + " is not valid for -b", file=stderr)
exit()
i += 1
elif arg == "-d" and i < len(argv):
if argv[i] == "5":
bytesize = serial.FIVEBITS
elif argv[i] == "6":
bytesize = serial.SIXBITS
elif argv[i] == "7":
bytesize = serial.SEVENBITS
elif argv[i] == "8":
bytesize = serial.EIGHTBITS
else:
print(argv[i] + " is not valid for -d", file=stderr)
exit()
i += 1
elif arg == "-p" and i < len(argv):
if argv[i] == "N" or argv[i] == "n" or argv[i] == "none":
parity = serial.PARITY_NONE
elif argv[i] == "E" or argv[i] == "e" or argv[i] == "even":
parity = serial.PARITY_EVEN
elif argv[i] == "O" or argv[i] == "o" or argv[i] == "odd":
parity = serial.PARITY_ODD
elif argv[i] == "M" or argv[i] == "m" or argv[i] == "mark":
parity = serial.PARITY_MARK
elif argv[i] == "S" or argv[i] == "s" or argv[i] == "space":
parity = serial.PARITY_SPACE
else:
print(argv[i] + " is not valid for -p", file=stderr)
exit()
i += 1
elif arg == "-s" and i < len(argv):
if argv[i] == "1":
stopbits = serial.STOPBITS_ONE
elif argv[i] == "1.5":
stopbits = serial.STOPBITS_ONE_POINT_FIVE
elif argv[i] == "2":
stopbits = serial.STOPBITS_TWO
else:
print(argv[i] + " is not valid for -s", file=stderr)
exit()
i += 1
elif arg == "-t" and i < len(argv):
try:
timeout = float(argv[i])
except:
print(argv[i] + " is not valid for -t", file=stderr)
exit()
i += 1
elif arg == "-x":
xonxoff = True
elif arg == "-X":
xonxoff = False
elif arg == "-r":
rtscts = True
elif arg == "-R":
rtscts = False
elif arg == "-h":
dsrdtr = True
elif arg == "-H":
dsrdtr = False
elif arg == "-i" and i < len(argv):
input = argv[i]
i += 1
elif arg == "-w" and i < len(argv):
try:
delay = float(argv[i])
except:
print(argv[i] + " is not valid for -w", file=stderr)
exit()
i += 1
elif arg == "-l" and i < len(argv):
try:
length = int(argv[i])
except:
print(argv[i] + " is not valid for -l", file=stderr)
exit()
i += 1
elif port is None:
port = arg
elif input is None:
input = arg
else:
help()
exit()
if port is None:
help()
else:
try:
if input is None:
with serial.Serial(port=port, baudrate=baudrate, bytesize=bytesize,
parity=parity, stopbits=stopbits, timeout=timeout,
xonxoff=xonxoff, rtscts=rtscts, dsrdtr=dsrdtr) as ser:
if delay is not None:
time.sleep(delay)
i = 0
while length is None or i < length:
b = stdin.read(1)
ser.write(b)
i += len(b)
else:
with open(input, "rb") as f:
with serial.Serial(port=port, baudrate=baudrate, bytesize=bytesize,
parity=parity, stopbits=stopbits, timeout=timeout,
xonxoff=xonxoff, rtscts=rtscts, dsrdtr=dsrdtr) as ser:
if delay is not None:
time.sleep(delay)
i = 0
while length is None or i < length:
b = f.read(1)
ser.write(b)
i += len(b)
except KeyboardInterrupt:
pass
if __name__ == "__main__": main()