PyUSB与Raspberry Pi和自定义温度/湿度传感器通信

时间:2015-11-03 04:16:52

标签: python usb raspberry-pi sensor pyusb

我有: USB HID原始温度/湿度传感器 树莓派B

我想: python从PI上的USB传感器获取数据

借助几个不同的网页和本网站上的一些答案,我找到了设备,我可以显示界面详细信息并发送数组并获得一个数据点,但随后设备忙碌并超时。为什么它只发送一个读数然后需要重新插入以获得另一个数据点?我的第一个想法可能是它是设备,但设备在我的窗户盒上工作得很好。我的python代码错了吗? USB设备在中断IN端点#1上发送16位数据,并在中断OUT端点2上接收数据(因为这是我编写固件的方式)。

Python代码:

    #!/usr/bin/env python
import usb
import sys

dev = usb.core.find(idVendor=0x1dfd, idProduct=0x0006)
if not dev:
    raise IOError('Device not found')
else:
#    dev.set_configuration()
    cfg = dev.get_active_configuration();
#    cfg = [(0,0)]
    print ("Device located")
print ('configuration ' + str(cfg));
for configuration in dev:
    for interface in configuration:
        ifnum = interface.bInterfaceNumber
        if not dev.is_kernel_driver_active(ifnum):
            continue
        try:
            print "detach %s: %s" % (dev, ifnum)
            dev.detach_kernel_driver(ifnum)
            reattach = True
            renum = ifnum
        except usb.core.USBError, e:
            pass
dev.set_configuration()

interface_number = 0
alternate_setting = usb.control.get_interface(dev,interface_number)
intf = usb.util.find_descriptor(cfg, bInterfaceNumber = interface_number,bAlternateSetting = alternate_setting)
epO = usb.util.find_descriptor(
    intf,
    custom_match = \
    lambda f: \
    usb.util.endpoint_direction(f.bEndpointAddress) == \
    usb.util.ENDPOINT_OUT
)
epI = usb.util.find_descriptor(
    intf,
    custom_match = \
    lambda f: \
    usb.util.endpoint_direction(f.bEndpointAddress) == \
    usb.util.ENDPOINT_IN
)
# write the data
print('epI length    ' + str(epI.bLength))
print('ep desc type  ' + str(epI.bDescriptorType))
print('ep address    ' + str(epI.bEndpointAddress))
print('ep xfer type  ' + str(epI.bmAttributes))
#    print('ep synch type ' + str(epI.bmAttributes[1]))
#    print('ep Use type   ' + str(epI.bmAttributes[2]))
print('ep max packet ' + str(epI.wMaxPacketSize))
print('ep interval   ' + str(epI.bInterval))

print('epO length    ' + str(epO.bLength))
print('ep desc type  ' + str(epO.bDescriptorType))
print('ep address    ' + str(epO.bEndpointAddress))
print('ep xfer type  ' + str(epO.bmAttributes))
#    print('ep synch type ' + str(epO.bmAttributes[1]))
#    print('ep Use type   ' + str(epO.bmAttributes[2]))
print('ep max packet ' + str(epO.wMaxPacketSize))
print('ep interval   ' + str(epO.bInterval))

print('dev length      ' + str(dev.bLength))
print('dev num configs ' + str(dev.bNumConfigurations))
print('dev class       ' + str(dev.bDeviceClass))

assert epO is not None
assert epI is not None
msg = '2234567890123456' #first character is the command
print('writing to USB  ->' + msg)
print(str(dev))
#    dev.write(epO, msg, 100)
epO.write(msg)
print('reading from USB...')
ret = epI.read(16)
#    ret = dev.read(2,len(msg),100)    #  device timeout is 50 ms,
sret = ''.join([chr(x) for x in ret])
print(sret)
# 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 loads
if reattach:
    print('Reattaching Kernel Driver')
    dev.attach_kernel_driver(renum)

以下是在PI上执行的响应:

Device located
configuration <usb.core.Configuration object at 0xb6a6e6d0>
detach <usb.core.Device object at 0xb6a6e750>: 0
epI length    7
ep desc type  5
ep address    129
ep xfer type  3
ep max packet 16
ep interval   50
epO length    7
ep desc type  5
ep address    2
ep xfer type  3
ep max packet 16
ep interval   50
dev length      18
dev num configs 1
dev class       0
writing to USB  ->
<usb.core.Device object at 0xb6a6e750>
2234567890123456
reading from USB...

227.45,51.0 ## The first response has this after the last line above

## the second try gives this response instead:

Traceback (most recent call last):
File "./t8.py", line 78, in <module>
ret = epI.read(16)
File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 300, in read
return self.device.read(self.bEndpointAddress, size, self.interface, timeout)
File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 670, in read
self.__get_timeout(timeout)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 798, in intr_read
timeout)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 889, in __read
_check(retval)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 571, in _check
raise USBError(_str_error[ret], ret, _libusb_errno[ret])
usb.core.USBError: [Errno 110] Operation timed out

我搜索并发现我可以使用此命令检查USB消息:

grep usb /var/log/messages

我发现了这条消息:

Nov  3 02:16:06 raspberrypi kernel: [123725.144960] usb 1-1.3: usbfs: process 10698 (python) did not claim interface 0 before use

我添加了重新附加代码,然后就消失了。我搜索并发现其他人有问题,可能是pyUSB。我更新到了b2版本。现在,设备打印命令在给出print(dev)命令时给出完整的HID报告打印输出。非常好,但设备超时的原始问题仍然存在,并且它看起来与声称的界面不一样。

有关如何防止超时的任何见解,以便设备可以提供多组数据?

感谢您的帮助。

0 个答案:

没有答案