Skip to content

Commit

Permalink
core.py: Enable correct exporting of char data
Browse files Browse the repository at this point in the history
As the data type for saving the char in pyulog was np.int8, the topics
that had fields with characters weren't able to get encoded (with error:
pyulog struct.error: char format requires a bytes object of length 1).

Therefore, this enables manual conversion of np.int8 object into bytes()
with single character, so that struct.pack command works as expected.

Notable example of where this is needed is the "transponder_report"
topic (included in PX4 v1.14)'s callsign[] character array. Before this
fix, the "write_ulog()" function would fail as the struct.pack couldn't
correctly encode the characters stored in int8 type
  • Loading branch information
junwoo091400 committed Apr 17, 2024
1 parent 85c4f3e commit 30b32ec
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pyulog/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ def _make_data_items(self):
field_type = field.type_str
field_encoding = self._UNPACK_TYPES[field_type][0]
field_data = data_set.data[field_name][i_sample]

# For char type, convert np.int8 into single bytes() object so that struct.pack can handle it
if field_encoding == 'c':
field_data = bytes(chr(field_data), 'utf-8')

data.extend(struct.pack('<' + field_encoding, field_data))

header = struct.pack('<HB', len(data), self.MSG_TYPE_DATA)
Expand Down

0 comments on commit 30b32ec

Please sign in to comment.