web-serial-rxjs API Documentation
    Preparing search index...

    Interface SerialClient

    SerialClient interface for interacting with serial ports using RxJS Observables.

    This interface provides a reactive API for serial port communication, allowing you to connect to serial devices, read and write data using RxJS Observables.

    const client = createSerialClient({ baudRate: 9600 });

    // Connect to a port
    client.connect().subscribe({
    next: () => {
    console.log('Connected!');

    // Read data
    client.text$.subscribe({
    next: (text) => console.log('Received:', text),
    });

    // Write data
    const encoder = new TextEncoder();
    client.write(encoder.encode('Hello')).subscribe();
    },
    error: (error) => console.error('Connection error:', error),
    });
    interface SerialClient {
        bytes$: Observable<Uint8Array<ArrayBufferLike>>;
        connected: boolean;
        connected$: Observable<boolean>;
        currentPort: SerialPort | null;
        errors$: Observable<SerialError>;
        lines$: Observable<string>;
        state$: Observable<SerialState>;
        text$: Observable<string>;
        command$(
            command: string,
            options?: SerialCommandOptions,
        ): Observable<CommandResult>;
        connect(port?: SerialPort): Observable<void>;
        disconnect(): Observable<void>;
        getPorts(): Observable<SerialPort[]>;
        requestPort(): Observable<SerialPort>;
        send$(data: string | Uint8Array<ArrayBufferLike>): Observable<void>;
        support(): SerialSupport;
        transact$<T>(request: SerialRequest<T>): Observable<T>;
        write(data: Uint8Array): Observable<void>;
        writeText(data: string): Observable<void>;
    }
    Index

    Properties

    bytes$: Observable<Uint8Array<ArrayBufferLike>>

    Get an Observable that emits received byte chunks.

    This stream is driven by an internal read pump and becomes active after connect.

    An Observable that emits Uint8Array chunks from the serial port

    connected: boolean

    Check if the port is currently open and connected.

    true if a port is currently open, false otherwise

    connected$: Observable<boolean>

    Reactive connection state stream.

    Emits true when connected and false when disconnected.

    currentPort: SerialPort | null

    Get the current SerialPort instance.

    Returns the currently connected SerialPort instance, or null if no port is open. This allows direct access to the underlying Web Serial API SerialPort object if needed.

    The current SerialPort instance, or null if no port is open

    errors$: Observable<SerialError>

    Reactive error stream.

    Emits all SerialError instances produced by this client.

    lines$: Observable<string>

    Get an Observable that emits newline-delimited text lines.

    Lines are emitted without trailing newline characters.

    An Observable that emits parsed line strings

    state$: Observable<SerialState>

    Reactive serial state stream.

    Emits detailed connection lifecycle and error states.

    text$: Observable<string>

    Get an Observable that emits decoded text chunks.

    This stream decodes bytes with TextDecoder in streaming mode.

    An Observable that emits decoded text chunks

    Methods

    • Disconnect from the serial port.

      Closes the currently open port and stops all active read/write streams. This method is safe to call even if no port is currently open.

      Returns Observable<void>

      An Observable that completes when the port is successfully closed

      SerialError with code SerialErrorCode.CONNECTION_LOST if closing the port fails

      client.disconnect().subscribe({
      next: () => console.log('Disconnected'),
      error: (error) => console.error('Disconnect failed:', error),
      });
    • Get available serial ports that have been previously granted access.

      This method returns an Observable that emits an array of SerialPort instances that the user has previously granted access to in this browser session.

      Returns Observable<SerialPort[]>

      An Observable that emits an array of available SerialPort instances

      SerialError with code SerialErrorCode.PORT_NOT_AVAILABLE if getting ports fails

      SerialError with code SerialErrorCode.BROWSER_NOT_SUPPORTED if the browser doesn't support Web Serial API

      client.getPorts().subscribe({
      next: (ports) => {
      console.log(`Found ${ports.length} available ports`);
      if (ports.length > 0) {
      client.connect(ports[0]).subscribe();
      }
      },
      });
    • Write a single chunk of data to the serial port.

      Writes a single Uint8Array chunk to the serial port immediately. For queued ordering semantics, prefer send$.

      Parameters

      • data: Uint8Array

        Uint8Array data to write to the serial port

      Returns Observable<void>

      An Observable that completes when the data has been written

      SerialError with code SerialErrorCode.PORT_NOT_OPEN if the port is not open

      SerialError with code SerialErrorCode.WRITE_FAILED if writing fails

      const encoder = new TextEncoder();
      const data = encoder.encode('Hello, Serial!');

      client.write(data).subscribe({
      next: () => console.log('Data written'),
      error: (error) => console.error('Write error:', error),
      });