2

我正在使用 Phidg​​et 8/8/8 板从传感器获取温度和 RH 信号。我想要一个 10 秒的采样间隔,但我只能通过语句将间隔指定为 1ms 到 1000ms setDataInterval()。我想要的是 10 秒的采样率。我曾尝试time.sleep(10)在该print功能后使用,但它不起作用。例如,它在 10 秒后打印的第二个值是它从一开始就在默认采样间隔(~250 毫秒)后采样的值。看来我只是延迟了print函数而不是改变采样间隔。
以下是从 Phidg​​et 网站下载的示例代码。

import sys
import time 
from Phidget22.Devices.VoltageInput import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *

try:
    ch = VoltageInput()
    ch.setDeviceSerialNumber(437701)
    ch.setChannel(1)
    #set channel 1 as the input voltage port
    #ch.setHubPort(1)
except RuntimeError as e:
    print("Runtime Exception %s" % e.details)
    print("Press Enter to Exit...\n")
    readin = sys.stdin.read(1)
    exit(1)

def VoltageInputAttached(e):
    try:
        attached = e
        print("\nAttach Event Detected (Information Below)")
        print("===========================================")
        print("Library Version: %s" % attached.getLibraryVersion())
        print("Serial Number: %d" % attached.getDeviceSerialNumber())
        print("Channel: %d" % attached.getChannel())
        print("Channel Class: %s" % attached.getChannelClass())
        print("Channel Name: %s" % attached.getChannelName())
        print("Device ID: %d" % attached.getDeviceID())
        print("Device Version: %d" % attached.getDeviceVersion())
        print("Device Name: %s" % attached.getDeviceName())
        print("Device Class: %d" % attached.getDeviceClass())
        print("\n")

    except PhidgetException as e:
        print("Phidget Exception %i: %s" % (e.code, e.details))
        print("Press Enter to Exit...\n")
        readin = sys.stdin.read(1)
        exit(1)   

def VoltageInputDetached(e):
    detached = e
    try:
        print("\nDetach event on Port %d Channel %d" % (detached.getHubPort(), detached.getChannel()))
    except PhidgetException as e:
        print("Phidget Exception %i: %s" % (e.code, e.details))
        print("Press Enter to Exit...\n")
        readin = sys.stdin.read(1)
        exit(1)   

def ErrorEvent(e, eCode, description):
    print("Error %i : %s" % (eCode, description))

def VoltageChangeHandler(e, voltage):
    print("Voltage: %f" % voltage)
    time.sleep(10)

def SensorChangeHandler(e, sensorValue, sensorUnit):
    print("Sensor Value: %f" % sensorValue)

try:
    ch.setOnAttachHandler(VoltageInputAttached)
    ch.setOnDetachHandler(VoltageInputDetached)
    ch.setOnErrorHandler(ErrorEvent)

    ch.setOnVoltageChangeHandler(VoltageChangeHandler)
    ch.setOnSensorChangeHandler(SensorChangeHandler)

    print("Waiting for the Phidget VoltageInput Object to be attached...")
    ch.openWaitForAttachment(5000)
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Press Enter to Exit...\n")
    readin = sys.stdin.read(1)
    exit(1)

print("Gathering data for 20 seconds...")
time.sleep(20)

try:
    ch.close()
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Press Enter to Exit...\n")
    readin = sys.stdin.read(1)
    exit(1) 
print("Closed VoltageInput device")
exit(0)
4

1 回答 1

0

我在自己的项目中与这个问题斗争了一段时间……我发现真正刷新电压读数的唯一方法是重新连接电压传感器。采样率将不准确(因为“等待附件”可能需要数百毫秒),但如果您可以允许测量时间有 5% 的偏差,这是可行的。

另一个问题是对事件的依赖——您不是在主动读取电压,而是等待电压变化来触发事件。

    import sys
    import time
    from Phidget22.Devices.VoltageInput import *
    from Phidget22.PhidgetException import *
    from Phidget22.Phidget import *
    from Phidget22.Net import *

    volt_array = []
    v_read = 0
    sample_array = []
    volt_time_output = []

    def VoltageInputAttached(e):
        try:
            attached = e
            print("\nAttach Event Detected (Information Below)")
            print("===========================================")
            print("Library Version: %s" % attached.getLibraryVersion())
            print("Serial Number: %d" % attached.getDeviceSerialNumber())
            print("Channel: %d" % attached.getChannel())
            print("Channel Class: %s" % attached.getChannelClass())
            print("Channel Name: %s" % attached.getChannelName())
            print("Device ID: %d" % attached.getDeviceID())
            print("Device Version: %d" % attached.getDeviceVersion())
            print("Device Name: %s" % attached.getDeviceName())
            print("Device Class: %d" % attached.getDeviceClass())
            print("\n")

        except PhidgetException as e:
            print("Phidget Exception %i: %s" % (e.code, e.details))
            print("Press Enter to Exit...\n")
            readin = sys.stdin.read(1)
            exit(1)

    def VoltageInputDetached(e):
        detached = e
        try:
            print("\nDetach event on Port %d Channel %d" % (detached.getHubPort(),         detached.getChannel()))
        except PhidgetException as e:
            print("Phidget Exception %i: %s" % (e.code, e.details))
            print("Press Enter to Exit...\n")
            readin = sys.stdin.read(1)
            exit(1)

    def ErrorEvent(e, eCode, description):
        print("Error %i : %s" % (eCode, description))

    def VoltageChangeHandler(e, voltage):
        volt_array.append(voltage)

    def SensorChangeHandler(e, sensorValue, sensorUnit):
        print("Sensor Value: %f" % sensorValue)

    while True: #Set a more reasonable exit condition here
        try:
            ch = VoltageInput()
            ch.setDeviceSerialNumber(437701)
            ch.setChannel(1)
            #set channel 1 as the input voltage port
            #ch.setHubPort(1)
        except RuntimeError as e:
            print("Runtime Exception %s" % e.details)
            print("Press Enter to Exit...\n")
            readin = sys.stdin.read(1)
            exit(1)
        try:
            ch.setOnAttachHandler(VoltageInputAttached)
            ch.setOnDetachHandler(VoltageInputDetached)
            ch.setOnErrorHandler(ErrorEvent)

            ch.setOnVoltageChangeHandler(VoltageChangeHandler)
            ch.setOnSensorChangeHandler(SensorChangeHandler)

            print("Waiting for the Phidget VoltageInput Object to be attached...")
            ch.openWaitForAttachment(5000)
        except PhidgetException as e:
            print("Phidget Exception %i: %s" % (e.code, e.details))
            print("Press Enter to Exit...\n")
            readin = sys.stdin.read(1)
            exit(1)

        print("Gathering data...")
        time.sleep(1)
        ts = time.gmtime()
        sample_array.append(float(sum(volt_array)) / max(len(volt_array), 1))
        sample_array.append(time.strftime("%Y-%m-%d %H:%M:%S", ts))
        volt_time_output.append(sample_array)
        volt_array=[]
        sample_array=[]
        time.sleep(9)


    try:
        ch.close()
    except PhidgetException as e:
        print("Phidget Exception %i: %s" % (e.code, e.details))
        print("Press Enter to Exit...\n")
        readin = sys.stdin.read(1)
        exit(1)
    print("Closed VoltageInput device")
    print (volt_time_output)
    exit(0)

'volt_time_output' 数组将包括一个时间戳和一个电压读数,这是样本 1000 毫秒内所有变化的平均值。之后,发出 9000 毫秒的暂停。

于 2019-01-14T07:43:55.957 回答