Last active
July 19, 2021 19:51
-
-
Save tresf/8b8c6000d84032edbae090867a217ed6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://unpkg.com/qz-tray"></script> | |
<script src="https://unpkg.com/qz-sift"></script> | |
<label for="lb">lb: </label><input id="lb" type="text" readonly/> | |
<label for="oz">oz: </label><input id="oz" type="text" readonly/> | |
<script> | |
// The callback code when a response is received | |
qz.hid.setHidCallbacks(function (event) { | |
if(event.type === 'RECEIVE') { | |
var parsed = sift.parse.scale(event.output); | |
if(parsed.valid) { | |
if(parsed.units.value == 'lb') { | |
document.getElementById('lb').value = parsed.weight.value; | |
document.getElementById('oz').value = parsed.weight.value * 16; | |
} else { | |
document.getElementById('lb').value = "Wrong units!"; | |
console.warn("Value:", parsed); | |
} | |
} | |
} else { | |
document.getElementById('lb').value = 'ERROR'; | |
document.getElementById('oz').value = 'ERROR'; | |
} | |
}); | |
// Scale manufacturer. 0x0eb8|0xf000 = Mettler Toledo | |
var device = { vendorId: '0x0eb8', productId: '0xf000' }; | |
device.interval = 25; // interval between reads, in milliseconds | |
device.responseSize = 8; // response size, in bytes | |
qz.websocket.connect().then(() => { | |
return qz.hid.claimDevice(device); | |
}).then(() => { | |
return qz.hid.openStream(device); | |
}).catch(err => alert(err)); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment