node-serialport-bindings-cpp/bin/write-a-lot.js
Francis Gulotta 145b906d9c
feat: drop callback argument on SerialPort.list() (#1943)
Promises are now standard

BREAKING: SerialPort.list() now takes no arguments and always returns a promise.
2019-09-24 23:15:31 -04:00

40 lines
1,011 B
JavaScript
Executable file

#!/usr/bin/env node
process.env.DEBUG = '*'
const SerialPort = require('../packages/serialport')
// outputs the path to an arduino or nothing
async function findArduino() {
if (process.argv[2]) {
return process.argv[2]
}
const ports = await SerialPort.list()
for (const port of ports) {
if (/arduino/i.test(port.manufacturer)) {
return port.path
}
}
throw new Error('No arduinos found')
}
findArduino().then(
portName => {
const port = new SerialPort(portName)
port.on('open', () => {
console.log('opened', portName)
// port.write(Buffer.alloc(1024 * 20, 0));
port.on('data', data => console.log('data', data.toString())) // put the port into flowing mode
// setTimeout(() => {
// console.log('closing');
// port.close((err) => {
// console.log('closed?', err);
// });
// }, 5000);
})
},
() => {
console.log('no arduino')
}
)
process.on('unhandledRejection', r => console.log(r, r.stack))