-
Notifications
You must be signed in to change notification settings - Fork 2
/
udp_wrapper.py
66 lines (57 loc) · 1.97 KB
/
udp_wrapper.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
#!/usr/bin/python3
import sys
import serial
import socket
import queue
import threading
import time
from binascii import hexlify, unhexlify
UDP_IP = "quic.tech"
UDP_PORT = 4433
UDP_PAIR = (UDP_IP, UDP_PORT)
def run_thread(q, sock):
while True:
try:
data, server_addr = sock.recvfrom(1500)
print("Net << ({}) {}".format(len(data), data.hex()))
q.put(data)
except socket.timeout:
print("Net << timeout")
time.sleep(0.5)
def main():
q = queue.SimpleQueue()
port = sys.argv[1]
print("Opening serial port {}".format(port))
port = serial.Serial(port, 115200, timeout=0.5, write_timeout=10, rtscts=True)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(1.0)
port.read()
port.write("\rquiche SERIAL\r".encode("utf-8"))
t = threading.Thread(target=run_thread, args=(q, sock), daemon=True)
print("Starting thread...")
t.start()
print("Main loop...")
while True:
line = port.readline().decode("utf-8").strip()
if line:
print("Port << {!r}".format(line))
if line.startswith("TX"):
data_len = int(line[2:6], 16)
data = unhexlify(line[6:])
if data_len != len(data):
raise Exception("Bad data length {} != {}".format(
data_len, len(data)))
print("Net >> {}".format(data.hex()))
sock.sendto(data, UDP_PAIR)
elif line == "?":
try:
print("Queue pend...")
data = q.get(block=True, timeout=0.1)
print("Queue gave {} byte packet".format(len(data)))
msg = "{:04x}{}".format(len(data), data.hex())
print("Port << {!r}".format(msg))
port.write(msg.encode("ascii"))
except queue.Empty:
pass
if __name__ == '__main__':
main()