-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenPort.js
31 lines (25 loc) · 874 Bytes
/
OpenPort.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
var serialport = require('serialport');// include the library
SerialPort = serialport.SerialPort; // make a local instance of it
// get port name from the command line:
portName = process.argv[2];
var myPort = new SerialPort(portName, {
baudRate: 9600,
// look for return and newline at the end of each data packet:
parser: serialport.parsers.readline("\r\n")
});
function showPortOpen() {
console.log('port open. Data rate: ' + myPort.options.baudRate);
}
function saveLatestData(data) {
console.log('data received: ' + data);
}
function showPortClose() {
console.log('port closed.');
}
function showError(error) {
console.log('Serial port error: ' + error);
}
myPort.on('open', showPortOpen);
myPort.on('data', saveLatestData);
myPort.on('close', showPortClose);
myPort.on('error', showError);