Add util.js featuring bufferToString()

This commit is contained in:
László Monda
2016-09-27 16:08:00 +02:00
parent fcd1bc4f58
commit 67695151aa
2 changed files with 15 additions and 1 deletions

View File

@@ -2,6 +2,7 @@
'use strict'; 'use strict';
var usb = require('usb'); var usb = require('usb');
var util = require('./util');
var vid = 0x16d3; var vid = 0x16d3;
var pid = 0x05ea; var pid = 0x05ea;
@@ -34,7 +35,7 @@ setInterval(function() {
console.error("USB error: %s", err2); console.error("USB error: %s", err2);
process.exit(2); process.exit(2);
} }
console.log('Received', receivedBuffer, receivedBuffer.length); console.log('Received', util.bufferToString(receivedBuffer));
}) })
}); });
}, 500) }, 500)

13
usb/util.js Executable file
View File

@@ -0,0 +1,13 @@
exports = module.exports = {
bufferToString: function(buffer) {
var str = '';
for (var i = 0; i < buffer.length; i++) {
var hex = buffer[i].toString(16) + ' ';
if (hex.length <= 2) {
hex = '0' + hex;
}
str += hex;
};
return str;
}
}