Last active
February 11, 2023 02:33
-
-
Save PhantomRay/cbccda0d3ab9a9f42d7346cc378d808b to your computer and use it in GitHub Desktop.
Read data from Dymo M10 scale using nodejs in Windows.
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
var HID = require('node-hid'), | |
usb = require('usb'); | |
var reading = false, | |
interval, | |
vid = 0x922, | |
pid = 0x8003; | |
// try to connect to the scale if available | |
startReading(); | |
usb.on('attach', function (device) { | |
if (device.deviceDescriptor.idVendor === vid && device.deviceDescriptor.idProduct === pid) { | |
console.log('Dymo M10 attached'); | |
interval = setInterval(startReading, 1000); | |
} | |
}); | |
usb.on('detach', function (device) { | |
if (device.deviceDescriptor.idVendor === vid && device.deviceDescriptor.idProduct === pid) { | |
console.log('Dymo M10 detached'); | |
reading = false; | |
clearInterval(interval); | |
} | |
}); | |
function startReading() { | |
if (reading) return; | |
try { | |
var d = new HID.HID(vid, pid); | |
console.log('Starting to read data from scale'); | |
reading = true; | |
d.on("data", function (data) { | |
var buf = new Buffer(data); | |
var grams = buf[4] + (256 * buf[5]); | |
console.log(new Date().toISOString() + ': ' + grams + ' grams'); | |
}); | |
d.on("error", function (error) { | |
console.log(error); | |
reading = false; | |
d.close(); | |
}); | |
} catch (err) { | |
if (/cannot open device/.test(err.message)) { | |
console.log('Dymo M10 cannot be found'); | |
} else | |
console.log(err); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment