<legend id='Yxas8'><style id='Yxas8'><dir id='Yxas8'><q id='Yxas8'></q></dir></style></legend>

    <small id='Yxas8'></small><noframes id='Yxas8'>

  1. <i id='Yxas8'><tr id='Yxas8'><dt id='Yxas8'><q id='Yxas8'><span id='Yxas8'><b id='Yxas8'><form id='Yxas8'><ins id='Yxas8'></ins><ul id='Yxas8'></ul><sub id='Yxas8'></sub></form><legend id='Yxas8'></legend><bdo id='Yxas8'><pre id='Yxas8'><center id='Yxas8'></center></pre></bdo></b><th id='Yxas8'></th></span></q></dt></tr></i><div id='Yxas8'><tfoot id='Yxas8'></tfoot><dl id='Yxas8'><fieldset id='Yxas8'></fieldset></dl></div>
      <bdo id='Yxas8'></bdo><ul id='Yxas8'></ul>
    1. <tfoot id='Yxas8'></tfoot>

    2. 用Python实现与USB设备的通信

      时间:2024-08-21
      <legend id='wQf69'><style id='wQf69'><dir id='wQf69'><q id='wQf69'></q></dir></style></legend>

        <small id='wQf69'></small><noframes id='wQf69'>

          <tbody id='wQf69'></tbody>

          • <bdo id='wQf69'></bdo><ul id='wQf69'></ul>

              <i id='wQf69'><tr id='wQf69'><dt id='wQf69'><q id='wQf69'><span id='wQf69'><b id='wQf69'><form id='wQf69'><ins id='wQf69'></ins><ul id='wQf69'></ul><sub id='wQf69'></sub></form><legend id='wQf69'></legend><bdo id='wQf69'><pre id='wQf69'><center id='wQf69'></center></pre></bdo></b><th id='wQf69'></th></span></q></dt></tr></i><div id='wQf69'><tfoot id='wQf69'></tfoot><dl id='wQf69'><fieldset id='wQf69'></fieldset></dl></div>
              1. <tfoot id='wQf69'></tfoot>
                本文介绍了用Python实现与USB设备的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我要与USB设备通信并向其发送数据。我能够找到该设备,但是在将该设备与内核驱动程序连接时,它提供了USB Error: Resource Busy。以下是我的代码:

                import usb
                
                dev = usb.core.find(idVendor=0x0403, idProduct=0x6001)
                dev.set_configuration()
                
                cfg = dev.get_active_configuration()
                
                dev.attach_kernel_driver(interface)
                
                interface_number = cfg[(0, 0)].bInterfaceNumber
                alternate_settting = usb.control.get_interface(interface_number)
                intf = usb.util.find_descriptor(
                    cfg, bInterfaceNumber=interface_number,
                    bAlternateSetting=alternate_setting)
                
                ep = usb.util.find_descriptor(
                    intf, custom_match=lambda e:
                    usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT)
                
                dev.detach_kernel_driver(interface)
                
                ep.write("
                " + linea1[:19] + "
                
                " + " " * (20 - len(linea2)) + linea2)
                

                推荐答案

                假定您使用的是linux,并且libusb-1.0PyUSB的后端库。

                根据libusb documentation:

                // Detach a kernel driver from an interface.
                // If successful, you will then be able to claim the interface and perform I/O.
                int libusb_detach_kernel_driver (libusb_device_handle *dev, 
                                                 int interface_number)  
                
                // Re-attach an interface's kernel driver, which was previously 
                // detached using libusb_detach_kernel_driver().
                int libusb_attach_kernel_driver(libusb_device_handle *dev,
                                                int interface_number)
                

                因此,基本上,您需要首先调用detach_kernel_driver从设备接口分离已经附加的内核驱动程序(如果有),这样您就可以在代码中与其通信(它可以是您的代码,也可以是与设备接口对话的某个内核驱动程序)。完成后,您可能需要调用attach_kernel_driver重新附加内核驱动程序。

                我相信,如果您可以确保没有为给定设备加载内核驱动程序(或者在运行代码之前手动卸载它),那么就不需要调用这些C函数/Python方法中的任何一个。

                编辑:

                我刚刚让这段代码(基于您的示例)正常工作。注意:为简单起见,我已将detach_kernel_driverattach_kernel_driver的接口编号硬编码为0-我想您应该使其更智能。

                import usb
                
                dev = usb.core.find(idVendor=0x0403, idProduct=0x6001)
                
                reattach = False
                if dev.is_kernel_driver_active(0):
                    reattach = True
                    dev.detach_kernel_driver(0)
                
                dev.set_configuration() 
                cfg = dev.get_active_configuration() 
                
                interface_number = cfg[(0,0)].bInterfaceNumber 
                alternate_settting = usb.control.get_interface(dev, interface_number) 
                intf = usb.util.find_descriptor(cfg, bInterfaceNumber = interface_number, 
                                            bAlternateSetting = alternate_settting) 
                
                ep = usb.util.find_descriptor(intf,custom_match = 
                      lambda e: 
                    usb.util.endpoint_direction(e.bEndpointAddress) == 
                    usb.util.ENDPOINT_OUT) 
                ep.write("test
                
                ")
                
                # This is needed to release interface, otherwise attach_kernel_driver fails 
                # due to "Resource busy"
                usb.util.dispose_resources(dev)
                
                # It may raise USBError if there's e.g. no kernel driver loaded at all
                if reattach:
                    dev.attach_kernel_driver(0) 
                

                这篇关于用Python实现与USB设备的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:PyVISA未列出Linux上的USB仪器 下一篇:如何在硒中移动鼠标?

                相关文章

                  <legend id='2xqDc'><style id='2xqDc'><dir id='2xqDc'><q id='2xqDc'></q></dir></style></legend>
                  1. <tfoot id='2xqDc'></tfoot>

                    <small id='2xqDc'></small><noframes id='2xqDc'>

                    <i id='2xqDc'><tr id='2xqDc'><dt id='2xqDc'><q id='2xqDc'><span id='2xqDc'><b id='2xqDc'><form id='2xqDc'><ins id='2xqDc'></ins><ul id='2xqDc'></ul><sub id='2xqDc'></sub></form><legend id='2xqDc'></legend><bdo id='2xqDc'><pre id='2xqDc'><center id='2xqDc'></center></pre></bdo></b><th id='2xqDc'></th></span></q></dt></tr></i><div id='2xqDc'><tfoot id='2xqDc'></tfoot><dl id='2xqDc'><fieldset id='2xqDc'></fieldset></dl></div>
                      <bdo id='2xqDc'></bdo><ul id='2xqDc'></ul>