我正在使用Raspberry Pi 2板,我已将Nonin Xpod PulseOxy传感器连接到它。我在python脚本中使用PyUSB模块来访问传感器的基本数据,如供应商ID,产品ID等。我无法设置设备的配置并继续收集读数。我尝试安装http://www.ftdichip.com/为Raspberry Pi提供的ftdi驱动程序。但是d2xx模块没有在我的python脚本中导入。 我是新手为设备编写代码。请帮我说明如何继续。
我厌倦了以下事情:
import usb.core
import usb.util
dev = usb.core.find(idVendor=0x0424, idProduct=0x9514)
dev.set_configuration()
我在设置配置中遇到错误。
Traceback (most recent call last):
File "s1.py", line 18, in <module>
main()
File "s1.py", line 13, in main
dev.set_configuration()
File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 799, in set_configuration
self._ctx.managed_set_configuration(self, configuration)
File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 128, in managed_set_configuration
self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb0.py", line 439, in set_configuration
_check(_lib.usb_set_configuration(dev_handle, config_value))
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb0.py", line 380, in _check
raise USBError(errmsg, ret)
usb.core.USBError: [Errno None] could not set config 1: Device or resource busy
传感器的数据表是Nonin Xpod 3012LP
首先感谢您的回答。我尝试了libftd2XX安装,使用命令make -B
for n in BitMode EEPROM/erase EEPROM/read EEPROM/write EEPROM/user/read EEPROM/user/size EEPROM/user/write Events LargeRead MultiThread SetVIDPID Simple Timeouts ; do make -C $n || exit 1; done
make[1]: Entering directory '/home/pi/iiitd/release/examples/BitMode'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/pi/iiitd/release/examples/BitMode'
make[1]: Entering directory '/home/pi/iiitd/release/examples/EEPROM/erase'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/pi/iiitd/release/examples/EEPROM/erase'
make[1]: Entering directory '/home/pi/iiitd/release/examples/EEPROM/read'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/pi/iiitd/release/examples/EEPROM/read'
make[1]: Entering directory '/home/pi/iiitd/release/examples/EEPROM/write'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/pi/iiitd/release/examples/EEPROM/write'
make[1]: Entering directory '/home/pi/iiitd/release/examples/EEPROM/user/read'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/pi/iiitd/release/examples/EEPROM/user/read'
make[1]: Entering directory '/home/pi/iiitd/release/examples/EEPROM/user/size'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/pi/iiitd/release/examples/EEPROM/user/size'
make[1]: Entering directory '/home/pi/iiitd/release/examples/EEPROM/user/write'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/pi/iiitd/release/examples/EEPROM/user/write'
make[1]: Entering directory '/home/pi/iiitd/release/examples/Events'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/pi/iiitd/release/examples/Events'
make[1]: Entering directory '/home/pi/iiitd/release/examples/LargeRead'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/pi/iiitd/release/examples/LargeRead'
make[1]: Entering directory '/home/pi/iiitd/release/examples/MultiThread'
gcc main.c -o multi -Wall -Wextra -L. -lftd2xx -Wl,-rpath /usr/local/lib
/usr/bin/ld: /tmp/ccStfEVz.o: undefined reference to symbol 'pthread_join@@GLIBC_2.4'
//lib/arm-linux-gnueabihf/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: ld returned 1 exit status
Makefile:9: recipe for target 'multi' failed
make[1]: *** [multi] Error 1
make[1]: Leaving directory '/home/pi/iiitd/release/examples/MultiThread'
Makefile:11: recipe for target 'subdirs' failed
make: *** [subdirs] Error 1
答案 0 :(得分:1)
我还没有使用过Nonin Xpod PulseOxy传感器,但它现在还不清楚接口。如果有通讯数据表,请发布。
如果设备显示为虚拟COM端口,Raspberry PI应该准备好库,因此从Python pySerial开始工作。
如果您需要使用FTDI D2XX库,请先安装native library first(直接链接)。详细信息here并查看readme,尤其是此部分:
> If the message "FT_Open failed" appears:
> Perhaps the kernel automatically loaded another driver for the
> FTDI USB device.
>
> `sudo lsmod`
>
> If "ftdi_sio" is listed:
> Unload it (and its helper module, usbserial), as follows.
>
> `sudo rmmod ftdi_sio`
> `sudo rmmod usbserial`
>
> Otherwise, it's possible that libftd2xx does not recognise your
> device's Vendor and Product Identifiers. Call FT_SetVIDPID before
> calling FT_Open/FT_OpenEx/FT_ListDevices.
我发现Raspberry PI默认加载了ftdi_sio和usbserial驱动程序,因此在我使用D2XX库列出FTDI设备和详细信息之前必须先禁用它们。确保您可以先编译样本(库已正确链接)并运行其样本(您可以看到列出的FTDI设备以及详细信息(如VID / PID /等))。只需导航到示例文件夹并使用make -b
即可。
请注意,您可能需要将它们作为sudo运行。
如果上述方法运行良好,剩下的就是安装D2XX库的Python绑定。我已经使用了这些ftd2xx Python bindings。安装应该是直截了当的。如果出现错误,请检查ftd2xx是否正在寻找正确路径中的.so文件(/usr/local/lib/libftd2xx.so
)
安装完成后,您可以先尝试列出您的设备:
import ftd2xx
print ftd2xx.listDevices()
<强>更新强>
看起来只有一个样本有错误,这些错误依赖于pthreads库。我现在尝试安装ftd2xx python库。 令人困惑的部分是普通序列应该起作用:
&#34;绿线=串行输出:9600波特,8个数据位,一个启动位 (起始位= 0),一个停止位(停止位= 1),无奇偶校验。&#34;
您应该尝试使用串行库读取数据:
import serial,time
def stringAsHex(s):
return ":".join("{:02x}".format(ord(c)) for c in s)
sensor = serial.Serial('/dev/ttyACM0',timeout=1)#should default to 9600, 8 data bits, 1 stop bit, but feel free to use the constructor arguments to configure it
while True:
data = sensor.read()
if len(data) > 0:
print "data str:",data,"hex:",stringAsHex(data)
请务必阅读3.8v至3.3v转换的注意事项以及基于所用电阻的不同数据格式(数据表第3页和第4页)。
注意我使用的端口(/dev/ttyACM0
):这通常是新的Arduino端口显示的内容。您必须检查设备的内容(可能会显示为/dev/ttyUSB0
,在将设备连接到USB端口之前和之后执行ls /dev/tty*
。)
答案 1 :(得分:1)
我终于可以使用以下代码从传感器获取数据:
import serial,time,sys
#returns non zero value if the bit is set
def testbit(value,bit):
return (value & 1 << bit)
#converts raw string to hexadecimal
def stringAsHex(s):
return ":".join("{:02x}".format(ord(c)) for c in s)
#extract the information from a packet
def processpacket(p):
msb=p[19][3]
lsb=p[20][3]
hr= ((msb<<7)|(lsb)) & 0x1ff
emsb=p[21][3]
elsb=p[22][3]
ehr= ((emsb<<7)|(elsb)) & 0x1ff
oxy=p[8][3]
ol= oxy & 0x7f
eoxy=p[16][3]
eol= eoxy & 0x7f
#print "Pulse Rate:", hr
#print "Oxygen Level:", ol
#if hr != 511 and ol !=127 :
if hr > 40 and hr < 200 and ol > 85 and ol < 127:
#print "Pulse Rate:", hr
#print "Extended Pulse Rate:",ehr
#print "Oxygen Level:", ol
#print "Extended Oxygen Level:",eol
return hr,ol
else:
#print "Place your finger"
hr=0
ol=0
return hr,ol
def readsensor(n):
sd=[]
val=[]
sensor = serial.Serial('/dev/ttyUSB0',timeout=1,baudrate=9600)
frame=[]
fc=0
packet=[]
while True:
frame=[]
data = sensor.read()
d=stringAsHex(data)
intdata=int(d,16)
#print 'loop 0'
#print intdata
if intdata == 1:
data1 = sensor.read()
d1=stringAsHex(data1)
intdata1=int(d1,16)
#print 'if intdata == 1 sensor.read:',intdata1
tb = testbit(intdata1,0)
#print "test bit:", tb
if tb > 0 :
#print 'packet began'
frame.append(intdata)
frame.append(intdata1)
fc = 2
cnt = 0
wc=0
while ( fc <= 125 ):
wc = wc + 1
#print 'while inside',wc
data = sensor.read()
d=stringAsHex(data)
intdata=int(d,16)
if len(frame)<=5:
frame.append(intdata)
else:
#print 'oversized frame'
break
#print 'fc',fc
fc = fc + 1
if fc%5 == 0:
#print 'if fc%5 == 0'
#print frame
if frame[0] == 1:
cnt = cnt + 1
#print cnt
packet.append(frame)
frame=[]
#print 'frame empty'
else:
#print 'else fc%5',fc
break
if fc == 126:
n = n - 1
if n >= 0:
h,o= processpacket(packet)
sd.append([h,o])
#print sd
packet=[]
#print 'packet empty'
#print "***********Packet received***********"
fc = 1
else:
return sd
else:
continue
else:
continue
print readsensor(20)
我意识到传感器正在给角色。然后我将数据转换为整数并将其放入帧中并将帧组织成一个数据包(数据表中提到了帧和数据包详细信息)。然后我从数据包中提取了传感器读数。 帧和数据包是使用python中的列表实现的。