Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

2.0 USB Communication

Tres Finocchiaro edited this page Feb 11, 2016 · 24 revisions

Compatibility

  • ✅ 2.0 | ⛔ 1.9 | ...

Read Data

  • Claims device based on hardware information
  • Reads data from device
  • Releases device
// Hardware info (modify to match hardware)
var usb = {
   vendor: '0x0EB8',
   product: '0xF000',
   interface: '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.interface);
}).then(function() {
   return qz.usb.readData(usb.vendor, usb.product, usb.endpoint, '8');
}).then(process).then(release).catch(err);

Send Data

  1. Send data to a claimed USB device sendData(vendorId, productId, endpoint, data)

    function sendUsbData() {
        qz.usb.sendData('0x0EB8', '0xF000', '0x81', data).catch(displayError);
    }
qz.print(config, data).catch(function(e) { console.error(e); });

### Process Data
#### USB Scale
 * 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](#read-data) example above.

```javascript
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);
}

Advanced

Get nth

Although not advised for selecting a device, it is often useful to selecting the nth interface or the nth endpoint for a particular device.

// Generic error handler
var err = function(e) { console.error(e); }

// Generic data handler
var process = function(data) { console.log(data); }

// Stores device information
var usb = { vendor: null, product: null, interface: null, endpoint: null };

qz.websocket.connect().then(function() {

   // First device
   return qz.usb.listDevices(false);
}).then(function(devices) {
   usb.vendor = "0x" + devices[0].vendorId;
   usb.product = "0x" + devices[0].productId;

   // First interface
   return qz.usb.listInterfaces(usb.vendor, usb.product);
}).then(function(interfaces) {
   usb.interface = "0x" + interfaces[0];

   // First endpoint
   return qz.usb.listEndpoints(usb.vendor, usb.product, usb.interface);
}).then(function(endpoints) {
   usb.endpoint= "0x" + endpoints[0];
   return qz.usb.claimDevice(usb.vendor, usb.product, usb.interface);

  // process, release, err
}).then(function() {
   return qz.usb.readData(usb.vendor, usb.product, usb.endpoint, '8');
}).then(process).then(release).catch(err);