Created
March 2, 2018 01:00
-
-
Save sharoonthomas/2bd2fabc0607c2d4f0cdae3a7c4c54ab to your computer and use it in GitHub Desktop.
Find weight from scale using python and usb
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
# -*- coding: utf-8 -*- | |
""" | |
scale.py | |
Utilities to fetch weight from weighing scales | |
:copyright: (c) 2016-2018 by Fulfil.IO Inc. | |
:license: see LICENSE for details. | |
""" | |
from collections import namedtuple | |
import hid | |
from pint import UnitRegistry | |
UREG = UnitRegistry() | |
DATA_MODE_GRAMS = 2 | |
DATA_MODE_KILOS = 3 | |
DATA_MODE_OUNCES = 11 | |
DATA_MODE_POUNDS = 12 | |
DATA_STABLE = 4 | |
SCALE_VENDOR_WHITELIST = [ | |
0x6096, # SANFORD | |
0x0922, # Dymo | |
0x1446, # WeighMax | |
0x0eb8, # Mettler Toledo | |
0x6096, # Pelouze | |
0x2474, # Stamps.com | |
0x0b67, # Fairbanks | |
] | |
class FulfilException(Exception): | |
pass | |
class ScaleReadError(FulfilException): | |
pass | |
def get_scale(product_string): | |
""" | |
Return the scale_hid with the given product name | |
""" | |
try: | |
scale_data, = filter( | |
lambda h: h['product_string'] == product_string, | |
get_scale_hid_devices() | |
) | |
except ValueError: | |
raise ScaleReadError('No scale selected.') | |
try: | |
scale_hid = hid.device( | |
scale_data['vendor_id'], | |
scale_data['product_id'], | |
) | |
scale_hid.open_path(scale_data['path']) | |
except IOError: | |
raise ScaleReadError('Error connecting to the scale') | |
if not scale_hid: | |
raise ScaleReadError('Error connecting to the scale') | |
return scale_hid | |
Weight = namedtuple('Weight', ['weight', 'stable', 'raw_data']) | |
def get_weight(scale_name=None): | |
""" | |
API which reads the weight from weighing scale | |
:return: A namedtuple | |
""" | |
scale_hid = get_scale(scale_name) | |
# Read from hid scale | |
raw_data = scale_hid.read(64) | |
scale_hid.close() | |
return ( | |
parse_weight(*raw_data[2:]), | |
raw_data[1] == DATA_STABLE, | |
raw_data, | |
) | |
def parse_weight(unit, factor, weight_part_1, weight_part_2): | |
""" | |
Compute and return the weight in multiple UOMs | |
""" | |
weight = 0.0 | |
raw_weight = weight_part_1 + (weight_part_2 * 256) | |
if unit == DATA_MODE_GRAMS: | |
weight = UREG.g * raw_weight | |
elif unit == DATA_MODE_KILOS: | |
weight = UREG.kg * raw_weight | |
elif unit == DATA_MODE_OUNCES: | |
weight = UREG.oz * raw_weight * 0.1 | |
elif unit == DATA_MODE_POUNDS: | |
weight = UREG.lbs * raw_weight * 0.1 | |
return weight | |
def get_scale_hid_devices(): | |
""" | |
Return a list of HID devices | |
""" | |
devices = [] | |
for device in hid.enumerate(): | |
if device['vendor_id'] in SCALE_VENDOR_WHITELIST: | |
devices.append(device) | |
return devices | |
if __name__ == '__main__': | |
scales = get_scale_hid_devices() | |
for scale in scales: | |
print "Manufacturer:", scale['manufacturer_string'] | |
print "Product:", scale['product_string'] | |
print get_weight(scale['product_string']) | |
if not scales: | |
print "No Scales Found" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a question, ¿Does it work on Linux?