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

Co-authored-by: Junwoo Hwang <junwoo091400@gmail.com>
  • Loading branch information
hermankolden and junwoo091400 committed Jul 4, 2024
1 parent a84bf52 commit d23cb8f
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pyulog/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ 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 d23cb8f

Please sign in to comment.