Skip to content

Instantly share code, notes, and snippets.

@HERRKIN
Created June 3, 2014 02:11
Show Gist options
  • Save HERRKIN/88c6b489954bb10d7d26 to your computer and use it in GitHub Desktop.
Save HERRKIN/88c6b489954bb10d7d26 to your computer and use it in GitHub Desktop.
function send(d){
var p = "\x02"+d+"\x0D";//adds STX y ETX
var timeout;
var deferred = Q.defer();
serialPort.on('data', function (data) {
//console.log('RECEIVED: ' + data );
data=''+data;
deferred.resolve(data.trim())
});
console.log("\nSENDING: "+p+'\n');+
serialPort.write(p, function (err, results) {
if (err)
deferred.reject(new Error(err))
});
timeout=setTimeout(function(){
console.log('TIMEOUT'.blue)
deferred.reject(new Error('TIMEOUT NO ANSWER FROM SERIAL PORT'.red))
},500);
return deferred.promise
}
function activate(d){
var def = Q.defer();
var deco=d;
send("06"+d)
.then(function (d) {
//console.log('doing activation\n');
//console.log('data: '+d+' len: '+d.length);
d = d.substr(1,d.length-1);
//console.log('recibo: '+d+',');
if(d!="00"){
def.reject(new Error ('recieved an error\n'.red+d))
}
else{
console.log('activated data: '+d);
// console.log(serialPort.path)
def.resolve(deco,d)
}
return def.promise
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment