Python Forum
Pyusb freeze when using Dev.read(nr,nr,timeout=1000) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Pyusb freeze when using Dev.read(nr,nr,timeout=1000) (/thread-1526.html)



Pyusb freeze when using Dev.read(nr,nr,timeout=1000) - bojane - Jan-10-2017

Shouldn't timeout release after 1sek ? As of now the code just halt with no error or anything.


RE: Pyusb freeze when using Dev.read(nr,nr,timeout=1000) - Larz60+ - Jan-10-2017

?? Is there a question here?

How about some code,
an error traceback,
and a question


RE: Pyusb freeze when using Dev.read(nr,nr,timeout=1000) - bojane - Jan-11-2017

Well, why do python freeze when I call: self.dev2.read(self.endpoint2.bEndpointAddress,self.endpoint2.wMaxPacketSize,timeout=120)  ? What can I do to find out ? The call is in function: readUsbDPS()
 

import usb.core
import os
import usb.util
import time
import sys
from commandParser import commandParser_Class
from commandParserDPS import command_ParserDPS
os.environ['PYUSB_DEBUG'] = 'debug'
#import d2xx
#VENDOR_ID = 0x0922
#PRODUCT_ID = 0x8003

class slider_DPS_Class:

    def __init__(self):
        self.devT = usb.core.find(find_all=True, idVendor=7931)

        for d in self.devT:
            print (usb.util.get_string(d,2))
            x=usb.util.get_string(d,2)
            print (d.idProduct,d.idVendor)

            if (x.find('SLID')>=0):
                self.dev=d
                self.dev.reset()
                print('satt slid')

            elif (x.find('DPS')>=0):
                self.dev2=d
                print(d)
                usb.util.dispose_resources(self.dev2)
                self.dev2.reset()
                print('satt dps')

# ----------------------  usb setup ------------------------------------
        #self.dev2=usb.core.find(idVendor=0x1efb, idProduct=0x1590)
        self.endpoint = self.dev[0][(0, 0)][0]

        if self.dev is None:
            raise ValueError('Device not found')
            reattach = False
        if self.dev.is_kernel_driver_active(0):
            reattach = True
            self.dev.detach_kernel_driver(0)

        self.dev.set_configuration()

        # get an endpoint instance
        cfg = self.dev.get_active_configuration()
        intf = cfg[(0, 0)]

        self.ep = usb.util.find_descriptor(
            intf,
            # match the first OUT endpoint
            custom_match=lambda e: \
            usb.util.endpoint_direction(e.bEndpointAddress) == \
            usb.util.ENDPOINT_OUT)

        assert self.ep is not None
        self.commandX = commandParser_Class()

        #print('satt dpssss')
# ------------------------------end ---------------------------------------

# -----------------------------dps usb setup -------------------------------

#self.dev2=usb.core.find(idVendor=0x1efb, idProduct=0x1590)
        self.endpoint2 = self.dev2[0][(0, 0)][0]

        if self.dev2 is None:
            raise ValueError('Device not found')
            reattach = False
        if self.dev2.is_kernel_driver_active(0):
            reattach = True
            self.dev2.detach_kernel_driver(0)

        self.dev2.set_configuration()

        # get an endpoint instance
        cfg2 = self.dev2.get_active_configuration()
        intf2 = cfg2[(0, 0)]

        self.ep2 = usb.util.find_descriptor(
            intf,
            # match the first OUT endpoint
            custom_match=lambda e: \
            usb.util.endpoint_direction(e.bEndpointAddress) == \
            usb.util.ENDPOINT_OUT)

        assert self.ep2 is not None
        self.commandXdps = command_ParserDPS()
        self.get_L_dps()
#-------------------------------end ------------------------------------------

# --------------------------------- sli ----------------------------------

    def init1(self):
        hexCommand = (self.commandX.sendCommand(
            "SETUSEFORCEDPOSITION", 1))  # sld obey pos-values
        self.ep.write(hexCommand)
        time.sleep(0.2)
        self.get_L_Cstrokeum()
        self.get_L_Cstrokeum()
        # hexCommand = (commandX.sendCommand("SETUSEFORCEDPOSITION", 0)) # sld obey pos-values
        # ep.write(hexCommand)

        time.sleep(1)


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


    



# -------------------------------- dps -------------------------------------
    def readUsbDPS(self):
        data = None
        attempts = 10
        while data is None and attempts > 0:
            try:

                data = self.dev2.read(self.endpoint2.bEndpointAddress,
                                self.endpoint2.wMaxPacketSize,timeout=120)
            except usb.core.USBError as e:
                data = None
                if e.args == ('Operation timed out',):
                    attempts -= 1
                    continue
        return data

    def get_L_dps(self):
        try:
            hexCommand = (self.commandXdps.getCommand('T_GET_DP_PHYS'))
            print('hexcommand')
            print(hexCommand)
            #'\x01\x36\x04\x00\x00\x13\x88'
            print('bytestring')
            print('\xFC\x16')
            for i in range(1,2):
                self.ep2.write('\xFC\x16\xFC\x16\xFC\x16\xFC\x16\xFC\x16')
                #self.ep2.write(64534)
                #self.ep2.write(hexCommand)
                print(self.endpoint2.bEndpointAddress)
                print(self.endpoint2.wMaxPacketSize)
            data = self.readUsbDPS()
            #return data
        except(usb.core.USBError):
            print('USBError get_L_command')
            self.commandX.iError()



RE: Pyusb freeze when using Dev.read(nr,nr,timeout=1000) - bojane - Jan-11-2017

Heres the output from python about the device:

INTERFACE 0: Human Interface Device ====================
    bLength            :    0x9 (9 bytes)
    bDescriptorType    :    0x4 Interface
    bInterfaceNumber   :    0x0
    bAlternateSetting  :    0x0
    bNumEndpoints      :    0x2
    bInterfaceClass    :    0x3 Human Interface Device
    bInterfaceSubClass :    0x0
    bInterfaceProtocol :    0x0
    iInterface         :    0x0
     ENDPOINT 0x81: Interrupt IN ==========================
      bLength          :    0x7 (7 bytes)
      bDescriptorType  :    0x5 Endpoint
      bEndpointAddress :   0x81 IN
      bmAttributes     :    0x3 Interrupt
      wMaxPacketSize   :   0x40 (64 bytes)
      bInterval        :   0x20
     ENDPOINT 0x1: Interrupt OUT ==========================
      bLength          :    0x7 (7 bytes)
      bDescriptorType  :    0x5 Endpoint
      bEndpointAddress :    0x1 OUT
      bmAttributes     :    0x3 Interrupt
      wMaxPacketSize   :   0x40 (64 bytes)
      bInterval        :   0x20