mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-26 02:47:17 +00:00
We refer to the data as path everywhere but the port info object. This unifies them. Accessing `comName` will log a warning for one major release and go away afterwards.
52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
const { promirepl } = require('promirepl')
|
|
const repl = require('repl')
|
|
const SerialPort = require('serialport')
|
|
|
|
process.env.DEBUG = process.env.DEBUG || '*'
|
|
|
|
// outputs the path to an arduino or nothing
|
|
function findArduino() {
|
|
return new Promise((resolve, reject) => {
|
|
const envPort = process.argv[2] || process.env.TEST_PORT
|
|
if (envPort) {
|
|
return resolve(envPort)
|
|
}
|
|
SerialPort.list((err, ports) => {
|
|
if (err) {
|
|
return reject(err)
|
|
}
|
|
let resolved = false
|
|
ports.forEach(port => {
|
|
if (!resolved && /arduino/i.test(port.manufacturer)) {
|
|
resolved = true
|
|
return resolve(port.path)
|
|
}
|
|
})
|
|
if (!resolved) {
|
|
reject(
|
|
new Error(
|
|
'No arduinos found. You must specify a port to load.\n\nFor example:\n\tserialport-repl COM3\n\tserialport-repl /dev/tty.my-serialport'
|
|
)
|
|
)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
findArduino()
|
|
.then(portName => {
|
|
console.log(`port = SerialPort("${portName}", { autoOpen: false })`)
|
|
console.log('globals { SerialPort, portName, port }')
|
|
const port = new SerialPort(portName, { autoOpen: false })
|
|
const spRepl = repl.start({ prompt: '> ' })
|
|
promirepl(spRepl)
|
|
spRepl.context.SerialPort = SerialPort
|
|
spRepl.context.portName = portName
|
|
spRepl.context.port = port
|
|
})
|
|
.catch(e => {
|
|
console.error(e.message)
|
|
process.exit(1)
|
|
})
|