From d23cb8fc239c5872fc3663ddebe69ffa66dd685f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herman=20=C3=98ie=20Kolden?= Date: Thu, 4 Jul 2024 09:05:17 +0200 Subject: [PATCH] core.py: Enable correct exporting of char data 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 --- pyulog/core.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pyulog/core.py b/pyulog/core.py index c56ad00..48489e4 100644 --- a/pyulog/core.py +++ b/pyulog/core.py @@ -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('