-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial.js
executable file
·46 lines (40 loc) · 1.14 KB
/
serial.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr//bin/node
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
serialport.list(function (err, ports) {
console.log("Serial ports found:");
ports.forEach(function(port) {
console.log(port.comName);
console.log(port.pnpId);
console.log(port.manufacturer);
});
});
var comPortName = '/dev/ttyUSB0';
var serial = new SerialPort(comPortName, {
baudrate: 9600,
parser: serialport.parsers.readline('\n')
}, false);
serial.open(function (error) {
if ( error ) {
console.log('failed to open: '+error);
} else {
console.log('open');
serial.on('data', function(data) {
console.log('data received: ' + data);
});
serial.write("dragon", function(err, results) {
console.log('err ' + err);
console.log('results ' + results);
});
}
});
serial.on('open', function (data) {
console.log('Serial opened: ' + data);
});
serial.on('error', function (data) {
console.log('Serial error: ' + data);
});
serial.on('data', function (data) {
console.log('Serial Data: ' + data);
});
console.log('Press <ctrl>+C to exit.')