Guest User

Untitled

a guest
Sep 30th, 2014
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. #!/usr/bin/python
  2. #-*- coding: UTF-8 -*-
  3. import os, time
  4. import usb.core
  5. import usb.util
  6. from sys import exit
  7.  
  8. #DYMO M25
  9. VENDOR_ID = 0x0922
  10. PRODUCT_ID = 0x8004
  11.  
  12. def connect():
  13.     # find the USB device
  14.     dev = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
  15.     if dev is None:
  16.         print("device not found")
  17.     else:
  18.         devmanufacturer = usb.util.get_string(dev, dev.iManufacturer)
  19.         devname = usb.util.get_string(dev,  dev.iProduct)
  20.         serialno = usb.util.get_string(dev, dev.iSerialNumber)
  21.         print("device found: ", devmanufacturer, devname, serialno)
  22.  
  23.         interface = 0
  24.         try:
  25.             dev.detach_kernel_driver(interface)
  26.         except usb.core.USBError as e:
  27.             exit("Kernel driver won't give up control over device: %s" % str(e))
  28.         try:
  29.             # use the first/default configuration
  30.             dev.set_configuration()
  31.             dev.reset()
  32.             usb.util.claim_interface(dev, interface)
  33.             print("claiming device")
  34.         except:
  35.             exit("Cannot set configuration the device: %s" % str(e))
  36.     return dev
  37.  
  38. def disconnect(dev):
  39.     interface = 0
  40.     print('release claimed interface')
  41.     usb.util.release_interface(dev, interface)
  42.     print('now attaching the kernel driver again')
  43.     dev.attach_kernel_driver(0)
  44.     print('device disconnected')
  45.          
  46. def analyzeData(data):
  47.     DATA_MODE_GRAMS = 2
  48.     DATA_MODE_OUNCES = 11
  49.  
  50.     weight = 0
  51.    
  52.     if data != None:
  53.         raw_weight = data[4] + data[5] * 256
  54.  
  55.     if raw_weight > 0 and data[1] == 4:
  56.         if data[2] == DATA_MODE_OUNCES:
  57.             ounces = raw_weight * 0.1
  58.             weight = "%s oz" % ounces
  59.         elif data[2] == DATA_MODE_GRAMS:
  60.             grams = raw_weight
  61.             #convert to ounces
  62.             grams = round(grams / 28.3495, 2)
  63.             weight = "%s oz" % grams
  64.  
  65.         print("stable weight: " + weight)
  66.  
  67.     if raw_weight == 0:
  68.         print('All zeros')
  69.  
  70.  
  71. def grabData(dev):
  72.     try:
  73.         # first endpoint
  74.         endpoint = dev[0][(0,0)][0] #get the first endpoint for the first interface and alt setting for the first configuration
  75.         # read a data packet
  76.         attempts = 5
  77.         data = []
  78.         while (data is None or len(data) < 6) and attempts > 0:
  79.             try:
  80.                 data = dev.read(endpoint.bEndpointAddress,
  81.                                 endpoint.wMaxPacketSize,
  82.                                 timeout=2000)
  83.                 print(data, len(data), type(data))
  84.             except usb.core.USBError as e:
  85.                 data = None
  86.                 print(str(e.args))
  87.                 if e.args[0] == 110: #timeout
  88.                     attempts -= 1
  89.                     print("timed out... trying again")
  90.                     continue
  91.                 elif e.args[0] is None:
  92.                     print("Error: ", e.args[1])
  93.                     continue
  94.                 #if e.args[0] == libusb._libusb_errno[libusb.LIBUSB_ERROR_PIPE]:
  95.                 #    print("broken pipe" )
  96.                 #if e.args[0] == libusb._libusb_errno[libusb.LIBUSB_ERROR_TIMEOUT]:
  97.                 #    attempts -= 1
  98.                 #    print("timed out... trying again")
  99.                 #    continue
  100.  
  101.         return data
  102.     except usb.core.USBError as e:
  103.         print("USBError: " + str(e.args))
  104.     except IndexError as e:
  105.         print("IndexError: " + str(e.args))
  106.  
  107. #############main################
  108. while True:
  109.     dev = connect()
  110.     if dev is not None:
  111.         print('Connected')
  112.         data = grabData(dev)
  113.         print('Got data: ' + str(data))
  114.         analyzeData(data)
  115.         print('Data Analyzed')
  116.         print(str(grabData(dev)))
  117.         print('data cleared')
  118.         disconnect(dev)
Advertisement
Add Comment
Please, Sign In to add comment