2010/9/10 Gao Hong <joy.highl...@gmail.com>:
> I'm glad you reply so quickly,thanks for you advise.
> The prototype is already setup in the function _setup_prototypes
> like this:
>>
>> # int libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
>>
>>     #                                         uint8_t desc_index,
>>
>>     #                                         unsigned char *data,
>>
>>     #                                         int length)
>>
>>     lib.libusb_get_string_descriptor_ascii.argtypes = [
>>
>>             _libusb_device_handle,
>>
>>             c_uint8,
>>
>>             POINTER(c_ubyte),
>>
>>             c_int
>>
>>         ]
>
> So,this is not the point where the error leads to.
> Frankly speaking,I knew nothing about the ctypes,and almost forget
> everything about the the C/C++ programming,the low level debug or C
> programming is too hard for me....
> Here is my script:
>>
>> import usb.core
>
> def InterfaceFilter(dev,ItfClass = 8):
>
>     """"""
>
>     try:
>
>         for cfg in dev:
>
>             try:
>
>                 inners = usb.util.find_descriptor(cfg,find_all = True)
>
>                 for inner in inners:
>
>                     if type(inner) == usb.core.Interface:
>
>                         print 'dev:%s:%s' % (dev,inner.bInterfaceClass)
>
>                         return inner.bInterfaceClass == ItfClass
>
>             except:
>
>                 False
>
>     except:
>
>         False
>
> usb_devs = usb.core.find(find_all = True,custom_match = InterfaceFilter)
>
> print usb_devs
>
> for dev in usb_devs:
>
>     print dev
>
>     print 'Vendor:',dev.idVendor
>
>     print 'Product:',dev.idProduct

Ah, now I see what's the problem, internally PyUSB manages device
opening automatically. Try a call to set_configuration at
this point of the code:

dev.set_configuration()

Only to force device opening...

>
>     print
> 'Manufacturer:',dev._ctx.backend.get_string_descriptor_ascii(dev._ctx.handle,dev.iManufacturer)
>
> This is the funcition I added to the libusb10.py Class _LibUSB:
>>
>> @methodtrace(_logger)
>>
>>
>>  #----------------------------------------------------------------------
>>
>>     def get_string_descriptor_ascii(self,dev_handle,desc_index):
>>
>>         """Added by GaoHong"""
>>
>>         buffer = array.array('B','\x00' * 255)
>>
>>         address, length = buffer.buffer_info()
>>
>>
>>  _check(_lib.libusb_get_string_descriptor_ascii(dev_handle,desc_index,cast(address,
>> POINTER(c_ubyte)),length))
>>
>>         return data
>
> I really not know what to do....
> P.S the code of :
>>
>> def getStringDescriptor(device, index):
>>
>>     """
>>
>>     """
>>
>>     response = device.ctrl_transfer(usb.util.ENDPOINT_IN,
>>
>>                                     usb.legacy.REQ_GET_DESCRIPTOR,
>>
>>                                     (usb.util.DESC_TYPE_STRING << 8) |
>> index,
>>
>>                                     0, # language id
>>
>>                                     255) # length
>>
>>     # TODO: Refer to 'libusb_get_string_descriptor_ascii' for error
>> handling
>>
>>
>>
>>     return response[2:].tostring().decode('utf-16')
>
> Doesn't work on windows7 ,libusb windows backend, raise not implemtented
> error,I don't know why/
> 2010/9/10 Wander Lairson <wander.lair...@gmail.com>
>>
>> > Hello wander.lairson:
>> >       First of all,thanks for your contribution for
>> > the opensource community
>> > and the work for libusb, I appricate it very much.
>> >       Currently I want to fetch the serial number of the massive storage
>> > device,which need the string descriptor. Unfortunately,the python
>> > interface
>> > hasn't wrapped the API yet. I tried to implement it myself and copy some
>> > of
>> > the __read function code. Like this:
>> >>
>> >> def get_string_descriptor_ascii(self,dev_handle,desc_index):
>> >>
>> >>         """Added by GaoHong"""
>> >>
>> >>         buffer = array.array('B','\x00' * 255)
>> >>
>> >>         address, length = buffer.buffer_info()
>> >>
>> >>
>> >>
>> >>  _check(_lib.libusb_get_string_descriptor_ascii(dev_handle,desc_index,cast(address,
>> >> POINTER(c_ubyte)),length))
>>
>> I am not sure if it is the problem (because I don't know how ctypes
>> pushes arguments in the stack), but if you haven't supplied the
>> function prototype, desc_index will be treated as an integer instead
>> of a 8 bits variable (as in the libusb_get_string_descriptor_ascii
>> interface). You might try this to see if it is a problem:
>>
>>
>> _check(_lib.libusb_get_string_descriptor_ascii(dev_handle,c_uint8(desc_index),cast(address,POINTER(c_ubyte)),length))
>>
>> If not, I am affraid you will have to play with WinDBG to see what's
>> going on. Also, try to write a little C program that call
>> libusb_get_string_descriptor_ascii. I may cut off the possiblity of a
>> bug in libusb.
>>
>> PS: I could debug it for you, but currently I do have no free time for
>> it, I am really sorry.
>>
>> >>
>> >>         return data
>> >
>> > but the result always raise WindowsError: exception: access violation
>> > reading 0x00000010
>> > File "E:\my_doc\My Dev\python\GWP 0902\usb_finder.py", line 60, in
>> > <module>
>> >   print
>> >
>> > 'Manufacturer:',back.get_string_descriptor_ascii(dev._ctx.handle,dev.iManufacturer)
>> > File "D:\Python26\Lib\site-packages\usb\_debug.py", line 53, in do_trace
>> >   return f(*args, **named_args)
>> > File "D:\Python26\Lib\site-packages\usb\backend\libusb10.py", line 593,
>> > in
>> > get_string_descriptor_ascii
>> >
>> >   _check(_lib.libusb_get_string_descriptor_ascii(dev_handle,desc_index,cast(address,
>> > POINTER(c_ubyte)),length-1))
>> > I don't know why,the only difference from __read function:
>> >>
>> >> def __read(self, fn, dev_handle, ep, intf, size, timeout):
>> >>
>> >>         buffer = array.array('B', '\x00' * size)
>> >>
>> >>         address, length = buffer.buffer_info()
>> >>
>> >>         transferred = c_int()
>> >>
>> >>         _check(fn(dev_handle,
>> >>
>> >>                   ep,
>> >>
>> >>                   cast(address, POINTER(c_ubyte)),
>> >>
>> >>                   length,
>> >>
>> >>                   byref(transferred),
>> >>
>> >>                   timeout))
>> >>
>> >>         return buffer[:transferred.value]
>> >
>> > is the called function.
>> > Can you tell me why? Thank you very much.
>> > --
>> > It's a long journey to go! :-)
>> > Legend since 1985...
>> >
>>
>>
>>
>> --
>> Best Regards,
>> Wander Lairson Costa
>
>
>
> --
> It's a long journey to go! :-)
> Legend since 1985...
>



-- 
Best Regards,
Wander Lairson Costa

------------------------------------------------------------------------------
Automate Storage Tiering Simply
Optimize IT performance and efficiency through flexible, powerful, 
automated storage tiering capabilities. View this brief to learn how
you can reduce costs and improve performance. 
http://p.sf.net/sfu/dell-sfdev2dev
_______________________________________________
pyusb-users mailing list
pyusb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyusb-users

Reply via email to