-
Notifications
You must be signed in to change notification settings - Fork 1
/
sender.py
91 lines (70 loc) · 2.96 KB
/
sender.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
#!/usr/bin/python3
#pylint: disable=no-member
import argparse
import serial
endByte = b"a"
serialLogLabel = "[info from serial]"
def _log_nothing(*args, **kwargs):
pass
def sendData(data, serialPort, baudRate, simulate=False, log=_log_nothing):
if simulate:
log(serialLogLabel, "Setup")
else:
ser = serial.Serial(serialPort, baudRate)
log(serialLogLabel, ser.readline()[:-2].decode("utf8"))
try:
i = 0
while i < len(data):
mode = data[i:i+1]
x = int.from_bytes(data[i+1:i+3], byteorder="big", signed=True)
y = int.from_bytes(data[i+3:i+5], byteorder="big", signed=True)
log(f"[info] Sent: {repr(mode)[2:-1]:<2} x={x:>5} y={y:>5}")
if not simulate:
ser.write(data[i:i+5])
readData = ser.readline()
log(serialLogLabel, readData[:-2].decode("utf8"))
i+=5
except KeyboardInterrupt:
log("[info] Sending interrupted by user")
finally:
if simulate:
log("[info] Completed!")
else:
ser.write(endByte)
readData = ser.readline()[:-2].decode("utf8")
log(serialLogLabel, readData)
if (readData != "Completed!"):
log(serialLogLabel, readData)
readData = ser.readline()[:-2].decode("utf8")
def parseArgs(namespace):
argParser = argparse.ArgumentParser(fromfile_prefix_chars="@",
description="Send binary data to a plotter using a serial connection")
ioGroup = argParser.add_argument_group("Input/output options")
ioGroup.add_argument("-i", "--input", type=argparse.FileType('rb'), required=True, metavar="FILE",
help="Binary file from which to read the raw data to send to the plotter")
ioGroup.add_argument("-l", "--log", type=argparse.FileType('w'), required=False, metavar="FILE",
help="File in which to save logs, comments and warnings")
connGroup = argParser.add_argument_group("Plotter connectivity options")
connGroup.add_argument("--simulate", action="store_true",
help="Simulate sending data to the plotter without really opening a connection. Useful with logging enabled to debug the commands sent.")
connGroup.add_argument("--port", "--serial-port", type=str, metavar="PORT", dest="serial_port",
help="The serial port the plotter is connected to (required unless there is --simulate)")
connGroup.add_argument("--baud", "--baud-rate", type=int, metavar="RATE", dest="baud_rate",
help="The baud rate to use for the connection with the plotter. It has to be equal to the plotter baud rate. (required unless there is --simulate)")
argParser.parse_args(namespace=namespace)
if not namespace.simulate:
if namespace.serial_port is None:
argParser.error(f"--serial-port is required unless there is --simulate")
if namespace.baud_rate is None:
argParser.error(f"--baud-rate is required unless there is --simulate")
def main():
class Args: pass
parseArgs(Args)
def log(*args, **kwargs):
if Args.log is not None:
kwargs["flush"] = True
print(*args, **kwargs, file=Args.log)
data = Args.input.read()
sendData(data, Args.serial_port, Args.baud_rate, Args.simulate, log=log)
if __name__ == "__main__":
main()