Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Created January 29, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joefiorini/cd479eb10c938225b2c2 to your computer and use it in GitHub Desktop.
Save joefiorini/cd479eb10c938225b2c2 to your computer and use it in GitHub Desktop.
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