Arduino Uno Quick Start Manual page 289

Hide thumbs Also See for Arduino Uno:
Table of Contents

Advertisement

First of all, you should note that we define all methods on the
erty of the
SerialDevice
object. I won t go into the details here, but you should
know that this is one way to add new methods to objects in JavaScript.
The
connect
method delegates its work to the
you saw in the previous section already. The only thing worth noting is the
callback function we pass in the function call. Again we use
callback function s context explicitly. This way, we make sure that
has access to the properties of the
Complete
We benefit from that in the
nectionId
property of our
ed to a serial device. If we hadn t bound
have a completely different meaning in this function, and we couldn t access
the properties of the
SerialDevice
In lines 14 and 15, we use the same technique to add receive and error listen-
ers to the
chrome.serial
object. Here we use the listeners we ve prepared in the
constructor function before. After we ve established the connection success-
fully, we call the
onConnect
all listeners outside.
Eventually, we have to implement the actual listener functions that deal with
incoming and outgoing data and with errors:
ChromeApps/SerialDevice/js/serial_device.js
SerialDevice.prototype.onReceive = function(receiveInfo) {
if
(receiveInfo.connectionId !== this.connectionId) {
return;
}
this.readBuffer += this.arrayBufferToString(receiveInfo.data);
var
n;
while
((n = this.readBuffer.indexOf('\n')) >= 0) {
var
line = this.readBuffer.substr(0, n + 1);
this.onReadLine.dispatch(line);
this.readBuffer = this.readBuffer.substr(n + 1);
}
};
SerialDevice.prototype.onReceiveError = function(errorInfo) {
if
(errorInfo.connectionId === this.connectionId) {
this.onError.dispatch(errorInfo.error);
}
};
SerialDevice.prototype.send = function(data) {
SerialDevice
onConnectComplete
method. Here we can set the
SerialDevice
object as soon as we ve successfully connect-
onConnectComplete
object.
object s
dispatch
method to spread the good news to
www.it-ebooks.info
Writing a SerialDevice Class
prototype
chrome.serial.connect
function that
bind
to set the
object.
before,
this
277
prop-
onConnect-
con-
would
report erratum
discuss

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the Arduino Uno and is the answer not in the manual?

Subscribe to Our Youtube Channel

Table of Contents