Skip to content

Commit 3008937

Browse files
feat: enhance serial monitor functionality with UTF-8 decoding options (#3)
1 parent f85c8e5 commit 3008937

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/wokwi_client/client.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,24 @@ async def restart_simulation(self, pause: bool = False) -> ResponseMessage:
172172
"""
173173
return await restart(self._transport, pause)
174174

175-
async def serial_monitor_cat(self) -> None:
175+
async def serial_monitor_cat(self, decode_utf8: bool = True, errors: str = "replace") -> None:
176176
"""
177177
Print serial monitor output to stdout as it is received from the simulation.
178+
179+
Args:
180+
decode_utf8: Whether to decode bytes as UTF-8. If False, prints raw bytes (default: True).
181+
errors: How to handle UTF-8 decoding errors. Options: 'strict', 'ignore', 'replace' (default: 'replace').
178182
"""
179183
async for line in monitor_lines(self._transport):
180-
print(line.decode("utf-8"), end="", flush=True)
184+
if decode_utf8:
185+
try:
186+
output = line.decode("utf-8", errors=errors)
187+
print(output, end="", flush=True)
188+
except UnicodeDecodeError:
189+
# Fallback to raw bytes if decoding fails completely
190+
print(line, end="", flush=True)
191+
else:
192+
print(line, end="", flush=True)
181193

182194
def _on_pause(self, event: EventMessage) -> None:
183195
self.last_pause_nanos = int(event["nanos"])

0 commit comments

Comments
 (0)