1 <h1>Serial Devices</h1>
7 This document describes how to use the <a href="serial.html">serial API</a> to read
8 and write from serial devices. Chrome Apps can also connect to
9 <a href="usb.html">USB</a> and <a href="bluetooth.html">Bluetooth</a> devices.
13 <b>Samples:</b> For examples that illustrate how Chrome Apps can connect to hardware devices, see the
14 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/serial">serial</a>,
15 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/servo">servo</a>,
16 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/usb">usb</a>, and
17 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/zephyr_hxm">zephyr_hxm
18 Bluetooth</a> samples.
21 <h2 id="requirement">Manifest requirement</h2>
24 You must add the "serial" permission to the manifest file:
26 <pre data-filename="manifest.json">
32 <h2 id="listing">Listing available serial ports</h2>
35 To get a list of available serial ports,
36 use the <code>getPorts()</code> method. <b>Note:</b> not all serial ports are available. The API uses a heuristic based on the name of the port to only expose serial devices that are expected to be safe.
40 var onGetPorts = function(ports) {
41 for (var i=0; i<ports.length; i++) {
42 console.log(ports[i]);
45 chrome.serial.getPorts(onGetPorts);
48 <h2 id="opening">Opening a serial device</h2>
51 If you know the serial port name, you can open it for read and write using the <code>open</code> method:
55 chrome.serial.open(portName, options, openCallback)
60 <th scope="col"> Parameter </th>
61 <th scope="col"> Description </th>
64 <td>portName (string)</td>
65 <td>If your device's port name is unknown, you can use the <code>getPorts</code> method.</td>
68 <td>options (object)</td>
69 <td>Parameter object with one single value: <code>bitrate</code>, an integer specifying the desired bitrate used to communicate with the serial port.</td>
73 <td>Invoked when the port has been successfully opened. The callback will be called with one parameter, <code>openInfo</code>, that has one attribute, <code>connectionId</code>. Save this id, because you will need it to actually communicate with the port.
78 <p>A simple example:</p>
81 var onOpen = function(connectionInfo) {
82 // The serial port has been opened. Save its id to use later.
83 _this.connectionId = connectionInfo.connectionId;
84 // Do whatever you need to do with the opened port.
86 // Open the serial port /dev/ttyS01
87 chrome.serial.open("/dev/ttyS01", {bitrate: 115200}, onOpen);
90 <h2 id="closing">Closing a serial port</h2>
93 Closing a serial port is simple but very important. See the example below:
97 var onClose = function(result) {
98 console.log("Serial port closed");
100 chrome.serial.close(connectionId, onClose);
103 <h2 id="reading">Reading from a serial port</h2>
106 The serial API reads from the serial port and
107 delivers the read bytes as an ArrayBuffer.
108 There is no guarantee that all the requested bytes, even if available in the port, will be read in one chunk.
109 The following example can accumulate read bytes, at most 128 at a time, until a new line is read,
110 and then call a listener with the <code>ArrayBuffer</code> bytes converted to a String:
116 var onCharRead=function(readInfo) {
117 if (!connectionInfo) {
120 if (readInfo && readInfo.bytesRead>0 && readInfo.data) {
121 var str=ab2str(readInfo.data);
122 if (str[readInfo.bytesRead-1]==='\n') {
123 dataRead+=str.substring(0, readInfo.bytesRead-1);
124 onLineRead(dataRead);
130 chrome.serial.read(connectionId, 128, onCharRead);
133 /* Convert an ArrayBuffer to a String, using UTF-8 as the encoding scheme.
134 This is consistent with how Arduino sends characters by default */
135 var ab2str=function(buf) {
136 return String.fromCharCode.apply(null, new Uint8Array(buf));
140 <h2 id="writing">Writing to a serial port</h2>
143 The writing routine is simpler than reading,
144 since the writing can occur all at once.
145 The only catch is that if your data protocol is String based,
146 you have to convert your output string to an <code>ArrayBuffer</code>.
147 See the code example below:
151 var writeSerial=function(str) {
152 chrome.serial.write(connectionId, str2ab(str), onWrite);
154 // Convert string to ArrayBuffer
155 var str2ab=function(str) {
156 var buf=new ArrayBuffer(str.length);
157 var bufView=new Uint8Array(buf);
158 for (var i=0; i<str.length; i++) {
159 bufView[i]=str.charCodeAt(i);
165 <h2 id="flushing">Flushing a serial port buffer</h2>
168 You can flush your serial port buffer by issuing the flush command:
172 chrome.serial.flush(connectionId, onFlush);