-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharduino2nt.py
More file actions
71 lines (60 loc) · 1.83 KB
/
arduino2nt.py
File metadata and controls
71 lines (60 loc) · 1.83 KB
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
import serial
from serial.tools import list_ports
from networktables import NetworkTables
import config
import time
if config.SERVER_IP == 'default':
t0 = config.TEAM // 100
t1 = config.TEAM % 100
NetworkTables.initialize('10.{}.{}.2'.format(t0, t1))
else:
NetworkTables.initialize(config.SERVER_IP)
ports = list_ports.comports()
good_port = ''
for i, port in enumerate(ports):
print(i, ':', port.name)
if port.name in config.PORTS:
good_port = port.device
break
if good_port == '':
print('cant find ports listed in config.py, please check.')
exit(1)
ser = serial.Serial(good_port, baudrate=115200, timeout=1000)
table = NetworkTables.getTable('arduino2nt')
time.sleep(2) # wait for the arduino and the network table
all_values = {}
try:
while True:
while ser.inWaiting() > 0:
buffer = ser.read(3)
if buffer == b'AIN':
typ = buffer
buffer = ser.read(4)
assert buffer[3] == 10
pin = buffer[0]
value = buffer[1] * 256 + buffer[2]
key = 'A{}'.format(pin)
# print(typ, pin, value)
all_values[key] = value
table.putNumber(key, value)
elif buffer == b'DIN':
typ = buffer
buffer = ser.read(3)
assert buffer[2] == 10
pin = buffer[0]
value = buffer[1]
key = 'D{}'.format(pin)
# print(typ, pin, value)
all_values[key] = value
table.putBoolean(key, value)
else:
while ser.read() != b'\n':
pass
# print(all_values)
except KeyboardInterrupt:
ser.close()
print('exit')
except:
ser.close()
print('exit')
raise