chrome.serial
- Description
Use the
chrome.serial
API to read from and write to a device connected to a serial port. - Permissions
serial
Summary
- Types
- Methods
chrome.serial.clearBreak(connectionId: number, callback: function)
chrome.serial.connect(path: string, options?: ConnectionOptions, callback: function)
chrome.serial.disconnect(connectionId: number, callback: function)
chrome.serial.flush(connectionId: number, callback: function)
chrome.serial.getConnections(callback: function)
chrome.serial.getControlSignals(connectionId: number, callback: function)
chrome.serial.getDevices(callback: function)
chrome.serial.getInfo(connectionId: number, callback: function)
chrome.serial.send(connectionId: number, data: ArrayBuffer, callback: function)
chrome.serial.setBreak(connectionId: number, callback: function)
chrome.serial.setControlSignals(connectionId: number, signals: HostControlSignals, callback: function)
chrome.serial.setPaused(connectionId: number, paused: boolean, callback: function)
chrome.serial.update(connectionId: number, options: ConnectionOptions, callback: function)
- Events
Types
ConnectionInfo
Properties
- bitratenumber optional
See
ConnectionOptions.bitrate
. This field may be omitted or inaccurate if a non-standard bitrate is in use, or if an error occurred while querying the underlying device. - bufferSizenumber
See
ConnectionOptions.bufferSize
- connectionIdnumber
The id of the serial port connection.
- ctsFlowControlboolean optional
See
ConnectionOptions.ctsFlowControl
. This field may be omitted if an error occurred while querying the underlying device. - dataBitsDataBits optional
See
ConnectionOptions.dataBits
. This field may be omitted if an error occurred while querying the underlying device. - namestring
See
ConnectionOptions.name
- parityBitParityBit optional
See
ConnectionOptions.parityBit
. This field may be omitted if an error occurred while querying the underlying device. - pausedboolean
Flag indicating whether the connection is blocked from firing onReceive events.
- persistentboolean
See
ConnectionOptions.persistent
- receiveTimeoutnumber
See
ConnectionOptions.receiveTimeout
- sendTimeoutnumber
See
ConnectionOptions.sendTimeout
- stopBitsStopBits optional
See
ConnectionOptions.stopBits
. This field may be omitted if an error occurred while querying the underlying device.
ConnectionOptions
Properties
- bitratenumber optional
The requested bitrate of the connection to be opened. For compatibility with the widest range of hardware, this number should match one of commonly-available bitrates, such as 110, 300, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200. There is no guarantee, of course, that the device connected to the serial port will support the requested bitrate, even if the port itself supports that bitrate.
9600
will be passed by default. - bufferSizenumber optional
The size of the buffer used to receive data. The default value is 4096.
- ctsFlowControlboolean optional
Flag indicating whether or not to enable RTS/CTS hardware flow control. Defaults to false.
- dataBitsDataBits optional
"eight"
will be passed by default. - namestring optional
An application-defined string to associate with the connection.
- parityBitParityBit optional
"no"
will be passed by default. - persistentboolean optional
Flag indicating whether or not the connection should be left open when the application is suspended (see Manage App Lifecycle). The default value is "false." When the application is loaded, any serial connections previously opened with persistent=true can be fetched with
getConnections
. - receiveTimeoutnumber optional
The maximum amount of time (in milliseconds) to wait for new data before raising an
onReceiveError
event with a "timeout" error. If zero, receive timeout errors will not be raised for the connection. Defaults to 0. - sendTimeoutnumber optional
The maximum amount of time (in milliseconds) to wait for a
send
operation to complete before calling the callback with a "timeout" error. If zero, send timeout errors will not be triggered. Defaults to 0. - stopBitsStopBits optional
"one"
will be passed by default.
DeviceControlSignals
Properties
- ctsboolean
CTS (Clear To Send).
- dcdboolean
DCD (Data Carrier Detect) or RLSD (Receive Line Signal/ Detect).
- dsrboolean
DSR (Data Set Ready).
- riboolean
RI (Ring Indicator).
DeviceInfo
Properties
- displayNamestring optional
A human-readable display name for the underlying device if one can be queried from the host driver.
- pathstring
The device's system path. This should be passed as the
path
argument tochrome.serial.connect
in order to connect to this device. - productIdnumber optional
A USB product ID if one can be determined for the underlying device.
- vendorIdnumber optional
A PCI or USB vendor ID if one can be determined for the underlying device.
HostControlSignals
Properties
- dtrboolean optional
DTR (Data Terminal Ready).
- rtsboolean optional
RTS (Request To Send).
ReceiveErrorInfo
Properties
- connectionIdnumber
The connection identifier.
- error
An error code indicating what went wrong.
ReceiveInfo
Properties
- connectionIdnumber
The connection identifier.
- dataArrayBuffer
The data received.
SendInfo
Properties
- bytesSentnumber
The number of bytes sent.
- errorSendError optional
An error code if an error occurred.
DataBits
Enum
"seven"
, or "eight"
ParityBit
Enum
"no"
, "odd"
, or "even"
ReceiveError
Enum
"disconnected"
, "timeout"
, "device_lost"
, "break"
, "frame_error"
, "overrun"
, "buffer_overflow"
, "parity_error"
, or "system_error"
SendError
Enum
"disconnected"
, "pending"
, "timeout"
, or "system_error"
StopBits
Enum
"one"
, or "two"
Methods
clearBreak
chrome.serial.clearBreak(connectionId: number, callback: function)
Restore character transmission on a given connection and place the transmission line in a nonbreak state.
Parameters
- connectionIdnumber
The id of the connection.
- callbackfunction
The callback parameter should be a function that looks like this:
(result: boolean) => {...}
- resultboolean
connect
chrome.serial.connect(path: string, options?: ConnectionOptions, callback: function)
Connects to a given serial port.
Parameters
- pathstring
The system path of the serial port to open.
- optionsConnectionOptions optional
Port configuration options.
- callbackfunction
Called when the connection has been opened.
The callback parameter should be a function that looks like this:
(connectionInfo: ConnectionInfo) => {...}
- connectionInfo
disconnect
chrome.serial.disconnect(connectionId: number, callback: function)
Disconnects from a serial port.
Parameters
- connectionIdnumber
The id of the opened connection.
- callbackfunction
Called when the connection has been closed.
The callback parameter should be a function that looks like this:
(result: boolean) => {...}
- resultboolean
flush
chrome.serial.flush(connectionId: number, callback: function)
Flushes all bytes in the given connection's input and output buffers.
Parameters
- connectionIdnumber
- callbackfunction
The callback parameter should be a function that looks like this:
(result: boolean) => {...}
- resultboolean
getConnections
chrome.serial.getConnections(callback: function)
Retrieves the list of currently opened serial port connections owned by the application.
Parameters
- callbackfunction
Called with the list of connections when available.
The callback parameter should be a function that looks like this:
(connectionInfos: ConnectionInfo[]) => {...}
- connectionInfos
getControlSignals
chrome.serial.getControlSignals(connectionId: number, callback: function)
Retrieves the state of control signals on a given connection.
Parameters
- connectionIdnumber
The id of the connection.
- callbackfunction
Called when the control signals are available.
The callback parameter should be a function that looks like this:
(signals: DeviceControlSignals) => {...}
- signals
getDevices
chrome.serial.getDevices(callback: function)
Returns information about available serial devices on the system. The list is regenerated each time this method is called.
Parameters
- callbackfunction
Called with the list of
DeviceInfo
objects.The callback parameter should be a function that looks like this:
(ports: DeviceInfo[]) => {...}
- ports
getInfo
chrome.serial.getInfo(connectionId: number, callback: function)
Retrieves the state of a given connection.
Parameters
- connectionIdnumber
The id of the opened connection.
- callbackfunction
Called with connection state information when available.
The callback parameter should be a function that looks like this:
(connectionInfo: ConnectionInfo) => {...}
- connectionInfo
send
chrome.serial.send(connectionId: number, data: ArrayBuffer, callback: function)
Writes data to the given connection.
setBreak
chrome.serial.setBreak(connectionId: number, callback: function)
Suspends character transmission on a given connection and places the transmission line in a break state until the clearBreak is called.
Parameters
- connectionIdnumber
The id of the connection.
- callbackfunction
The callback parameter should be a function that looks like this:
(result: boolean) => {...}
- resultboolean
setControlSignals
chrome.serial.setControlSignals(connectionId: number, signals: HostControlSignals, callback: function)
Sets the state of control signals on a given connection.
Parameters
- connectionIdnumber
The id of the connection.
- signals
The set of signal changes to send to the device.
- callbackfunction
Called once the control signals have been set.
The callback parameter should be a function that looks like this:
(result: boolean) => {...}
- resultboolean
setPaused
chrome.serial.setPaused(connectionId: number, paused: boolean, callback: function)
Pauses or unpauses an open connection.
Parameters
- connectionIdnumber
The id of the opened connection.
- pausedboolean
Flag to indicate whether to pause or unpause.
- callbackfunction
Called when the connection has been successfully paused or unpaused.
The callback parameter should be a function that looks like this:
() => {...}
update
chrome.serial.update(connectionId: number, options: ConnectionOptions, callback: function)
Update the option settings on an open serial port connection.
Parameters
- connectionIdnumber
The id of the opened connection.
- options
Port configuration options.
- callbackfunction
Called when the configuation has completed.
The callback parameter should be a function that looks like this:
(result: boolean) => {...}
- resultboolean
Events
onReceive
chrome.serial.onReceive.addListener(listener: function)
Event raised when data has been read from the connection.
Event
- listenerfunction
The listener parameter should be a function that looks like this:
(info: ReceiveInfo) => {...}
- info
Event data.
onReceiveError
chrome.serial.onReceiveError.addListener(listener: function)
Event raised when an error occurred while the runtime was waiting for data on the serial port. Once this event is raised, the connection may be set to paused
. A "timeout"
error does not pause the connection.
Event
- listenerfunction
The listener parameter should be a function that looks like this:
(info: ReceiveErrorInfo) => {...}
- info