Skip to content

Commit 09a3efd

Browse files
committed
feat: enhance serial monitor functionality with UTF-8 decoding options
1 parent c8d3388 commit 09a3efd

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/wokwi_client/client.py

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

210-
async def serial_monitor_cat(self) -> None:
210+
async def serial_monitor_cat(self, decode_utf8: bool = True, errors: str = "replace") -> None:
211211
"""
212212
Print serial monitor output to stdout as it is received from the simulation.
213+
214+
Args:
215+
decode_utf8: Whether to decode bytes as UTF-8. If False, prints raw bytes (default: True).
216+
errors: How to handle UTF-8 decoding errors. Options: 'strict', 'ignore', 'replace' (default: 'replace').
213217
"""
214218
async for line in monitor_lines(self._transport):
215-
print(line.decode("utf-8"), end="", flush=True)
219+
if decode_utf8:
220+
try:
221+
output = line.decode("utf-8", errors=errors)
222+
print(output, end="", flush=True)
223+
except UnicodeDecodeError:
224+
# Fallback to raw bytes if decoding fails completely
225+
print(line, end="", flush=True)
226+
else:
227+
print(line, end="", flush=True)
216228

217229
async def serial_write(self, data: typing.Union[bytes, str, list[int]]) -> None:
218230
"""Write data to the simulation serial monitor interface."""

src/wokwi_client/client_sync.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,13 @@ def restart_simulation(self, pause: bool = False) -> t.Any:
141141
assert self._client is not None
142142
return self._run_async(self._client.restart_simulation(pause))
143143

144-
def serial_monitor_cat(self) -> t.Any:
144+
def serial_monitor_cat(self, decode_utf8: bool = True, errors: str = "replace") -> t.Any:
145145
if not self._connected:
146146
raise RuntimeError("Client not connected")
147147
assert self._client is not None
148-
return self._run_async(self._client.serial_monitor_cat())
148+
return self._run_async(
149+
self._client.serial_monitor_cat(decode_utf8=decode_utf8, errors=errors)
150+
)
149151

150152
def write_serial(self, data: bytes | str | list[int]) -> t.Any:
151153
if not self._connected:

0 commit comments

Comments
 (0)