Skip to content

Instantly share code, notes, and snippets.

@rlemon

rlemon/111.js Secret

Created April 22, 2014 20:49
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 rlemon/15477c1e3de7c15992eb to your computer and use it in GitHub Desktop.
Save rlemon/15477c1e3de7c15992eb to your computer and use it in GitHub Desktop.
/*
so the server.js starts and runs
the init function in device.js
That function sets up the serial communications and calls a read on the SN provided from the device.
Using that SN for the port number in this example.
The issue at hand is this:
there is a unknown delay period where the serialport it open but it is sending back
garbage data that doesn't meet the checksum or just times out (expected)
so there are a handful of errors in the read function before I get the SN back.
for ever error I see, the group.process method is called (idk why? shouldn't it only call for the passing read?)
which leads to me listening on the same port for as many errors as I see (dozen +)
Why is it doing this?
*/
function init(cb) {
// setting up the serial connection.
getSN(cb);
}
function getSN(cb) {
read({
start: 149,
length: 1,
process: function(res) {
var serialPort = res.values[0] * 256 + res.values[1];
if( serialPort > 1024 ) { // valid value and not garbage
exec('some ssh command here', puts);
cb(serialPort);
} else {
getSN(cb);
}
}
});
}
function read(group, cb) {
var req = master.readHoldingRegisters(group.start, group.length, {timeout: 1000,unit: 1});
req.on('complete', function(err, res) {
if( err ) {
console.log("READ ERROR", err);
errors.increment(); // ignore me
read(group, cb);
return;
}
errors.reset(); // ignore me
group.process(res, group);
if( cb ) { // i'm used for another part of the program, you can ignore me as well.
cb();
}
}).on('error', function(err){
// do nothing here. errors are handled above but the library insists I put this
});
}
device.init(start);
function start(devicePort) {
port = devicePort;
app.listen(port);
console.log( "app listening on port " + port );
// other shit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment