Skip to content

Instantly share code, notes, and snippets.

@Vzor-
Created June 4, 2020 01:43
Show Gist options
  • Save Vzor-/d8a1cdb1b8bbaf1657a0df9b4cfef572 to your computer and use it in GitHub Desktop.
Save Vzor-/d8a1cdb1b8bbaf1657a0df9b4cfef572 to your computer and use it in GitHub Desktop.
/**
USB kick code for the posiflex 4000 series:
- 120 bytes including the report id
- all 0x00 with byte[1] and byte[2] the drawer number
- the drawers can be assigned a number 0-7 (default is 7)
**/
function openDrawer(number)
{
// Posiflex Cash Drawer
var deviceInfo = {
vendorId: '0x0d3a',
productId: '0x0207',
data: "",
};
// Drawer number 0-7 (7 is default)
var drawer = String.fromCharCode(number);
// ^-- Drawer number
var prepend = [drawer, drawer];
// Create 120 byte null-filled string (119 bytes data + 1 byte reportId)
var data = prepend.join('') + Array(120 - (prepend.length + 1)).fill("\u0000").join('');
// ^-- Report ID is appended by QZ Tray automatically
// Connect to QZ Tray, claim, write, release
qz.websocket.connect().then(function() {
return qz.hid.claimDevice(deviceInfo.vendorId, deviceInfo.productId);
}).then(function() {
return createDataChain(qz.hid.sendData, deviceInfo, data, 32);
}).catch(function(sendErr) {
console.error(sendErr);
}).then(function() {
return qz.hid.releaseDevice(deviceInfo.vendorId, deviceInfo.productId);
}).catch(function(releaseErr) {
console.error(sendErr);
});
}
function createDataChain(targetFunction, deviceInfo, data, chunkLength) {
var chain = Promise.resolve();
for (var i = 0; i < data.length; i += chunkLength) {
//copy object
let newInfo = {
vendorId: deviceInfo.vendorId,
productId: deviceInfo.productId,
usagePage: deviceInfo.usagePage,
serial: deviceInfo.serial,
data: "",
endpoint: deviceInfo.endpoint
};
newInfo.data = data.substring(i, Math.min(i + chunkLength, data.length));
console.log(newInfo.data);
chain = chain.then(()=>targetFunction(newInfo));
}
return chain;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment