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.getReadStream().subscribe({
    next: (data) => console.log('Received:', data),
    });

    // Write data
    const encoder = new TextEncoder();
    client.write(encoder.encode('Hello')).subscribe();
    },
    error: (error) => console.error('Connection error:', error),
    });
    interface SerialClient {
        connected: boolean;
        currentPort: SerialPort | null;
        connect(port?: SerialPort): Observable<void>;
        disconnect(): Observable<void>;
        getPorts(): Observable<SerialPort[]>;
        getReadStream(): Observable<Uint8Array<ArrayBufferLike>>;
        requestPort(): Observable<SerialPort>;
        write(data: Uint8Array): Observable<void>;
        writeStream(
            data$: Observable<Uint8Array<ArrayBufferLike>>,
        ): Observable<void>;
    }
    Index

    Properties

    connected: boolean

    Check if the port is currently open and connected.

    true if a port is currently open, false otherwise

    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

    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();
      }
      },
      });
    • Get an Observable that emits data read from the serial port.

      Returns an Observable stream that emits Uint8Array chunks as data is received from the serial port. The stream will continue until the port is disconnected or an error occurs.

      Returns Observable<Uint8Array<ArrayBufferLike>>

      An Observable that emits Uint8Array chunks containing data read from the serial port

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

      client.getReadStream().subscribe({
      next: (data) => {
      const text = new TextDecoder().decode(data);
      console.log('Received:', text);
      },
      error: (error) => console.error('Read error:', error),
      });
    • Write a single chunk of data to the serial port.

      Writes a single Uint8Array chunk to the serial port. For writing multiple chunks, consider using writeStream with an Observable instead.

      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),
      });
    • Write data to the serial port from an Observable.

      Writes data from an Observable stream to the serial port. The Observable should emit Uint8Array chunks that will be written sequentially to the port. If a previous write stream is active, it will be cancelled before starting the new one.

      Parameters

      • data$: Observable<Uint8Array<ArrayBufferLike>>

        Observable that emits Uint8Array chunks to write to the serial port

      Returns Observable<void>

      An Observable that completes when all data has been written and the stream completes

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

      SerialError with code SerialErrorCode.WRITE_FAILED if writing fails

      const data$ = from([
      new TextEncoder().encode('Hello'),
      new TextEncoder().encode('World'),
      ]);

      client.writeStream(data$).subscribe({
      next: () => console.log('Writing...'),
      complete: () => console.log('All data written'),
      error: (error) => console.error('Write error:', error),
      });