Raspberry Pyusb使资源繁忙 [英] Raspberry Pyusb gets Resource busy

查看:133
本文介绍了Raspberry Pyusb使资源繁忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过USB将Raspberry PI连接到Pic4550.(使用Windows C#程序可以使用PIC功能!).因此,我已经安装了rpi 2,pyusb,并尝试在[ https://github.com/walac/pyusb/blob/master/docs/tutorial.rst] [1]

I'm trying to connect my Raspberry PI to Pic4550 via USB. (Pic function is ok with windows c# program!). So I have installed rpi 2, pyusb, and tried to communicate with the help of [https://github.com/walac/pyusb/blob/master/docs/tutorial.rst][1]

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

I get connected to the USB device, lsusb shows:

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

Bus 001 Device 006: ID 04d8:0080 Microchip Technology, Inc.

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

The python prog finds the device! Gets the right config but cannot write message:

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

usb.core.USBError: [Errno 16] Resource busy

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

I tried to run as sudo, I have added rule:

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

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

无论如何我都忙于同一资源

Anyway I get the same resource busy

是否有任何胶水帮助链接?

Any glue-help-link?

推荐答案

对于其他像我这样的菜鸟,我发布了我的解决方案.总结:请仔细阅读文档.

For other rookies like me I post my solution. To sum up: read docs carefully.

Python:3.2

Python: 3.2

PyUSB 1.0

PyUSB 1.0

端点是HID设备.

这是我的代码正常工作.

Here is my code working properly.

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)

这篇关于Raspberry Pyusb使资源繁忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆