【问题标题】:Raspberry Pyusb gets Resource busyRaspberry Pyusb 资源繁忙
【发布时间】:2015-06-03 09:59:25
【问题描述】:

我正在尝试通过 USB 将我的 Raspberry PI 连接到 Pic4550。 (图片功能可以用windows c#程序!)。 所以我已经安装了rpi 2,pyusb,并尝试在[https://github.com/walac/pyusb/blob/master/docs/tutorial.rst][1]

的帮助下进行通信

我连接到 USB 设备,lsusb 显示:

总线 001 设备 006:ID 04d8:0080 Microchip Technology, Inc.

python prog 找到了设备!获取正确的配置但无法写入消息:

usb.core.USBError: [Errno 16] 资源繁忙

我尝试以 sudo 运行,我添加了规则:

SUBSYSTEM=="usb", ATTR{idVendor}=="04d8", ATTR{idProduct}=="0080", MODE="666"

不管怎样,我得到相同的资源忙

任何胶水帮助链接?

【问题讨论】:

标签: python usb hid pyusb raspberry-pi2


【解决方案1】:

我使用的是 Raspberry 3B+、Python 2.7,并且收到了完全相同的错误消息:

usb.core.USBError: [Errno 16] 资源繁忙

对我来说,dev.reset() 解决了我的问题。谢谢@rishta。

【讨论】:

    【解决方案2】:

    对于像我这样的其他新手,我发布了我的解决方案。 总结一下:仔细阅读文档。

    Python:3.2

    PyUSB 1.0

    端点是 HID 设备。

    这是我的代码正常工作。

    import usb.core
    import usb.util
    import sys
    from time import gmtime, strftime
    import time
    
    print ("Meteo kezdés",strftime("%Y-%m-%d %H:%M:%S", gmtime()))
    
    
    # find our device
    dev = usb.core.find(idVendor=0x04d8, idProduct=0x0080)
    
    # was it found?
    if dev is None:
        raise ValueError('Device not found')
    else:
        print ("meteo megvan!")
    
    reattach = False
    if dev.is_kernel_driver_active(0):
        reattach = True
        dev.detach_kernel_driver(0)
    
    endpoint_in = dev[0][(0,0)][0]
    endpoint_out = dev[0][(0,0)][1]
    #print ("endpoint_out",endpoint_out)
    #print ("endpoint_in",endpoint_in)
    
    # write the data
    msg = b'\x81'
    
    while 1:
        try:
            endpoint_out.write(msg)
    
            # reading
            #print ("Waiting to read...")
            #print (endpoint.bEndpointAddress)
            data = dev.read(endpoint_in.bEndpointAddress, 64, 1000)
            DHT11_H = data[0]   # a tobbi helyiertek kimaradt!!
            DHT11_R = data[4]
            BMP180_H = data[8]
            BMP180_P = (data[12]+data[13]*256+data[14]*65536)/100
    
            print (strftime("%Y-%m-%d %H:%M:%S", gmtime()),
            "DHT t=" , str(DHT11_H) , "C| ",
            "DHT r=", DHT11_R, "%| " ,
            "BMP t=", BMP180_H, "C| " ,
            "BMP p=", BMP180_P, "HPa"
            )
            #print (data)
            time.sleep(10)
        except usb.core.USBError:
            print ("USB error")
        except:
            print ("write failed")
    # end while
    
    # 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)
    

    【讨论】:

    • dev.reset() 不是更简单吗?我发现它在与此类似的情况下非常有用。
    【解决方案3】:

    我从这里(不远......)找到了解决方案:

    Communication with the USB device in Python

    应该像这样分离驱动程序:

    if dev.is_kernel_driver_active(0):
        reattach = True
        dev.detach_kernel_driver(0)
    

    【讨论】:

    • reattach 是什么?
    猜你喜欢
    • 2016-06-06
    • 2017-09-02
    • 1970-01-01
    • 2016-08-29
    • 2015-09-24
    • 2013-05-12
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    相关资源
    最近更新 更多