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

Python 如何从Non-Xpod脉冲氧传感器到Raspberry Pi获取数据,python,raspberry-pi2,ftdi,Python,Raspberry Pi2,Ftdi,我使用的是Raspberry Pi 2板,我已将Non-Xpod脉冲氧传感器连接到该板上。我在python脚本中使用PyUSB模块访问传感器的基本数据,如供应商id、产品id等。我无法设置设备的配置并继续收集读数。我尝试安装Raspberry Pi提供的ftdi驱动程序。但是在我的python脚本中没有导入d2xx模块。 我不熟悉为设备编写代码。请帮助我如何进行 我对以下几点感到厌倦: import usb.core import usb.util dev = usb.core.find(idV

我使用的是Raspberry Pi 2板,我已将Non-Xpod脉冲氧传感器连接到该板上。我在python脚本中使用PyUSB模块访问传感器的基本数据,如供应商id、产品id等。我无法设置设备的配置并继续收集读数。我尝试安装Raspberry Pi提供的ftdi驱动程序。但是在我的python脚本中没有导入d2xx模块。 我不熟悉为设备编写代码。请帮助我如何进行

我对以下几点感到厌倦:

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 

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

如果设备显示为虚拟com端口,则Raspberry PI应该准备好库,以便从Python开始工作

如果需要使用FTDI D2XX库,请首先安装(直接链接)。提供详细信息并查看,尤其是本部分:

> 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/etc))。只需导航到samples文件夹并使用
make-b
。 请注意,您可能需要将它们作为sudo运行

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

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

import ftd2xx
print ftd2xx.listDevices()
更新

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

“绿线=串行输出:9600波特,8个数据位,一个起始位 (起始位=0),一个停止位(停止位=1),无奇偶校验。“

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

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端口显示的内容。在将设备连接到usb端口之前和之后,您必须检查设备的功能(可能显示为
/dev/ttyUSB0
,do
ls/dev/tty*

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

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)
导入序列、时间、系统
#如果位已设置,则返回非零值
def测试位(值,位):

return(value&1请发布您编写的代码,通常不可能在没有看到您的代码的情况下回答问题。另外,请包含完整错误代码的准确引用,因为它通常包含有用的信息。我已尝试过libftd2x安装,并收到了问题编辑中提到的错误。请帮助。谢谢:)@Sucharitharedy看一下更新的答案。您正在通过USB连接设备?是的,我正在尝试通过USBI连接设备。我已尝试使用串行库代码读取数据。但由于我无法配置传感器,因此无法读取数据。请帮助配置@GeorgeI我不确定配置传感器是什么意思。如果没有设备本身,我将无法测试设备的行为。我不是100%了解你如何连接传感器。它是否显示串行端口?您是否如数据表所述,使用任何电阻器从3.8v变为3.3v逻辑?据此,“3012型的默认数据格式为数据格式2”,即“数据格式2 5字节/75次/秒8.2K+/-5%”。
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)