-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpidtrace.py
More file actions
24 lines (20 loc) · 743 Bytes
/
pidtrace.py
File metadata and controls
24 lines (20 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import serial
import struct
from datetime import datetime
with serial.Serial('/dev/ttyUSB0', 115200, timeout=2) as ser:
with open("trace.csv", "w") as f:
fmt = '@ffi'
lbl = ["temp", "setPoint", "pwm"]
f.write(",".join(["time"] + lbl) + "\n")
while 1:
s = ser.read(64)
b = bytes(s)
part = b[:struct.calcsize(fmt)]
print(part)
unpacked = struct.unpack(fmt, part)
data = dict()
for i in range(len(unpacked)):
data[lbl[i]] = unpacked[i]
now = datetime.now()
f.write("{},{},{},{}\n".format(now.strftime("%H:%M:%S"), data["temp"], data["setPoint"], data["pwm"]))
f.flush()