page. If you click it, Chrome will open a console for the background page.
Also, it automatically creates another link next to it, pointing to the applica-
tion’s main page. If you click this link, you’ll get access to all of your applica-
tion’s innards—that is, you’ll get a JavaScript console for your running Chrome
App.
Because our first Chrome app’s manifest requests permission for accessing
the serial port, you can use Chrome’s serial API
6
in the application’s JavaScript
console.
The following function prints the paths to all serial devices connected to your
computer (to enter multi-line functions in the JavaScript console, press
Shift+Enter at the end of each line):
chrome.serial.getDevices(function(devices) {
devices.forEach(function(d) {
console.log(d.path);
})
});
On my machine this function outputs the following:
/dev/cu.Bluetooth-Incoming-Port
/dev/tty.Bluetooth-Incoming-Port
/dev/cu.Bluetooth-Modem
/dev/tty.Bluetooth-Modem
/dev/cu.usbmodem24321
/dev/tty.usbmodem24321
The last two lines represent the serial port connected to my Arduino. With
the following statement you can connect to the Arduino from the JavaScript
console:
chrome.serial.connect(
"/dev/tty.usbmodem24321",
{ bitrate: 38400 },
function(c) { console.log(c) })
chrome.serial.connect
expects three arguments. The first is the path to the serial
port to connect to. With the second argument, you can specify typical options
for serial ports, such as the baud rate (named
bitrate
in this case) or the parity
bit. Eventually you have to pass a callback function that gets called after
Chrome tries to establish the connection.
6.
https://developer.chrome.com/apps/serial
Appendix 4. Controlling the Arduino with a Browser • 272
report erratum • discuss
www.it-ebooks.info