2012/1/26 bibek chitrakar <bibek.chitra...@gmail.com>:
> Hello Wander,
>
> Now I am very much confused about everythin going on with Pyusb.
> 1. First with installation.
>     I installed ----> libusb-1.-00 from synaptic
>                        >
> 'http://sourceforge.net/projects/pyusb/files/latest/download'
>                            got the pyusb files, unzipped and installed as
>                            $ sudo python setup.py install
>
>> The following code works.
>> =========================================================
>>>
>>> import usb.core
>>> import usb.util
>>>
>>> # find our device
>>> dev = usb.core.find(idVendor=0x0dd4, idProduct=0x01a8)
>>>
>>> # was it found?
>>> if dev is None:
>>>     raise ValueError('Device not found')
>>>
>>> # set the active configuration. With no arguments, the first
>>> # configuration will be the active one
>>> dev.set_configuration()
>>>
>>> # get an endpoint instance
>>> cfg = dev.get_active_configuration()
>>> interface_number = cfg[(0,0)].bInterfaceNumber
>>> alternate_setting = usb.control.get_interface(dev,interface_number)
>>> intf = usb.util.find_descriptor(cfg, bInterfaceNumber = interface_number,
>>>                                 bAlternateSetting = alternate_setting
>>> )
>>>
>>> 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 ep is not None
>>>
>>> # write the data
>>> print ep.write('\x10')
>>> print ep.read(1,0)
>>
>>
>> =========================================================
>> Output is:
>>
>>> $ sudo python hardware_monitor_printer.py
>>> bytes written:  1
>>> bytes read:  array('B', [0])
>
>

Here you are using PyUSB 1.0, which read and write function
automatically detect the endpoint type, so it is expected to work

> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
>
> Then I tried my original code.
> =========================================================
>
>> #!/usr/bin/python
>>
>>
>> import usb
>> import time
>>
>> class HardwareMonitor_printer(object):
>>     def __init__(self, vendor_id, product_id):
>>         self.device_found = False # True if device found
>>         busses = usb.busses() # enumerate busses
>>         self.handle = None
>>         for bus in busses:
>>                 devices = bus.devices
>>
>>                 for dev in devices:
>>                     if dev.idVendor == vendor_id and dev.idProduct ==
>> product_id: # device matches
>>                         self.dev = dev
>>                         self.conf = self.dev.configurations[0]
>>                         self.intf = self.conf.interfaces[0][0]
>>                         self.endpoints = []
>>                         for endpoint in self.intf.endpoints:
>>
>> self.endpoints.append(endpoint)
>>                         self.device_found = True
>>                         return
>>
>>     def open(self):
>>         if self.handle:
>>             self.handle = None
>>         try:
>>             self.handle = self.dev.open()
>>             if self.handle.is_kernel_driver_active(self.intf):
>>                 try:
>>                     self.handle.detach_kernel_driver(self.intf)
>>                 except Exception as e:
>>                     print "ERROR: Could not detach device from kernel:", e
>>                 else:
>>                     self.handle.claimInterface(self.intf)
>>                     self.handle.setConfiguration(self.conf)
>>                     self.handle.setAltInterface(self.intf)
>>             return True
>>         except:
>>             return False
>>
>>     def write(self, ep , buff, timeout=100):
>>         try:
>>             return self.handle.interruptWrite(ep, buff, timeout) #return
>> bytes written
>>         except usb.USBError as e:
>>             print e
>>             return 0
>>
>>     def read(self, ep, size, timeout=100):
>>         try:
>>             return self.handle.interruptRead(ep, size, timeout) # return
>> data read
>>         except:
>>             return []
>>
>>     def getDeviceName(self):
>>         try:
>>             for i in range (1, 4):
>>                 print self.handle.getString(i,25)
>>         except IOError as e:
>>             print e
>>
>>     def endpoint(self):
>>             return self.endpoints
>>
>> if __name__ == '__main__':
>>     printer = HardwareMonitor_printer(0xdd4,0x1a8)
>>     if printer.device_found:
>>         printer.open()
>>         print 'endpoints: ', (printer.endpoints)
>>         printer.getDeviceName()
>>         try:
>>             bytes_written = printer.write(1, [0x10], 100)
>>             print 'bytes written: ', bytes_written
>>         except usb.USBError, e:
>>             print e
>>         bytes_read = printer.read(0x81, 64)
>>         print 'bytes read: ', bytes_read
>>
>>     else:
>>         print 'device not found!!!'
>
>
> =========================================================
> It gives the device info but also errors.
>
>> $ sudo python hardware_monitor_printer.py
>> endpoints:  [<usb.legacy.Endpoint object at 0x8b46f2c>,
>> <usb.legacy.Endpoint object at 0x8b46eac>]
>>
>> CUSTOM Engineering S.p.A.
>> TG2480-H
>> Self Power Mode
>> Traceback (most recent call last):
>>   File "hardware_monitor_printer.py", line 122, in <module>
>>     bytes_written = printer.write(1, [0x10], 100)
>>   File "hardware_monitor_printer.py", line 94, in write
>>
>>     return self.handle.interruptWrite(ep, buff, timeout) #return bytes
>> written
>>   File "/usr/local/lib/python2.6/dist-packages/usb/legacy.py", line 171,
>> in interruptWrite
>>     return self.dev.write(endpoint, buffer, self.__claimed_interface,
>> timeout)
>>   File "/usr/local/lib/python2.6/dist-packages/usb/core.py", line 613, in
>> write
>>     fn = fn_map[self._ctx.get_endpoint_type(self, endpoint, intf)]
>>   File "/usr/local/lib/python2.6/dist-packages/usb/core.py", line 183, in
>> get_endpoint_type
>>     etype = util.endpoint_type(e.bmAttributes)
>> AttributeError: 'NoneType' object has no attribute 'bmAttributes'
>
>

Here you are using PyUSB 0.4, try to replace
interruptWrite/interruptRead by bulkWrite/bulkRead.

>
> 2. Now if I install python-usb from synaptic.
>     It only gives error like this with my original code( the code with
> 'import usb'...)
>
>> $ sudo python hardware_monitor_printer.py
>> endpoints:  [<usb.Endpoint object at 0xb7805090>, <usb.Endpoint object at
>> 0xb7805080>]
>>
>> CUSTOM Engineering S.p.A.
>> TG2480-H
>> Self Power Mode
>> error submitting URB: No such file or directory
>> bytes written:  0
>> bytes read:  []
>

This is still the wrong endpoint type problem, I guess.

>
> 3. So where am I missing.
>
> Did I installed files correctly ??
> Do I need to install python-usb from synaptic or not ?
>

I think you are doing interrupt transfers when you should use bulk
transfers, as I said earlier.

> 4. This is my device info.
>>
>> Device: 004
>>   Device class: 0
>>   Device sub class: 0
>>   Device protocol: 0
>>   Max packet size: 64
>>   idVendor: 3540
>>   idProduct: 424
>>   Device Version: 01.19
>>   Configuration: 1
>>     Total length: 32
>>     selfPowered: 1
>>     remoteWakeup: 0
>>     maxPower: 4
>>     Interface: 0
>>     Alternate Setting: 0
>>       Interface class: 7
>>       Interface sub class: 1
>>       Interface protocol: 2
>>       Endpoint: 0x81L
>>         Type: 2
>>         Max packet size: 64
>>         Interval: 0
>>       Endpoint: 0x2L
>>         Type: 2
>>         Max packet size: 64
>>         Interval: 0
>
> Got this info through usbenum.py and it says written by Wander Lairson Costa

Again, endpoint type is 2, which means bulk.

>
> 5. Since I have my device info, endpoint info(above), why cant I write into
> my device.
>     If the parameters for write(self, ep , buff, timeout=100) is wrong
>     Please somebody find a way out to work this code.
>
> If I lag any information please feel free to ask for any information.
>
> Regards,
> Bibek Chitrakar
>
>
>
>



-- 
Best Regards,
Wander Lairson Costa

------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
pyusb-users mailing list
pyusb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyusb-users

Reply via email to