This repository has been archived by the owner on Dec 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
2.0 USB Communication
Tres Finocchiaro edited this page Jan 13, 2016
·
24 revisions
- Claims device based on hardware information
- Reads data from device
- Releases device
// Hardware info (modify to match hardware)
var usb = {
vendor: '0x0EB8',
product: '0xF000',
device: '0x00',
endpoint: '0x81'
};
// Generic error handler
var err = function(e) { console.error(e); }
// Generic data handler
var process = function(data) { console.log(data); }
// Handler to release claimed device
var release = function() {
qz.usb.releaseDevice(usb.vendor, usb.product).catch(err);
}
// Connect to QZ Tray, claim, read, release
qz.websocket.connect().then(function() {
return qz.usb.claimDevice(usb.vendor, usb.product, usb.device);
}).catch(err).then(function() {
return qz.usb.readData(usb.vendor, usb.product, usb.endpoint, '8');
}).then(process).then(release);
- FIXME
//
- Parses data returned from USB port to determine status, weight, units and precision. Compatible with most USB attached scales (Stamps.com, Mettler Toledo, Dymo, etc).
- Requires read data example above.
var process = function(data) {
// Get status
var status = parseInt(data[1], 16);
switch(status) {
case 1: // fault
case 5: // underweight
case 6: // overweight
case 7: // calibrate
case 8: // re-zero
status = 'error'; break;
case 3:
status = 'busy'; break;
case 2: // stable at zero
case 4: // stable non-zero
default:
status = 'stable';
}
// Get precision
var precision = parseInt(data[3], 16);
precision = precision ^ -256; // unsigned to signed
// Get units
var units = parseInt(data[2], 16);
switch(units) {
case 3:
units = 'kg'; break;
case 11:
units = 'oz'; break;
case 12:
default:
units = 'lbs';
}
// Get weight
data.splice(0,4);
data.reverse();
weight = parseInt(data.join(''), 16);
// Apply precision
weight *= Math.pow(10, precision);
weight = weight.toFixed(Math.abs(precision));
// Log data to the console
console.log(status + ": " + weight + units);
}