Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- #-*- coding: UTF-8 -*-
- import os, time
- import usb.core
- import usb.util
- from sys import exit
- #DYMO M25
- VENDOR_ID = 0x0922
- PRODUCT_ID = 0x8004
- def connect():
- # find the USB device
- dev = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
- if dev is None:
- print("device not found")
- else:
- devmanufacturer = usb.util.get_string(dev, dev.iManufacturer)
- devname = usb.util.get_string(dev, dev.iProduct)
- serialno = usb.util.get_string(dev, dev.iSerialNumber)
- print("device found: ", devmanufacturer, devname, serialno)
- interface = 0
- try:
- dev.detach_kernel_driver(interface)
- except usb.core.USBError as e:
- exit("Kernel driver won't give up control over device: %s" % str(e))
- try:
- # use the first/default configuration
- dev.set_configuration()
- dev.reset()
- usb.util.claim_interface(dev, interface)
- print("claiming device")
- except:
- exit("Cannot set configuration the device: %s" % str(e))
- return dev
- def disconnect(dev):
- interface = 0
- print('release claimed interface')
- usb.util.release_interface(dev, interface)
- print('now attaching the kernel driver again')
- dev.attach_kernel_driver(0)
- print('device disconnected')
- def analyzeData(data):
- DATA_MODE_GRAMS = 2
- DATA_MODE_OUNCES = 11
- weight = 0
- if data != None:
- raw_weight = data[4] + data[5] * 256
- if raw_weight > 0 and data[1] == 4:
- if data[2] == DATA_MODE_OUNCES:
- ounces = raw_weight * 0.1
- weight = "%s oz" % ounces
- elif data[2] == DATA_MODE_GRAMS:
- grams = raw_weight
- #convert to ounces
- grams = round(grams / 28.3495, 2)
- weight = "%s oz" % grams
- print("stable weight: " + weight)
- if raw_weight == 0:
- print('All zeros')
- def grabData(dev):
- try:
- # first endpoint
- endpoint = dev[0][(0,0)][0] #get the first endpoint for the first interface and alt setting for the first configuration
- # read a data packet
- attempts = 5
- data = []
- while (data is None or len(data) < 6) and attempts > 0:
- try:
- data = dev.read(endpoint.bEndpointAddress,
- endpoint.wMaxPacketSize,
- timeout=2000)
- print(data, len(data), type(data))
- except usb.core.USBError as e:
- data = None
- print(str(e.args))
- if e.args[0] == 110: #timeout
- attempts -= 1
- print("timed out... trying again")
- continue
- elif e.args[0] is None:
- print("Error: ", e.args[1])
- continue
- #if e.args[0] == libusb._libusb_errno[libusb.LIBUSB_ERROR_PIPE]:
- # print("broken pipe" )
- #if e.args[0] == libusb._libusb_errno[libusb.LIBUSB_ERROR_TIMEOUT]:
- # attempts -= 1
- # print("timed out... trying again")
- # continue
- return data
- except usb.core.USBError as e:
- print("USBError: " + str(e.args))
- except IndexError as e:
- print("IndexError: " + str(e.args))
- #############main################
- while True:
- dev = connect()
- if dev is not None:
- print('Connected')
- data = grabData(dev)
- print('Got data: ' + str(data))
- analyzeData(data)
- print('Data Analyzed')
- print(str(grabData(dev)))
- print('data cleared')
- disconnect(dev)
Advertisement
Add Comment
Please, Sign In to add comment