Created
January 29, 2016 14:44
-
-
Save joefiorini/cd479eb10c938225b2c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
var SerialPort = require('serialport').SerialPort; | |
var serialPort = new SerialPort(process.env['DEVICE'], { | |
baudRate: 19200, | |
}); | |
const contents = fs.readFileSync(process.env.COMMANDS, { encoding: 'utf-8' }); | |
const commands = contents.split('\n'); | |
function runCommand(command, rest, done) { | |
if (command === undefined) { | |
done(); | |
return; | |
} | |
console.log(`Writing command: ${command}`); | |
serialPort.write(`${command} `, (err, results) => { | |
console.log('err: ', err); | |
console.log('results: ', results); | |
}); | |
const [next, ...remaining] = rest; | |
console.log('Pausing between commands...'); | |
setTimeout(() => runCommand(next, remaining, done), 50); | |
} | |
serialPort.on('open', () => { | |
serialPort.on('data', data => { | |
console.log('data received: ', data.toString('utf-8')); | |
}); | |
new Promise((resolve, reject) => { | |
const [firstCommand,...rest] = commands; | |
runCommand(firstCommand, rest, resolve); | |
}) | |
.then(() => serialPort.close()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment