Skip to content

Azq2/node-serialport-bindings-webserial

Repository files navigation

SUMMARY

NPM Version

WebSerial bindings for serialport module.

INSTALL

npm i serialport-bindings-webserial
yarn add serialport-bindings-webserial
pnpm add serialport-bindings-webserial

EXAMPLES

  1. List available and paired ports:

    import { WebSerialBinding } from 'serialport-bindings-webserial';
    
    const ports = await WebSerialBinding.list();
    console.log(ports);
  2. Open any port:

    The browser will show pop-up with all available ports.

    import { WebSerialBinding, WebSerialBindingInterface } from 'serialport-bindings-webserial';
    import { SerialPortStream } from '@serialport/stream';
    
    const port = new SerialPortStream<WebSerialBindingInterface>({
        binding: WebSerialBinding,
        path: 'webserial://any',
        baudRate: 115200
    });
  3. Open any port with custom options for SerialPort.open:

    The browser will show pop-up with all available ports.

    import { WebSerialBinding, WebSerialBindingInterface } from 'serialport-bindings-webserial';
    import { SerialPortStream } from '@serialport/stream';
    
    const port = new SerialPortStream<WebSerialBindingInterface>({
        binding: WebSerialBinding,
        path: 'webserial://any',
        baudRate: 115200,
        webSerialOpenOptions: {
            bufferSize: 4 * 1024 // https://developer.mozilla.org/en-US/docs/Web/API/SerialPort/open#buffersize
        }
    }); 
  4. Open any port with filters:

    The browser will show pop-up with all available ports which fit the requested filter.

    import { WebSerialBinding, WebSerialBindingInterface } from 'serialport-bindings-webserial'; 
    import { SerialPortStream } from '@serialport/stream';
    
    const port = new SerialPortStream<WebSerialBindingInterface>({
       binding: WebSerialBinding,
       path: 'webserial://any',
       baudRate: 115200,
       webSerialRequestOptions: {
           filters: [{ usbVendorId: 0x067B, usbProductId: 0x2303 }]
       }
    });
  5. Open with native SerialPort:

    You can open a port using reference to the native SerialPort object.

    import { WebSerialBinding, WebSerialBindingInterface } from 'serialport-bindings-webserial'; 
    import { SerialPortStream } from '@serialport/stream';
    
    const webSerialPort = await navigator.serial.requestPort();
    
    const port = new SerialPortStream({
       binding: WebSerialBinding,
       path: 'webserial://any',
       baudRate: 115200,
       webSerialPort
    });
  6. Open port by virtual path:

    import { WebSerialBinding, WebSerialBindingInterface } from 'serialport-bindings-webserial'; 
    import { SerialPortStream } from '@serialport/stream';
    
    const ports = await WebSerialBinding.list();
    
    const port = new SerialPortStream({
       binding: WebSerialBinding,
       path: ports[0].path, // for example: webserial://usb0
       baudRate: 115200,
    });

NOTES

You need something like vite-plugin-node-polyfills or node-stdlib-browser for using the serialport module in the browser.

About

High quality WebSerial bindings for serialport.

Resources

License

Stars

Watchers

Forks

Packages

No packages published