-
Notifications
You must be signed in to change notification settings - Fork 1
API
Returns whether the browser supports the Web Serial API.
Note: Even in compatible browsers, support will not be available on insecure origins. Websites/applications need to be served using HTTPS to use Web Serial.
Note: readonly properties are not enforced at runtime or by the typings, but modification may lead to undefined behavior.
- type:
boolean
- readonly
Whether or not the serial port is currently open.
- type:
SerialPortInfo
- readonly
An object containing the serial port's info including USB device ID and vendor ID. This is the getInfo()
of the WebSerial
's port.
- type:
SerialPort
- readonly
The browser native SerialPort
wrapped by the WebSerial
object.
Prompt the user to select a port using the browser's native mechanism. Emits portavailable
on success and requesterror
if user cancels or no port is available.
Important: this user will only be prompted to choose a serial port if this method is called as a result of a user-triggered event, e.g., a button click.
- optional (default:
[]
) - type:
SerialPortFilter[]
Vender/device ID filters to apply on the port options offered to the user. By default all devices are shown.
- type:
Promise<void>
Resolves once the user makes a selection or cancels the request.
Select a port from the ports the user has previously granted access to. This method can be called without user interaction. Emits portavailable
on success and noport
if no port is available.
The choosePort
callback will be used to allow the client to choose which port to use. By default, the first port returned from the browser API will be used. If no port is available, the callback will not be called.
- optional
- type:
(ports: SerialPort[]) => SerialPort
Used to control which port is used by this WebSerial
.
- type:
Promise<void>
Resolves once a port (if available) has been chosen.
Opens the serial port. Emits openerror
if no serial port is selected, if the port is already opened or in the process of opening, or if another error occurs while opening the port. Otherwise, emits open
when the port is ready.
- optional (default:
{baudRate: 9600}
) - type:
SerialOptions
: an object with any of the following values:-
baudRate
: A positive, non-zero value indicating the baud rate at which serial communication should be established. - optional
bufferSize
: An unsigned long integer indicating the size of the read and write buffers that are to be established. If not passed, defaults to 255. - optional
dataBits
: An integer value of 7 or 8 indicating the number of data bits per frame. If not passed, defaults to 8. - optional
flowControl
: The flow control type, either"none"
or"hardware"
. If not passed, defaults to"none"
. - optional
parity
: The parity mode, either"none"
,"even"
, or"odd"
. If not passed, defaults to"none"
. - optional
stopBits
: An integer value of 1 or 2 indicating the number of stop bits at the end of the frame. If not passed, defaults to 1.
-
- type:
Promise<void>
Resolves when the operation is done.
Close the serial port.
Reads the next byte received over serial.
- type:
number
The next byte as a number, or -1
if no bytes are available.
Reads the next byte received over serial as a character.
- type:
string
The next byte as a single character, or null
if no bytes are available
Reads all data currently available over serial.
- type:
Uint8Array
The full contents of the serial receive buffer.
Reads bytes until a certain character is found.
- type:
string
Character to read bytes until. The byte corresponding to this character will not included in the result.
- optional (default value:
false
) - type:
boolean
Dictates the behavior if the given character is not found. If true
, the entire buffer will be returned; if false
, null
will be returned.
- type:
Uint8Array
The serial data received until the given character, or null
if the character was not found and returnAllIfNotFound
is false
or not provided. Note the distinction between null
(the character was not found) and an empty Uint8Array
(the character was the first byte in the buffer)
Reads the entire buffer as a string.
- type:
string
The full contents of the serial receive buffer.
Reads the buffer as a string until a certain string is found.
- type:
string
The string to find in the buffer. The bytes corresponding to this string will not be included in the result.
- optional (default value:
false
) - type:
boolean
Dictates the behavior if the given character is not found. If true
, the entire buffer will be returned as a string; if false
, null
will be returned.
- type:
string
The serial data received until the given string, or null
if the string was not found and returnAllIfNotFound
is false
or not provided. Note the distinction between null
(the string was not found) and ""
(the string was at the start of the buffer).
Reads a string until a line ending is found. The line ending will not be included in the result.
The next line available in the buffer. If no line ending was found, null
.
The number of bytes available in the buffer.
Clears the input buffer without reading it.
Write data to the serial port.
Emits writeerror
if an error occurs.
- type:
number
|number[]
|string
|Uint8Array
Data to be written to the serial port. If string
, encoded using UTF-8; otherwise, sent directly as bytes.
- type:
Promise<void>
Resolves once the write is complete.
Synonym of write()
.
- type:
string
Writes data to the serial port, along with a line ending.
- type:
string
Set the line ending (usually \r\n
or \n
).
- type:
string
The line ending to use.
Add an event listener.
- type:
"noport" | "portavailable" | "open" | "close" | "error" | "requesterror" | "openerror" | "closeerror" | "readerror" | "writeerror" | "data"
The name of the event to listen for
- type:
(detail: any, stopPropagation: () => void) => void
The listener for the event.
Removes an event listener.