WebSerial bindings for serialport module.
npm i serialport-bindings-webserial
yarn add serialport-bindings-webserial
pnpm add serialport-bindings-webserial
-
List available and paired ports:
import { WebSerialBinding } from 'serialport-bindings-webserial'; const ports = await WebSerialBinding.list(); console.log(ports);
-
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 });
-
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 } });
-
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 }] } });
-
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 });
-
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, });
You need something like vite-plugin-node-polyfills
or node-stdlib-browser
for using the serialport
module in the browser.