-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializeHearbeat.py
More file actions
39 lines (31 loc) · 1.12 KB
/
serializeHearbeat.py
File metadata and controls
39 lines (31 loc) · 1.12 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
import os
import serial
import time
import json
# load config file (same directory as this file)
configFile = open(os.path.dirname(os.path.abspath(__file__)) + '/../../config.json')
config = json.load(configFile)
print("Opening port " + str(config["serial_port"]) + " with baudrate " + str(config["serial_baudrate"]) + ".")
# open serial port
sensorcube = serial.Serial(port=config["serial_port"], baudrate=config["serial_baudrate"])
# enable only heartbeat message
command = '{"messages":["heartbeat"]}\r\n';
sensorcube.write(bytes(command, 'utf-8'))
while True:
try:
# read one line from serial and parse json
try:
line = sensorcube.readline().decode('utf-8').rstrip()
data = json.loads(line)
except:
continue
if not "msg" in data:
continue
# process heartbeat messages
if data["msg"] == "heartbeat":
stamp = data["stamp"]
seq = data["seq"]
print("Received heartbeat at time " + str(stamp) + " with sequence number " + str(seq) + ".")
time.sleep(1)
except KeyboardInterrupt:
break