Skip to content

Write python strings #1041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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