Skip to content

Commit

Permalink
Encode python strings before writing
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthomas committed Jan 11, 2024
1 parent dfcdfbe commit 2407c83
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,6 @@ TELEMETRY <%= target_name %> MECH BIG_ENDIAN "Mechanism status"
UNITS DEGREES DEG
APPEND_ITEM CURRENT 32 FLOAT "Device current"
UNITS micro-Ampères µA
APPEND_ITEM STRING 0 STRING "String"
ITEM PACKET_TIME 0 0 DERIVED "Ruby time based on TIMESEC and TIMEUS"
READ_CONVERSION openc3/conversions/unix_time_conversion.py TIMESEC TIMEUS
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ def read(self, count_100hz, time):
packet.write("slrpnl4", self.solar_panel_positions[3])
packet.write("slrpnl5", self.solar_panel_positions[4])
packet.write("current", 0.5)
packet.write("string", f"Time is {time}")

# Every 10s throw an unknown packet at the server just to demo that
if count_100hz % 1000 == 900:
Expand Down
5 changes: 4 additions & 1 deletion openc3/python/openc3/accessors/binary_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,10 @@ def write(
#######################################

if cls.byte_aligned(bit_offset):
temp = value
if data_type == "STRING" and type(value) == str:
temp = value.encode(encoding="utf-8")
else:
temp = value
if given_bit_size <= 0:
end_bytes = -math.floor(given_bit_size / 8)
old_upper_bound = len(buffer) - 1 - end_bytes
Expand Down
12 changes: 12 additions & 0 deletions openc3/python/test/accessors/test_binary_accessor_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,18 @@ def test_writes_a_shorter_string_with_zero_bit_size(self):
)
self.assertEqual(self.data, b"\x00\x00\x00\x00\x00\x00\x00\x00")

def test_writes_a_string_with_zero_bit_size(self):
BinaryAccessor.write(
"This is a test",
0,
0,
"STRING",
self.data,
"BIG_ENDIAN",
"ERROR",
)
self.assertEqual(self.data, b"This is a test")

def test_writes_a_shorter_block_with_zero_bit_size(self):
BinaryAccessor.write(
b"\x00\x00\x00\x00\x00\x00\x00\x00",
Expand Down

0 comments on commit 2407c83

Please sign in to comment.