node.js - How do I decod node-hid data buffer from a&d scale -
i have a&d scale monitoring input using node-hid. reading input, can't figure out how decode binary data. appreciated.
this code using:
var hid = require('node-hid'); var devices = hid.devices(); var device = new hid.hid('usb_0dbc_0005_14400000'); device.on("data", function(data){ console.log(data); });
and gets spat out when scale @ zero.
<buffer 00 00 53 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00> <buffer 00 00 57 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00> <buffer 00 00 62 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00> <buffer 00 00 62 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00> <buffer 00 00 62 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00> <buffer 00 00 62 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00> <buffer 00 00 62 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00> <buffer 00 00 63 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00> <buffer 00 00 62 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00> <buffer 00 00 62 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00> <buffer 00 00 58 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00> <buffer 00 00 53 00 00 00 00 00> <buffer 00 00 00 00 00 00 00 00>
i ended writing buffer on whiteboard , thinking freely. found patterns in data , wrote code decoding , parsing scale data. code included below might need push in right direction.
"use strict"; var hid = require('node-hid'); var device = new hid.hid('usb_0dbc_0005_14400000'); var weight = [] var count = 0 module.exports.show_devices = function(){ console.log(devices); } module.exports.start_logging = function(){ device.on("data", function(data){ var bad_array = [ '\u0000s\u0000\u0000', '\u0000w\u0000\u0000', ] if (count < 20 && count % 2 == 0 && !contains(bad_array, data.tostring('utf16le'))){ weight.push(key[data.tostring('utf16le')]) } if(count == 19){ display_weight() } if(count == 23){ count = -1 weight = [] } count++ }); } function display_weight(){ console.log(weight.join('')); } function contains(a, obj){ for(var = 0; < a.length; i++){ if(a[i] === obj){ return true; } } return false; } var key = { "\u0000y\u0000\u0000" : "1", "\u0000z\u0000\u0000" : "2", "\u0000[\u0000\u0000" : "3", "\u0000\\\u0000\u0000" : "4", "\u0000]\u0000\u0000" : "5", "\u0000^\u0000\u0000" : "6", "\u0000_\u0000\u0000" : "7", "\u0000`\u0000\u0000" : "8", "\u0000a\u0000\u0000" : "9", "\u0000b\u0000\u0000" : "0", "\u0000c\u0000\u0000" : "." }
Comments
Post a Comment