Sep 09, 2014 Chrome Apps. Contribute to GoogleChrome/chrome-app-samples development by creating an account on GitHub.
This is my first attempt at developing with a chrome app or extension. I have a GPS receiver on the USB port which is emulated as a serial device.
Running this code
gets me '/dev/ttyUSB0' in the console, so it appears to be finding the device.
How do I then connect to the device? I've included the serial.connect line above, with the following functions:
but I get the following error:
Error in response to serial.connect: ReferenceError: _this is not defined at Object.onGetDevices [as callback]
I'm not sure exactly what I'm doing right or wrong here so any pointers appreciated.
XanHow To Use Chrome.serial Api
1 Answer
First the example does not work properly. Try this instead:
Now as for the actual reading of the input from the serial connection it will work but converting the ArrayBuffer to a string is a bit harder than expected.
I'm working on a Google Chrome extension that communicates with an Arduino UNO over serial. I wrote the following function sendSerialCmd
that takes a port (string), a serial_cmd (ArrayBuffer), and a callback function that gets passed readInfo.data (an ArrayBuffer) that is read in from the serial connection.
One of the problems I've run into is the third parameter passed to the chrome.serial.read()
function. In the chrome.serial API, the third parameter is the bytesToRead ( integer ): The number of bytes to read, however, the number of bytes coming in from my Arduino are subject to change. Sometimes I may get 8 bytes, other times more. What's the best way of getting all the bytes sent back from my Arduino?
Chrome Extension Api Serial Port
Arduino has a novel solution through a function called Serial.available() which returns the number of bytes available to read. Is there something similar I can do with the Chrome.serial API?
rafiki_rafiChrome Serial Api Tutorial
rafiki_rafiChrome Serial Api
1 Answer
There is no similar functionality to Serial.available()
in the Chrome serial API, but you should just poll for some reasonable number of bytes and process the data at your own pace.
If a read
is requested for (say) 1024 bytes and only 8 bytes are available, the read
should still succeed quickly with just 8 bytes. If there is an 8-byte message and a 12-byte message available, the read
will succeed with 20 bytes and you can apply whatever logic is necessary to parse the resulting data.
FYI, the serial API is changing soon in Canary and you will no longer be responsible for manually polling with read
. Instead Chrome will poll for you and fire chrome.serial.onReceive
events as data is available from an open device.