-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildJSON.py
38 lines (29 loc) · 1.01 KB
/
buildJSON.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
import serial
serialPort = serial.Serial('/dev/ttyUSB0');
dataToPrint = []
quote = "\""
def checkPacketValidity(packet):
for x in packet:
if not (x.isdigit() or x=="." or x=="," or x=="\n"):
return false
return true
accepted = lambda x: x!="<" and x!=">"
while (73):
telemetryPackage = serialPort.readline() #waits indefinetly for the next packet
telemetryPackage = "".join(list(filter(accepted,telemetryPackage)))
if not checkPacketValidity(telemetryPackage[:-2]):
continue
telemetryPackage = quote + telemetryPackage[:-2] + quote
#add "," to current last element.
if (len(dataToPrint) > 0):
dataToPrint[len(dataToPrint)-1] += ","
#add new package.
dataToPrint += [telemetryPackage]
#write to file.
file = open("telemetry-data-plotting/public/TelemetryData.json","w")
file.write("[\n")
for i in dataToPrint:
file.write(i)
file.write("\n")
file.write("]")
file.close()