如何从Non-Xpod脉冲氧传感器获取数据到Raspberry Pi

2024-09-28 22:37:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我用的是树莓皮2板,我已经连接了非Xpod脉冲氧传感器。我在python脚本中使用PyUSB模块来访问传感器的基本数据,如供应商id、产品id等。我无法设置设备的配置并继续收集读数。我尝试安装http://www.ftdichip.com/为Raspberry Pi提供的ftdi驱动程序。但是我的python脚本中没有导入d2xx模块。 我对编写设备代码很陌生。请帮助我如何继续。在

我累了以下几点:

import usb.core
import usb.util
dev = usb.core.find(idVendor=0x0424, idProduct=0x9514)
dev.set_configuration()

设置配置出错。在

^{pr2}$

传感器的数据表是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

Tags: tohomeforreleasemakepiexamplesdirectory
2条回答

我终于能够使用以下代码从传感器获取数据:

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中的列表实现的。在

我还没有用过非Xpod脉冲氧传感器,但现在还不清楚它的接口。如果有通讯数据表,请张贴。在

如果设备显示为虚拟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/等等)。只需导航到samples文件夹并使用make -b。 请注意,您可能需要将它们作为sudo运行。在

如果上面的工作正常,剩下的就是为D2XX库安装Python绑定。我用过这些ftd2xx Python bindings。设置应该是直截了当的。如果出现错误,请检查ftd2xx是否在正确的路径(/usr/local/lib/libftd2xx.so)中查找.so文件

安装后,您可以尝试先列出您的设备:

^{pr2}$

更新

看起来只有一个示例出现了与pthreads库相关的错误。我现在尝试安装ftd2xx python库。 令人困惑的是,普通序列号应该可以工作:

"Green Wire = Serial Output: 9600 Baud, 8 data bits, One Start bit (Start bit =0), One Stop bit (Stop bit = 1), No Parity."

您应该尝试使用串行库简单地读取数据:

^{3}$

请务必阅读有关3.8v到3.3v转换的说明以及基于所用电阻器的不同数据格式(数据表第3页和第4页)。在

注意我使用的端口(/dev/ttyACM0):这通常是新的Arduino端口显示的。你必须检查你的设备是什么(可能显示为/dev/ttyUSB0,在将你的设备连接到usb端口之前和之后做ls /dev/tty*)。在

相关问题 更多 >