Hardware Reference
In-Depth Information
Set the LEDs to the current values of the
red
and
green
variables.
Wait for characters to arrive on the serial port.
After the characters are received, read it and respond to it.
On the Bone, add the script in
Example 6-14
to a file called
launchPad.js
and run it.
Example 6-14. Code for communicating via the UART (launchPad.js)
#!/usr/bin/env node
// Need to add exports.serialParsers = m.module.parsers;
// to /usr/local/lib/node_modules/bonescript/serial.js
var b
=
require
(
'
bonescript
'
);
var port
=
'
/
dev
/
ttyO1
'
;
var options
= {
baudrate
:
9600
,
parser
:
b
.
serialParsers
.
readline
(
"
\n
"
)
};
b
.
serialOpen
(
port
,
options
,
onSerial
);
function
onSerial
(
x
) {
console
.
log
(
x
.
event
);
if
(
x
.
err
) {
console
.
log
(
'
***
ERROR
***
'
+
JSON
.
stringify
(
x
));
}
if
(
x
.
event
==
'
open
'
) {
console
.
log
(
'
***
OPENED
***
'
);
setInterval
(
sendCommand
,
1000
);
}
if
(
x
.
event
==
'
data
'
) {
console
.
log
(
String
(
x
.
data
));
}
}
var command
= [
'r'
,
'g'
];
var commIdx
=
1
;
function
sendCommand
() {