Skip to content

Instantly share code, notes, and snippets.

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 samarulrajt/1ea2bb4aa2e7fcf05e3a3b1e71081b4f to your computer and use it in GitHub Desktop.
Save samarulrajt/1ea2bb4aa2e7fcf05e3a3b1e71081b4f to your computer and use it in GitHub Desktop.
var callbackStack = [];
const parser = port.pipe(new ByteLength({length: 11}))
parser.on('data', function (data) {
if (callbackStack.length > 0) {
var callback = callbackStack.pop();
callback(null, data);
};
console.log('DATA RECEIVED: ', Buffer.from(data, 'hex'))
});
function sendToPort (data, callback) {
port.write(data, function (err, res) {
if (err) {
callbackStack.splice(callbackStack.indexOf(callback), 1);
callback(err);
};
console.log('DATA WRITTEN: ', data)
});
callbackStack.unshift(callback);
};
function bl(cb){
var async = require("async");
var hex = ['aa5501020304050607080a', 'aa5501020304050607080a', 'aa5501020304050607080a'];
port.open(function(err){
var i = 0;
async.eachSeries(hex, function(item,next){
var buf = Buffer.from(item, 'hex');
sendToPort (buf, function(err,data){
var buf1 = Buffer.from(data, 'hex');
var buf2 = Buffer.from('aa5501020304050607080a', 'hex');
var x = Buffer.compare(buf1, buf2);
if(x === 0) next();
});
i++
},function(err){
console.log('ERROR:', err);
});
});
}
bl();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment