Skip to content

Commit 538191a

Browse files
committed
feat: enhance serial monitor functionality with write support and update documentation
1 parent 6f068f3 commit 538191a

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ The basic example is in the [examples/hello_esp32/main.py](examples/hello_esp32/
3232
- Connect to the Wokwi Simulator
3333
- Upload a diagram and firmware files
3434
- Start a simulation
35-
- Monitor the serial output
35+
- Monitor the serial output and write to them
36+
- Read GPIO pins
37+
- Control peripherals (buttons, MPU6050, etc.)
3638

3739
You can run the example with:
3840

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Typed, asyncio-friendly Python SDK for the **Wokwi Simulation API**.
77
- Connect to the Wokwi Simulator from Python
88
- Upload diagrams and firmware files
99
- Start, pause, resume, and restart simulations
10-
- Monitor serial output asynchronously
10+
- Monitor serial output asynchronously and write to them
1111
- Control peripherals and read GPIO pins
1212
- Fully type-annotated and easy to use with asyncio
1313

src/wokwi_client/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .file_ops import upload, upload_file
1313
from .pins import pin_listen, pin_read
1414
from .protocol_types import EventMessage, ResponseMessage
15-
from .serial import monitor_lines
15+
from .serial import monitor_lines, write_serial
1616
from .simulation import pause, restart, resume, start
1717
from .transport import Transport
1818

@@ -181,6 +181,10 @@ async def serial_monitor_cat(self) -> None:
181181
async for line in monitor_lines(self._transport):
182182
print(line.decode("utf-8"), end="", flush=True)
183183

184+
async def serial_write(self, data: bytes | str | list[int]) -> None:
185+
"""Write data to the simulation serial monitor interface."""
186+
await write_serial(self._transport, data)
187+
184188
def _on_pause(self, event: EventMessage) -> None:
185189
self.last_pause_nanos = int(event["nanos"])
186190

src/wokwi_client/serial.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
from collections.abc import AsyncGenerator
5+
from collections.abc import AsyncGenerator, Iterable
66

77
from .event_queue import EventQueue
88
from .transport import Transport
@@ -14,3 +14,17 @@ async def monitor_lines(transport: Transport) -> AsyncGenerator[bytes, None]:
1414
while True:
1515
event_msg = await queue.get()
1616
yield bytes(event_msg["payload"]["bytes"])
17+
18+
19+
async def write_serial(transport: Transport, data: bytes | str | Iterable[int]) -> None:
20+
"""Write data to the serial monitor.
21+
22+
Accepts bytes, str (encoded as utf-8), or an iterable of integer byte values.
23+
"""
24+
if isinstance(data, str):
25+
payload = list(data.encode("utf-8"))
26+
elif isinstance(data, bytes):
27+
payload = list(data)
28+
else:
29+
payload = list(int(b) & 0xFF for b in data)
30+
await transport.request("serial-monitor:write", {"bytes": payload})

0 commit comments

Comments
 (0)