Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a966d5ddc8
Fetching contributors…

Cannot retrieve contributors at this time

executable file 65 lines (51 sloc) 1.506 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
import usb.core
import usb.util

class Scale:
device = None
endpoint = None

MODE_GRAMS = 0x02
MODE_OUNCES = 0x0B
INTERFACE = 0

def __init__(self, vendor_id, product_id):
# find the USB device
self.device = usb.core.find(idVendor=vendor_id, idProduct=product_id)

#Get rid of kernal driver
try:
self.device.detach_kernel_driver(self.INTERFACE)
except usb.core.USBError:
print "USBError when detaching kernel driver. It's probably already detached. "

# use the first/default configuration
self.device.set_configuration()
# first endpoint
self.endpoint = self.device[0][(0,0)][0]

def readOunces(self):
data = self.readData()
return (self.readGrams() / 28.3495);

def readGrams(self):
data = self.readData()

grams = (data[4] + (256 * data[5]))
if(0x05 == data[1]):
grams = -grams

if(data[2] == self.MODE_GRAMS):
return grams

return ((grams*0.1) * 28.3495)

def readData(self):
# read a data packet
attempts = 10
data = None
while data is None and attempts > 0:
try:
data = self.device.read(self.endpoint.bEndpointAddress,
self.endpoint.wMaxPacketSize)
except usb.core.USBError as e:
data = None
if e.args == ('Operation timed out',):
attempts -= 1
continue
return data

def writeData(self, data):
self.device.write(self.endpoint.bEndpointAddress, data)

#scale = Scale(VENDOR_ID, PRODUCT_ID)
#print scale.readData()
#print scale.readOunces()

# print scale.writeData([0x00, 0x00, 0x00, 0x00])
Something went wrong with that request. Please try again.