Skip to content

Commit bd1bbb8

Browse files
committed
Throw exception if not metadata is read
1 parent 7ea7b69 commit bd1bbb8

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/ppk2_api.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,28 @@ def _read_metadata(self):
127127
read = self.ser.read(self.ser.in_waiting)
128128
time.sleep(0.1)
129129

130+
# TODO add a read_until serial read function with a timeout
130131
if read != b'' and "END" in read.decode("utf-8"):
131132
return read.decode("utf-8")
132133

133134
def _parse_metadata(self, metadata):
134135
"""Parse metadata and store it to modifiers"""
135-
data_split = [row.split(": ") for row in metadata.split("\n")]
136-
137-
for key in self.modifiers.keys():
138-
for data_pair in data_split:
139-
if key == data_pair[0]:
140-
self.modifiers[key] = data_pair[1]
141-
for ind in range(0, 5):
142-
if key+str(ind) == data_pair[0]:
143-
self.modifiers[key][str(ind)] = float(data_pair[1])
136+
# TODO handle more robustly
137+
try:
138+
data_split = [row.split(": ") for row in metadata.split("\n")]
139+
140+
for key in self.modifiers.keys():
141+
for data_pair in data_split:
142+
if key == data_pair[0]:
143+
self.modifiers[key] = data_pair[1]
144+
for ind in range(0, 5):
145+
if key+str(ind) == data_pair[0]:
146+
self.modifiers[key][str(ind)] = float(data_pair[1])
147+
148+
return True
149+
except Exception as e:
150+
# if exception triggers serial port is probably not correct
151+
return None
144152

145153
def _generate_mask(self, bits, pos):
146154
pos = pos
@@ -172,7 +180,8 @@ def get_modifiers(self):
172180
"""Gets and sets modifiers from device memory"""
173181
self._write_serial((PPK2_Command.GET_META_DATA, ))
174182
metadata = self._read_metadata()
175-
self._parse_metadata(metadata)
183+
ret = self._parse_metadata(metadata)
184+
return ret
176185

177186
def start_measuring(self):
178187
"""Start continous measurement"""

0 commit comments

Comments
 (0)