Title: [Windows] usb.core.USBError: [Errno None] libusb0-dll:err [claim_interface] could not claim interface 1, invalid configuration 0 · Issue #46 · python-ivi/python-usbtmc · GitHub
Open Graph Title: [Windows] usb.core.USBError: [Errno None] libusb0-dll:err [claim_interface] could not claim interface 1, invalid configuration 0 · Issue #46 · python-ivi/python-usbtmc
X Title: [Windows] usb.core.USBError: [Errno None] libusb0-dll:err [claim_interface] could not claim interface 1, invalid configuration 0 · Issue #46 · python-ivi/python-usbtmc
Description: Hello, I was working on a test for a device (LadyBug LB5918A Power Sensor) and decided to use python source code that a peer had already written to communicate with it. The code was only tested in a linux environment and being the masoch...
Open Graph Description: Hello, I was working on a test for a device (LadyBug LB5918A Power Sensor) and decided to use python source code that a peer had already written to communicate with it. The code was only tested in ...
X Description: Hello, I was working on a test for a device (LadyBug LB5918A Power Sensor) and decided to use python source code that a peer had already written to communicate with it. The code was only tested in ...
Opengraph URL: https://github.com/python-ivi/python-usbtmc/issues/46
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Windows] usb.core.USBError: [Errno None] libusb0-dll:err [claim_interface] could not claim interface 1, invalid configuration 0","articleBody":"Hello,\r\n\r\nI was working on a test for a device (LadyBug LB5918A Power Sensor) and decided to use python source code that a peer had already written to communicate with it. The code was only tested in a linux environment and being the masochist I am I decided to see if I could at least just identify the device on windows. After using pip to install pyusb \u0026 usbtmc, creating an inf filter with libusb-win32, I ran a brief test script to see if I could just query the device (\"*IDN?\"). Here's the section of the __init__ from the API were the code fails.\r\n```\r\ntry:\r\n\tself.PID = PID\r\n\tself.VID = VID\r\n\t# Initialize USBTMC interface with the LadyBug.\r\n\tself.instr = usbtmc.Instrument(self.VID, self.PID)\r\n\t#self.instr = usbtmc.Instrument(0x1A0D, 0x15D8)\r\n\tself.connected = True\r\n\tself.log.debug('Connecting to device with VID: '+str(self.VID)+' and PID: '+str(self.PID))\r\n\tself._send('SYST:PRES DEF')\r\n\tself.log.debug(str(self._request('*IDN?')))\r\n\t#self.clearErr()\r\nexcept Exception as e: \r\n\tself.exceptionHandler(e)\r\n```\r\n\r\nAnd the `__send()` function\r\n```\r\ndef _send(self,msg):\r\n\t\tself.log.debug(str('Writing: '+str(msg)))\r\n\t\tself.instr.write(msg+'\\n')\r\n\t\terr = str(self.instr.ask('SYST:ERR?\\n'))\r\n\t\tif err != '+0,\"No error\"':\r\n\t\t\tself.log.debug(str('LadyBug Error: '+str(err)))\r\n```\r\n\r\nThe error message I get in return is as follows\r\n```\r\n2018-08-03 15:29:08,970 :: ladyBug(DEBUG) :: __init__\r\n2018-08-03 15:29:08,996 :: ladyBug(DEBUG) :: Connecting to device with VID: 6669 and PID: 5592\r\n2018-08-03 15:29:08,997 :: ladyBug(DEBUG) :: Writing: SYST:PRES DEF\r\nTraceback (most recent call last):\r\n File \"ladyBug-test.py\", line 10, in \u003cmodule\u003e\r\n lb = ladyBug.meter(0x1a0d,0x15d8,True)\r\n File \"C:\\Users\\jonathan.bensing\\LadyBug-API\\ladyBug.py\", line 49, in __init__\r\n self.exceptionHandler(e)\r\n File \"C:\\Users\\jonathan.bensing\\LadyBug-API\\ladyBug.py\", line 249, in exceptionHandler\r\n self.instr.open()\r\n File \"C:\\Python27\\lib\\site-packages\\usbtmc\\usbtmc.py\", line 313, in open\r\n usb.util.claim_interface(self.device, self.iface)\r\n File \"C:\\Python27\\lib\\site-packages\\usb\\util.py\", line 205, in claim_interface\r\n device._ctx.managed_claim_interface(device, interface)\r\n File \"C:\\Python27\\lib\\site-packages\\usb\\core.py\", line 102, in wrapper\r\n return f(self, *args, **kwargs)\r\n File \"C:\\Python27\\lib\\site-packages\\usb\\core.py\", line 167, in managed_claim_interface\r\n self.backend.claim_interface(self.handle, i)\r\n File \"C:\\Python27\\lib\\site-packages\\usb\\backend\\libusb0.py\", line 521, in claim_interface\r\n _check(_lib.usb_claim_interface(dev_handle, intf))\r\n File \"C:\\Python27\\lib\\site-packages\\usb\\backend\\libusb0.py\", line 431, in _check\r\n raise USBError(errmsg, ret)\r\nusb.core.USBError: [Errno None] libusb0-dll:err [claim_interface] could not claim interface 1, invalid configuration 0\r\n```\r\n\r\nAfter a lot of googling, I couldn’t seem to find any conclusive reason why this shouldn’t work on windows seeing that I had followed all the steps correctly for both PyUSB and USBTMC setup. I tried just the PyUSB module to identify the device which I was able to do and I then tried to write to the device commands which I know I was most likely doing wrong. I did notice that for most of the examples of PyUSB, the `set_configuration()` function was used a lot to attach to a device. One post that had a similar error said that `set_configuration(1)` should be used and in a rather fluke way I was able to get the device working, so below is the script that cause the device to function properly.\r\n```\r\nmyVendorId =0x1a0d\r\nmyProductId=0x15d8\r\nimport usb.core\r\ndev = usb.core.find(idVendor=myVendorId, idProduct=myProductId)\r\nprint dev\r\ndev.set_configuration(1)\r\nimport ladyBug\r\nlb = ladyBug.meter(0x1a0d,0x15d8,True)\r\nexit()\r\n```\r\nSo I then decided to go through the USBTMC api to see where the issue may be occurring. I believe that the culprit is here in the `open()` function:\r\n```\r\n def open(self):\r\n if self.connected:\r\n return\r\n # initialize device\r\n # find first USBTMC interface\r\n for cfg in self.device:\r\n for iface in cfg:\r\n if (self.device.idVendor == 0x1334) or \\\r\n (iface.bInterfaceClass == USBTMC_bInterfaceClass and\r\n iface.bInterfaceSubClass == USBTMC_bInterfaceSubClass):\r\n self.cfg = cfg\r\n self.iface = iface\r\n break\r\n else:\r\n continue\r\n break\r\n if self.iface is None:\r\n raise UsbtmcException(\"Not a USBTMC device\", 'init')\r\n try:\r\n self.old_cfg = self.device.get_active_configuration()\r\n except usb.core.USBError:\r\n # ignore exception if configuration is not set\r\n pass\r\n if self.old_cfg is not None and self.old_cfg.bConfigurationValue == self.cfg.bConfigurationValue:\r\n # already set to correct configuration\r\n # release kernel driver on USBTMC interface\r\n self._release_kernel_driver(self.iface.bInterfaceNumber)\r\n else:\r\n # wrong configuration or configuration not set\r\n # release all kernel drivers\r\n if self.old_cfg is not None:\r\n for iface in self.old_cfg:\r\n self._release_kernel_driver(iface.bInterfaceNumber)\r\n # set proper configuration\r\n self.device.set_configuration(self.cfg)\r\n\r\n # claim interface\r\n usb.util.claim_interface(self.device, self.iface)\r\n``` \r\nAfter stepping through the code manually, the issue is where `self.device.set_configuration(self.cfg)` is never being called. I’m not sure the reasoning behind having the configuration not being set, because it seems that (on windows at least) the old_cfg bConfigurationValue will always be the same as the active cfg bConfigurationValue. And within the usbtmc code I simple moved `self.device.set_configuration(self.cfg)` out of the if statement so that the device will always be set.\r\n```\r\n if self.old_cfg is not None and self.old_cfg.bConfigurationValue == self.cfg.bConfigurationValue:\r\n # already set to correct configuration\r\n\r\n # release kernel driver on USBTMC interface\r\n self._release_kernel_driver(self.iface.bInterfaceNumber)\r\n else:\r\n # wrong configuration or configuration not set\r\n\r\n # release all kernel drivers\r\n if self.old_cfg is not None:\r\n for iface in self.old_cfg:\r\n self._release_kernel_driver(iface.bInterfaceNumber)\r\n\r\n # set proper configuration\r\n self.device.set_configuration(self.cfg)\r\n```\r\n\r\nI’m not sure if this is intended but I believe that this is how it should be in the code. I hope this helps!\r\n\r\nJon\r\n","author":{"url":"https://github.com/JonBensi","@type":"Person","name":"JonBensi"},"datePublished":"2018-08-06T14:27:49.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/46/python-usbtmc/issues/46"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:69bcaeea-41a6-55e5-76bd-475ecc2dc64e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B690:6230D:56F31:6E7E9:69781EB3 |
| html-safe-nonce | 5aa00b29a7cff12667392966f0aaf572245165f6cc43cb8d02b2f27ffc36fad1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNjkwOjYyMzBEOjU2RjMxOjZFN0U5OjY5NzgxRUIzIiwidmlzaXRvcl9pZCI6IjE5NDY2MTU5MjM0ODU0NDk5MDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e3d533422558c97b4174da316cf5096c4c28266dfd3388b6daf6c61b02128296 |
| hovercard-subject-tag | issue:347947330 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-ivi/python-usbtmc/46/issue_layout |
| twitter:image | https://opengraph.githubassets.com/eda814a9926b61ea0f7f6650e7a94515d985c0321d5270d8a3cadf8061b5f99c/python-ivi/python-usbtmc/issues/46 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/eda814a9926b61ea0f7f6650e7a94515d985c0321d5270d8a3cadf8061b5f99c/python-ivi/python-usbtmc/issues/46 |
| og:image:alt | Hello, I was working on a test for a device (LadyBug LB5918A Power Sensor) and decided to use python source code that a peer had already written to communicate with it. The code was only tested in ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JonBensi |
| hostname | github.com |
| expected-hostname | github.com |
| None | e71ed39771d00f42ce2fc9320895c8c608c164191e118eae487fc3bd0af6886d |
| turbo-cache-control | no-preview |
| go-import | github.com/python-ivi/python-usbtmc git https://github.com/python-ivi/python-usbtmc.git |
| octolytics-dimension-user_id | 6889594 |
| octolytics-dimension-user_login | python-ivi |
| octolytics-dimension-repository_id | 10878556 |
| octolytics-dimension-repository_nwo | python-ivi/python-usbtmc |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 10878556 |
| octolytics-dimension-repository_network_root_nwo | python-ivi/python-usbtmc |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 4919f24054c1e9bf76d7b543cb2ac018075b18ce |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width