diff --git a/files/en-us/web/api/serial/getports/index.html b/files/en-us/web/api/serial/getports/index.html new file mode 100644 index 000000000000000..b1b331123b9ae00 --- /dev/null +++ b/files/en-us/web/api/serial/getports/index.html @@ -0,0 +1,59 @@ +--- +title: Serial.getPorts() +slug: Web/API/Serial/getPorts +tags: + - API + - Method + - Reference + - Serial.getPorts + - Serial +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The getPorts() method of the {{domxref("Serial")}} interface returns a {{jsxref("Promise")}} that resolves with an array of {{domxref("SerialPort")}} objects representing serial ports connected to the host which the origin has permission to access.

+ +

Syntax

+ +
var promise = Serial.getPorts();
+ +

Return value

+ +

A {{jsxref("Promise")}} that resolves with an array of {{domxref("SerialPort")}} objects.

+ +

Exceptions

+ +
+
{{domxref("DOMException")}} "SecurityError"
+
The returned Promise rejects with this error if a Feature Policy restricts use of this API or a permission to use it has not granted via a user gesture.
+
+ +

Examples

+ +

The following example uses getPorts() to initialize a list of available ports.

+ +
navigator.serial.getPorts().then((ports) => {
+  // Initialize the list of available ports with `ports` on page load.
+});
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serial-getports','Serial.getPorts()')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.Serial.getPorts")}}

diff --git a/files/en-us/web/api/serial/index.html b/files/en-us/web/api/serial/index.html new file mode 100644 index 000000000000000..90ed5af9a5b6ff8 --- /dev/null +++ b/files/en-us/web/api/serial/index.html @@ -0,0 +1,91 @@ +--- +title: Serial +slug: Web/API/Serial +tags: + - API + - Interface + - Reference + - Serial +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

Description

+ +

The Serial interface of the {{domxref("Web_Serial_API", "Web Serial API")}} provides attributes and methods for finding and connecting to serial ports from a web page.

+ + +

Event Handlers

+ +
+
{{domxref("Serial.onconnect")}}
+
An event handler called when a port has been connected to the device.
+
{{domxref("Serial.ondisconnect")}}
+
An event handler called when a port has been disconnected from the device.
+
+ +

Methods

+ +
+
{{domxref("Serial.requestPort()")}}
+
+

Returns a {{jsxref("Promise")}} that resolves with an instance of {{domxref("SerialPort")}} representing the device chosen by the user or rejects if no device was selected.

+

This method must be called with user activation.

+
+
{{domxref("Serial.getPorts()")}}
+
+ Returns a {{jsxref("Promise")}} that resolves with an array of {{domxref("SerialPort")}} objects representing serial ports connected to + the host which the origin has permission to access. +
+
+ +

Examples

+ +

The following example shows how a site can check for available ports and allow the user to grant it permission to access additional ports.

+ +

On load event listeners are added for the connect and disconnect events so that the site can react when a device is connected or disconnected from the system. The {{domxref("Serial.getPorts()","getPorts()")}} method is then called to see which ports are connected that the site already has access to.

+ +

If the site doesn't have access to any connected ports it has to wait until it has user activation to proceed. In this example we use a {{domxref("Element.click_event", "click")}} event handler on a button for this task. A filter is passed to {{domxref("Serial.requestPort()","requestPort()")}} with a USB vendor ID in order to limit the set of devices shown to the user to only USB devices built by a particular manufacturer.

+ +
navigator.serial.addEventListener('connect', (e) => {
+  // Connect to `e.target` or add it to a list of available ports.
+});
+
+navigator.serial.addEventListener('disconnect', (e) => {
+  // Remove `e.target` from the list of available ports.
+});
+
+navigator.serial.getPorts().then((ports) => {
+  // Initialize the list of available ports with `ports` on page load.
+});
+
+button.addEventListener('click', () => {
+  const usbVendorId = ...;
+  navigator.serial.requestPort({ filters: [{ usbVendorId }]}).then((port) => {
+    // Connect to `port` or add it to the list of available ports.
+  }).catch((e) => {
+    // The user didn't select a port.
+  });
+});
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#serial-interface','Serial')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.Serial")}}

diff --git a/files/en-us/web/api/serial/onconnect/index.html b/files/en-us/web/api/serial/onconnect/index.html new file mode 100644 index 000000000000000..819c833a0471218 --- /dev/null +++ b/files/en-us/web/api/serial/onconnect/index.html @@ -0,0 +1,51 @@ +--- +title: Serial.onconnect +slug: Web/API/Serial/onconnect +tags: + - API + - Property + - Reference + - Serial.onconnect + - Serial +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The onconnect {{domxref("EventHandler")}} of the {{domxref("Serial")}} interface is called when a port has been connected from a device. This method receives an {{domxref("Event")}} object. The target of this event is the {{domxref("SerialPort")}} interface that has been disconnected. + +

This event is only fired for ports associated with removable devices such as those connected via USB. The user must grant the origin permission to access this device during a call to {{domxref("Serial.requestPort()","requestPort()")}} before this event will be fired.

+ +

Syntax

+ +
Serial.onconnect = function(event);
+Serial.addEventListener('connect', function(event));
+ +

Example

+ +

The following example shows an event listener for the connect event,. This allows the site to react when a port is connected.

+ +
navigator.serial.addEventListener('connect', (e) => {
+  // Connect to `e.target` or add it to a list of available ports.
+});
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serial-onconnect','Serial.onconnect')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.Serial.onconnect")}}

diff --git a/files/en-us/web/api/serial/ondisconnect/index.html b/files/en-us/web/api/serial/ondisconnect/index.html new file mode 100644 index 000000000000000..aa1b6eb449485c8 --- /dev/null +++ b/files/en-us/web/api/serial/ondisconnect/index.html @@ -0,0 +1,51 @@ +--- +title: Serial.ondisconnect +slug: Web/API/Serial/ondisconnect +tags: + - API + - Property + - Reference + - Serial.ondisconnect + - Serial +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The ondisconnect EventHandler of the {{domxref("Serial")}} interface is called when the port has been disconnected from the device. This method receives an {{domxref("Event")}} object. The target of this event is the {{domxref("SerialPort")}} interface that has been disconnected.

+ +

This event is only fired for ports associated with removable devices such as those connected via USB. The user must grant the origin permission to access this device during a call to {{domxref("Serial.requestPort()", "requestPort()")}} before this event will be fired.

+ +

Syntax

+ +
Serial.ondisconnect = function(event);
+Serial.addEventListener('disconnect', function(event));
+ +

Example

+ +

The following example shows an event listener for the disconnect event,. This allows the site to react when a port is disconnected.

+ +
navigator.serial.addEventListener('disconnect', (e) => {
+  // Remove `e.target` from the list of available ports.
+});
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serial-ondisconnect','Serial.ondisconnect')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.Serial.ondisconnect")}}

diff --git a/files/en-us/web/api/serial/requestport/index.html b/files/en-us/web/api/serial/requestport/index.html new file mode 100644 index 000000000000000..a59d82257db1148 --- /dev/null +++ b/files/en-us/web/api/serial/requestport/index.html @@ -0,0 +1,83 @@ +--- +title: Serial.requestPort() +slug: Web/API/Serial/requestPort +tags: + - API + - Method + - Reference + - requestPort + - Serial +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The Serial.requestPort() method of the {{domxref("Serial")}} interface returns a {{jsxref("Promise")}} that resolves with an instance of {{domxref("SerialPort")}} representing the device chosen by the user or rejects if no device was selected.

+ +

Syntax

+ +
var promise = Serial.requestPort([options]);
+ +

Parameters

+ +
+
options
+
+
+
filters
+
A list of objects containing vendor and product IDs used to search for attached devices. The USB Implementors Forum assigns IDs to specific companies. Each company assigns IDS to it's products. Filters contain the following values: +
    +
  • usbVendorId: An unsigned short integer that identifies a USB device vendor.
  • +
  • usbProductId: An unsigned short integer that identiffies a USB device.
  • +
+
+
+
+
+ +

Return value

+ +

A {{jsxref("Promise")}} that resolves with an instance of {{domxref("SerialPort")}}.

+ +

Exceptions

+ +
+
{{domxref("DOMException")}} "SecurityError"
+
The returned Promise rejects with this error if a Feature Policy restricts use of this API or a permission to use it has not granted via a user gesture.
+
{{domxref("DOMException")}} "AbortError"
+
The returned Promise rejects with this if the user does not select a port when prompted.
+
+ +

Examples

+ +

The following example shows a filter being passed to requestPort() with a USB vendor ID in order to limit the set of devices shown to the user to only USB devices built by a particular manufacturer. If this filter was omitted the user would be able to select any available port.

+ +
button.addEventListener('click', () => {
+  const usbVendorId = ...;
+  navigator.serial.requestPort({ filters: [{ usbVendorId }]}).then((port) => {
+    // Connect to `port` or add it to the list of available ports.
+  }).catch((e) => {
+    // The user didn't select a port.
+  });
+});
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serial-requestport','Serial.requestPort()')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.Serial.requestPort")}}

diff --git a/files/en-us/web/api/serialport/close/index.html b/files/en-us/web/api/serialport/close/index.html new file mode 100644 index 000000000000000..06e0fab1787d3ef --- /dev/null +++ b/files/en-us/web/api/serialport/close/index.html @@ -0,0 +1,48 @@ +--- +title: SerialPort.close() +slug: Web/API/SerialPort/close +tags: + - API + - Method + - Reference + - close + - SerialPort +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The SerialPort.close() method of the {{domxref("SerialPort")}} interface returns a {{jsxref("Promise")}} that resolves when the port closes.

+ +

Syntax

+ +
var promise = SerialPort.close();
+ +

Parameters

+ +

None.

+ +

Return value

+ +

A {{jsxref("Promise")}}.

+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serialport-close','SerialPort.close()')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.SerialPort.close()")}}

diff --git a/files/en-us/web/api/serialport/getinfo/index.html b/files/en-us/web/api/serialport/getinfo/index.html new file mode 100644 index 000000000000000..d3a5244a2f48143 --- /dev/null +++ b/files/en-us/web/api/serialport/getinfo/index.html @@ -0,0 +1,55 @@ +--- +title: SerialPort.getInfo() +slug: Web/API/SerialPort/getInfo +tags: + - API + - Method + - Reference + - getInfo() + - SerialPort +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The SerialPort.getInfo() method of the {{domxref("SerialPort")}} interface returns a {{jsxref("Promise")}} that resolves with an object containing properties of the port.

+ +

Syntax

+ +
var promise = SerialPort.getInfo();
+ +

Parameters

+ +

None.

+ +

Return value

+ +

An object containing the following values.

+ +
+
usbVendorId
+
If the port is part of a USB device, an unsigned short integer that identifies a USB device vendor, otherwise undefined.
+
usbProductId
+
If the port is part of a USB device, an unsigned short integer that identiffies a USB device, otherwise undefined.
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serialport-getinfo','SerialPort.getInfo()')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.SerialPort.getInfo")}}

diff --git a/files/en-us/web/api/serialport/getsignals/index.html b/files/en-us/web/api/serialport/getsignals/index.html new file mode 100644 index 000000000000000..802b0759ead6323 --- /dev/null +++ b/files/en-us/web/api/serialport/getsignals/index.html @@ -0,0 +1,68 @@ +--- +title: SerialPort.getSignals() +slug: Web/API/SerialPort/getSignals +tags: + - API + - Method + - Reference + - getSignals() + - SerialPort +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The SerialPort.getSignals() method of the {{domxref("SerialPort")}} interface returns a {{jsxref("Promise")}} that resolves with an object containing the current state of the port's control signals.

+ +

Syntax

+ +
var promise = SerialPort.getSignals();
+ +

Parameters

+ +

None.

+ +

Return value

+ +

Returns a {{jsxref("Promise")}} that resolves with an object containing the following members:

+ +
+
clearToSend
+
A boolean indicating to the other end of a serial connection that is is clear to send data.
+
dataCarrierDetect
+
A boolean that toggles the control signal needed to communicate over a serial connection.
+
dataSetReady
+
A boolean indicating whether the device is ready to send and receive data.
+
ringIndicator
+
A boolean indicating whether a ring signal should be sent down the serial connection.
+
+ +

Exceptions

+ +
+
{{domxref("DOMException")}} "InvalidStateError"
+
Indicates that the port is not open. Call {{domxref("SerialPort.open()")}} to avoid this error.
+
{{domxref("DOMException")}} NetworkError
+
Indicates that one of the signals on the device could not be set.
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serialport-getsignals','SerialPort.getSignals()')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.SerialPort.getSignals")}}

diff --git a/files/en-us/web/api/serialport/index.html b/files/en-us/web/api/serialport/index.html new file mode 100644 index 000000000000000..4d4e2353bd72e2d --- /dev/null +++ b/files/en-us/web/api/serialport/index.html @@ -0,0 +1,115 @@ +--- +title: SerialPort +slug: Web/API/SerialPort +tags: + - API + - Interface + - Reference + - SerialPort +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

Description

+ +

The SerialPort interface of the {{domxref("Web_Serial_API", "Web Serial API")}} provides access to a serial port on the host device.

+ +

Constructor

+ +

Instances of this interface may be obtained by calling methods of the {{domxref("Serial")}} interface, therefore it has no constructor of its own. + +

Properties

+ +
+
{{domxref("SerialPort.readable")}}{{readonlyInline}}
+
Returns a {{domxref("ReadableStream")}} for receiving data from the device connected to the port.
+
{{domxref("SerialPort.writable")}}{{readonlyInline}}
+
Returns a {{domxref("WritableStream")}} for sending data to the device connected to the port.
+
+ +

Event handlers

+ +
+
{{domxref("SerialPort.onconnect")}}
+
An event handler called when the port has connected to the device.
+
{{domxref("SerialPort.ondisconnect")}}
+
An event handler called when the port has disconnected from the device.
+
+ +

Methods

+ +
+
{{domxref("SerialPort.getInfo()")}}
+
Returns a {{jsxref("Promise")}} that resolves with an object containing properties of the port.
+
{{domxref("SerialPort.open()")}}
+
Returns a {{jsxref("Promise")}} that resolves when the port is opened. By default the port is opened with 8 data bits, 1 stop bit and no parity checking.
+
{{domxref("SerialPort.setSignals()")}}
+
Sets control signals on the port and returns a {{jsxref("Promise")}} that resolves when they are set.
+
{{domxref("SerialPort.getSignals()")}}
+
Returns a {{jsxref("Promise")}} that resolves with an object containing the current state of the port's control signals.
+
{{domxref("SerialPort.close()")}}
+
Returns a {{jsxref("Promise")}} that resolves when the port closes.
+
+ +

Examples

+ +

Opening a port

+ +

Before communicating on a serial port it must be opened. Opening the port allows the site to specify the necessary parameters that control how data is transmitted and received. Developers should check the documentation for the device they are connecting to for the appropriate parameters.

+ +
await port.open({ baudRate: /* pick your baud rate */ });
+ +

Once the Promise returned by open() resolves the readable and writable attributes can be accessed to get the {{domxref("ReadableStream")}} and {{domxref("WritableStream")}} instances for receiving data from and sending data to the connected device.

+ +

Reading data from a port

+ +

The following example shows how to read data from a port. The outer loop handles non-fatal errors, creating a new reader until a fatal error is encountered and readable becomes null.

+ +
while (port.readable) {
+  const reader = port.readable.getReader();
+  try {
+    while (true) {
+      const { value, done } = await reader.read();
+      if (done) {
+        // |reader| has been canceled.
+        break;
+      }
+      // Do something with |value|...
+    }
+  } catch (error) {
+    // Handle |error|...
+  } finally {
+    reader.releaseLock();
+  }
+}
+ +

Writing data to a port

+ +

The following example shows how to write a string to a port. A {{domxref("TextEncoder")}} converts the string to a Uint8Array before transmission.

+ +
const encoder = new TextEncoder();
+const writer = port.writable.getWriter();
+await writer.write(encoder.encode("PING"));
+writer.releaseLock();
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serialport','SerialPort')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.SerialPort")}}

diff --git a/files/en-us/web/api/serialport/onconnect/index.html b/files/en-us/web/api/serialport/onconnect/index.html new file mode 100644 index 000000000000000..fe45a4b3d5676f1 --- /dev/null +++ b/files/en-us/web/api/serialport/onconnect/index.html @@ -0,0 +1,42 @@ +--- +title: SerialPort.onconnect +slug: Web/API/SerialPort/onconnect +tags: + - API + - Property + - Reference + - onconnect + - SerialPort +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The onconnect event handler of the {{domxref("SerialPort")}} interface is called when the port has disconnected from the device. This method receives an {{domxref("Event")}} object. This event is only fired for ports associated with removable devices such as those connected via USB. This event bubbles to the instance of {{domxref("Serial")}} that returned this interface..

+ + +

Syntax

+ +
SerialPort.onconnect = function(event);
+SerialPort.addEventListener('connect', function(event));
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serialport-onconnect','SerialPort.onconnect')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.SerialPort.onconnect")}}

diff --git a/files/en-us/web/api/serialport/ondisconnect/index.html b/files/en-us/web/api/serialport/ondisconnect/index.html new file mode 100644 index 000000000000000..c9f9bc0cac080e5 --- /dev/null +++ b/files/en-us/web/api/serialport/ondisconnect/index.html @@ -0,0 +1,41 @@ +--- +title: SerialPort.ondisconnect +slug: Web/API/SerialPort/ondisconnect +tags: + - API + - Property + - Reference + - ondisconnect + - SerialPort +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The ondisconnect event handler of the {{domxref("SerialPort")}} interface is called when the port has disconnected from the device. This method receives an {{domxref("Event")}} object. This event is only fired for ports associated with removable devices such as those connected via USB. This event bubbles to the instance of {{domxref("Serial")}} that returned this interface. + +

Syntax

+ +
SerialPort.ondisconnect = function(event);
+SerialPort.addEventListener('disconnect', function(event));
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serialport-ondisconnect','SerialPort.ondisconnect')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.SerialPort.ondisconnect")}}

diff --git a/files/en-us/web/api/serialport/open/index.html b/files/en-us/web/api/serialport/open/index.html new file mode 100644 index 000000000000000..b4f05ea1502bb82 --- /dev/null +++ b/files/en-us/web/api/serialport/open/index.html @@ -0,0 +1,81 @@ +--- +title: SerialPort.open() +slug: Web/API/SerialPort/open +tags: + - API + - Method + - Reference + - open + - SerialPort +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The open() method of the {{domxref("SerialPort")}} interface returns a {{jsxref("Promise")}} that resolves when the port is opened. By default the port is opened with 8 data bits, 1 stop bit and no parity checking. The baudRate parameter is required.

+ +

Syntax

+ +
var promise = SerialPort.open(options);
+ +

Parameters

+ +
+
options
+
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.
+
bufferSize{{optional_inline}}
+
An unsigned long integer indicating the size of the read and write buffers that are to be established. If not passed, defaults to 255.
+
dataBits{{optional_inline}}
+
An integer value of 7 or 8 indicating the number of data bits per frame. If not passed, defaults to 8.
+
flowControl{{optional_inline}}
+
The flow control type, either "none" or "hardware". The default value is "none:.
+
parity{{optional_inline}}
+
The parity mode, either "none", "even", or "odd". The default value is "none".
+
stopBits{{optional_inline}}
+
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.
+
+
+
+ +

Return value

+ +

A {{jsxref("Promise")}}.

+ +

Exceptions

+ +
+
{{domxref("DOMException")}} "InvalidStateError"
+
Indicates that the port is already open.
+
{{domxref("DOMException")}} NetworkError
+
Indicates that the attempt to open the port failed.
+
+ +

Examples

+ +

Before communicating on a serial port it must be opened. Opening the port allows the site to specify the necessary parameters that control how data is transmitted and received. Developers should check the documentation for the device they are connecting to for the appropriate parameters.

+ +
await port.open({ baudRate: /* pick your baud rate */ });
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serialport-open','SerialPort.open()')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.SerialPort.open")}}

diff --git a/files/en-us/web/api/serialport/readable/index.html b/files/en-us/web/api/serialport/readable/index.html new file mode 100644 index 000000000000000..5d107d928196aaf --- /dev/null +++ b/files/en-us/web/api/serialport/readable/index.html @@ -0,0 +1,65 @@ +--- +title: SerialPort.readable +slug: Web/API/SerialPort/readable +tags: + - API + - Property + - Reference + - readable + - SerialPort +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The readable read-only property of the {{domxref("SerialPort")}} interface returns a {{domxref("ReadableStream")}} for receiving data from the device connected to the port. Chunks read from this stream are instances of {{jsxref("Uint8Array")}}. This property is non-null as long as the port is open and has not encountered a fatal error.

+ +

Syntax

+ +
var readableStream = SerialPort.readable;
+ +

Value

+

A {{domxref("ReadableStream")}}.

+ +

Examples

+ +

The following example shows how to read data from a port. The outer loop handles non-fatal errors, creating a new reader until a fatal error is encountered and readable becomes null.

+ +
while (port.readable) {
+  const reader = port.readable.getReader();
+  try {
+    while (true) {
+      const { value, done } = await reader.read();
+      if (done) {
+        // |reader| has been canceled.
+        break;
+      }
+      // Do something with |value|...
+    }
+  } catch (error) {
+    // Handle |error|...
+  } finally {
+    reader.releaseLock();
+  }
+}
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serialport-readable','SerialPort.readable')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.SerialPort.readable")}}

diff --git a/files/en-us/web/api/serialport/setsignals/index.html b/files/en-us/web/api/serialport/setsignals/index.html new file mode 100644 index 000000000000000..5816eead7615238 --- /dev/null +++ b/files/en-us/web/api/serialport/setsignals/index.html @@ -0,0 +1,71 @@ +--- +title: SerialPort.setSignals() +slug: Web/API/SerialPort/setSignals +tags: + - API + - Method + - Reference + - setSignals + - SerialPort +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The setSignals() method of the {{domxref("SerialPort")}} interface sets control signals on the port and returns a {{jsxref("Promise")}} that resolves when they are set.

+ +

Syntax

+ +
var promise = SerialPort.setSignals(options);
+ +

Parameters

+ +
+
options{{optional_inline}}
+
An object with any of the following values: +
+
clearToSend
+
A boolean indicating to the other end of a serial connection that is is clear to send data.
+
dataCarrierDetect
+
A boolean that toggles the control signal needed to communicate over a serial connection.
+
dataSetReady
+
A boolean indicating whether the device is ready to send and receive data.
+
ringIndicator
+
A boolean indicating whether a ring signal should be sent down the serial connection.
+
+
+
+ +

Return value

+ +

A {{jsxref("Promise")}}.

+ +

Exceptions

+ +
+
{{domxref("DOMException")}} "InvalidStateError"
+
Indicates that the port is not open. Call {{domxref("SerialPort.open()")}} to avoid this error.
+
{{domxref("DOMException")}} NetworkError
+
Indicates that one of the signals on the device could not be set.
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serialport-setsignals','SerialPort.setSignals()')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.SerialPort.setSignals")}}

diff --git a/files/en-us/web/api/serialport/writable/index.html b/files/en-us/web/api/serialport/writable/index.html new file mode 100644 index 000000000000000..78ce5ba466e0301 --- /dev/null +++ b/files/en-us/web/api/serialport/writable/index.html @@ -0,0 +1,52 @@ +--- +title: SerialPort.writable +slug: Web/API/SerialPort/writable +tags: + - API + - Property + - Reference + - writable + - SerialPort +--- +
{{securecontext_header}}{{DefaultAPISidebar("Serial API")}}
+ +

The writable read-only property of the {{domxref("SerialPort")}} interface returns a {{domxref("WritableStream")}} for sending data to the device connected to the port. Chunks written to this stream must be instances of {{domxref("BufferSource")}} (for example, an {{jsxref("ArrayBuffer")}} or {{domxref("ArrayBufferView")}} such as {{jsxref("Uint8Array")}}). This property is non-null as long as the port is open and has not encountered a fatal error.

+ +

Syntax

+ +
var writableStream = SerialPort.writable;
+ +

Value

+

A {{domxref("WritableStream")}}

+ +

Examples

+ +

The following example shows how to write a string to a port. A {{domxref("TextEncoder")}} converts the string to a Uint8Array before transmission.

+ +
const encoder = new TextEncoder();
+const writer = port.writable.getWriter();
+await writer.write(encoder.encode("PING"));
+writer.releaseLock();
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Serial API','#dom-serialport-writable','SerialPort.writable')}}{{Spec2('Web Serial API')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.SerialPort.writable")}}