Skip to content
Back to top
View this page
Edit this page

# Pyodide (Web Serial)

Pyodide serial port implementation using the Web Serial API.

Under Pyodide this module registers a transport for the `pyodide://` URI scheme (and for scheme-less `device://` fallbacks), backed by the browser’s Web Serial API. Only the async transport `PyodideSerialTransport` is implemented; there is no synchronous `BaseSerial` equivalent because Web Serial is promise-based.

Because `navigator.serial.requestPort()` must be called in response to a user gesture from JavaScript, the JS `SerialPort` object cannot be obtained from Python. Instead, the host page obtains a `SerialPort` and hands it to Python via `register_js_port()`, associating it with a URI path. Subsequent calls to `serialx.open_serial_connection()` (or the lower-level transport) look the path up in the registry. A `SerialPort` may also be passed directly via the `js_port` keyword argument, bypassing the registry.

See Connecting to a Web Serial port under Pyodide for a more complete example.

class serialx.platforms.serial_pyodide.PyodideSerialTransport

  Bases: `BaseSerialTransport`

  Async serial transport for Pyodide using the Web Serial API.

  __init__(loop, protocol)

    Initialize the Pyodide serial transport.

    Warning

    The Web Serial API does not support software flow control (XON/XOFF). Passing `xonxoff=True` to `connect()` is accepted for compatibility but is silently ignored; a warning is logged. Only hardware flow control (RTS/CTS) is honored.

    Parameters:

      - loop (AbstractEventLoop)

      - protocol (Protocol)

    Return type:

      None

  write(data)

    Write data to the transport.

    Parameters:

      data (bytes | bytearray | memoryview)

    Return type:

      None

  get_write_buffer_size()

    Return the number of bytes currently queued for writing.

    Return type:

      int

  abort()

    Close the transport immediately, discarding pending writes.

    Return type:

      None

  close()

    Close the transport.

    Return type:

      None

serialx.platforms.serial_pyodide.register_js_port(path, js_port)

  Associate a URL with a JS SerialPort for later connection.

  Parameters:

    - path (str)

    - js_port (JsSerialPort)

  Return type:

    None

serialx.platforms.serial_pyodide.unregister_js_port(path)

  Remove the entry for path from the JS port registry, if any.

  Parameters:

    path (str)

  Return type:

    None