Dart Documentationchrome.serialChromeSerial

ChromeSerial class

class ChromeSerial extends ChromeApi {
 static final JsObject _serial = chrome['serial'];

 ChromeSerial._();

 bool get available => _serial != null;

 /**
  * Returns names of valid ports on this machine, each of which is likely to be
  * valid to pass as the port argument to open(). The list is regenerated each
  * time this method is called, as port validity is dynamic.
  * 
  * [callback]: Called with the list of ports.
  */
 Future<List<String>> getPorts() {
   if (_serial == null) _throwNotAvailable();

   var completer = new ChromeCompleter<List<String>>.oneArg(listify);
   _serial.callMethod('getPorts', [completer.callback]);
   return completer.future;
 }

 /**
  * Opens a connection to the given serial port.
  * [port]: The name of the serial port to open.
  * [options]: Connection options.
  * [callback]: Called when the connection has been opened.
  */
 Future<OpenInfo> open(String port, [OpenOptions options]) {
   if (_serial == null) _throwNotAvailable();

   var completer = new ChromeCompleter<OpenInfo>.oneArg(_createOpenInfo);
   _serial.callMethod('open', [port, jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * Closes an open connection.
  * [connectionId]: The id of the opened connection.
  * [callback]: Called when the connection has been closed.
  * 
  * Returns:
  * Returns true if operation was successful.
  */
 Future<bool> close(int connectionId) {
   if (_serial == null) _throwNotAvailable();

   var completer = new ChromeCompleter<bool>.oneArg();
   _serial.callMethod('close', [connectionId, completer.callback]);
   return completer.future;
 }

 /**
  * Reads a byte from the given connection.
  * [connectionId]: The id of the connection.
  * [bytesToRead]: The number of bytes to read.
  * [callback]: Called when all the requested bytes have been read or when the
  * read blocks.
  */
 Future<SerialReadInfo> read(int connectionId, int bytesToRead) {
   if (_serial == null) _throwNotAvailable();

   var completer = new ChromeCompleter<SerialReadInfo>.oneArg(_createReadInfo);
   _serial.callMethod('read', [connectionId, bytesToRead, completer.callback]);
   return completer.future;
 }

 /**
  * Writes a string to the given connection.
  * [connectionId]: The id of the connection.
  * [data]: The string to write.
  * [callback]: Called when the string has been written.
  */
 Future<SerialWriteInfo> write(int connectionId, ArrayBuffer data) {
   if (_serial == null) _throwNotAvailable();

   var completer = new ChromeCompleter<SerialWriteInfo>.oneArg(_createWriteInfo);
   _serial.callMethod('write', [connectionId, jsify(data), completer.callback]);
   return completer.future;
 }

 /**
  * Flushes all bytes in the given connection's input and output buffers.
  * [connectionId]: The id of the connection.
  * [callback]: Called when the flush is complete.
  * 
  * Returns:
  * Returns true if operation was successful.
  */
 Future<bool> flush(int connectionId) {
   if (_serial == null) _throwNotAvailable();

   var completer = new ChromeCompleter<bool>.oneArg();
   _serial.callMethod('flush', [connectionId, completer.callback]);
   return completer.future;
 }

 Future<ControlSignalOptions> getControlSignals(int connectionId) {
   if (_serial == null) _throwNotAvailable();

   var completer = new ChromeCompleter<ControlSignalOptions>.oneArg(_createControlSignalOptions);
   _serial.callMethod('getControlSignals', [connectionId, completer.callback]);
   return completer.future;
 }

 Future<bool> setControlSignals(int connectionId, ControlSignalOptions options) {
   if (_serial == null) _throwNotAvailable();

   var completer = new ChromeCompleter<bool>.oneArg();
   _serial.callMethod('setControlSignals', [connectionId, jsify(options), completer.callback]);
   return completer.future;
 }

 void _throwNotAvailable() {
   throw new UnsupportedError("'chrome.serial' is not available");
 }
}

Extends

ChromeApi > ChromeSerial

Properties

final bool available #

Returns true if the API is available. The common causes of an API not being avilable are:

  • a permission is missing in the application's manifest.json file
  • the API is defined on a newer version of Chrome then the current runtime
docs inherited from ChromeApi
bool get available => _serial != null;

Methods

Future<bool> close(int connectionId) #

Closes an open connection. connectionId: The id of the opened connection. callback: Called when the connection has been closed.

Returns: Returns true if operation was successful.

Future<bool> close(int connectionId) {
 if (_serial == null) _throwNotAvailable();

 var completer = new ChromeCompleter<bool>.oneArg();
 _serial.callMethod('close', [connectionId, completer.callback]);
 return completer.future;
}

Future<bool> flush(int connectionId) #

Flushes all bytes in the given connection's input and output buffers. connectionId: The id of the connection. callback: Called when the flush is complete.

Returns: Returns true if operation was successful.

Future<bool> flush(int connectionId) {
 if (_serial == null) _throwNotAvailable();

 var completer = new ChromeCompleter<bool>.oneArg();
 _serial.callMethod('flush', [connectionId, completer.callback]);
 return completer.future;
}

Future<ControlSignalOptions> getControlSignals(int connectionId) #

Future<ControlSignalOptions> getControlSignals(int connectionId) {
 if (_serial == null) _throwNotAvailable();

 var completer = new ChromeCompleter<ControlSignalOptions>.oneArg(_createControlSignalOptions);
 _serial.callMethod('getControlSignals', [connectionId, completer.callback]);
 return completer.future;
}

Future<List<String>> getPorts() #

Returns names of valid ports on this machine, each of which is likely to be valid to pass as the port argument to open(). The list is regenerated each time this method is called, as port validity is dynamic.

callback: Called with the list of ports.

Future<List<String>> getPorts() {
 if (_serial == null) _throwNotAvailable();

 var completer = new ChromeCompleter<List<String>>.oneArg(listify);
 _serial.callMethod('getPorts', [completer.callback]);
 return completer.future;
}

Future<OpenInfo> open(String port, [OpenOptions options]) #

Opens a connection to the given serial port. port: The name of the serial port to open. options: Connection options. callback: Called when the connection has been opened.

Future<OpenInfo> open(String port, [OpenOptions options]) {
 if (_serial == null) _throwNotAvailable();

 var completer = new ChromeCompleter<OpenInfo>.oneArg(_createOpenInfo);
 _serial.callMethod('open', [port, jsify(options), completer.callback]);
 return completer.future;
}

Future<SerialReadInfo> read(int connectionId, int bytesToRead) #

Reads a byte from the given connection. connectionId: The id of the connection. bytesToRead: The number of bytes to read. callback: Called when all the requested bytes have been read or when the read blocks.

Future<SerialReadInfo> read(int connectionId, int bytesToRead) {
 if (_serial == null) _throwNotAvailable();

 var completer = new ChromeCompleter<SerialReadInfo>.oneArg(_createReadInfo);
 _serial.callMethod('read', [connectionId, bytesToRead, completer.callback]);
 return completer.future;
}

Future<bool> setControlSignals(int connectionId, ControlSignalOptions options) #

Future<bool> setControlSignals(int connectionId, ControlSignalOptions options) {
 if (_serial == null) _throwNotAvailable();

 var completer = new ChromeCompleter<bool>.oneArg();
 _serial.callMethod('setControlSignals', [connectionId, jsify(options), completer.callback]);
 return completer.future;
}

Future<SerialWriteInfo> write(int connectionId, ArrayBuffer data) #

Writes a string to the given connection. connectionId: The id of the connection. data: The string to write. callback: Called when the string has been written.

Future<SerialWriteInfo> write(int connectionId, ArrayBuffer data) {
 if (_serial == null) _throwNotAvailable();

 var completer = new ChromeCompleter<SerialWriteInfo>.oneArg(_createWriteInfo);
 _serial.callMethod('write', [connectionId, jsify(data), completer.callback]);
 return completer.future;
}