-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportConnection.js
More file actions
34 lines (29 loc) · 909 Bytes
/
portConnection.js
File metadata and controls
34 lines (29 loc) · 909 Bytes
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
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline');
let path = ''
let Port = ''
// Promise approach
SerialPort.list().then(ports => {
let done = false
let count = 0
let allports = ports.length
ports.forEach(function(port) {
count = count+1
pm = port.manufacturer
console.log(pm)
if (typeof pm !== 'undefined' && pm.includes('Silicon')) {
path = port.path
Port = new SerialPort(path, { baudRate: 9600 })
const parser = Port.pipe(new Readline({ delimiter: '\n' }));// Read the port data
Port.on("open", () => {
console.log('serial port open');
});parser.on('data', data =>{
console.log('got word from arduino:', data);
});
done = true
}
if(count === allports && done === false){
console.log(`can't find any arduino`)
}
})
})