如何在Phidget 8/8/8板上指定采样间隔(10秒)

2024-05-07 18:08:54 发布

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

我用Phidget8/8/8板从传感器获取温度和相对湿度信号。我希望有10秒的采样间隔,但我只能通过setDataInterval()语句将间隔指定为1ms到1000ms。我想要的是10秒的采样率。我曾试图在print函数之后使用time.sleep(10),但它不起作用。例如,它在10秒后打印的第二个值是在默认采样间隔(约250毫秒)之后从开始采样的值。似乎我只是延迟了print函数,而不是更改采样间隔。
下面是从Phidget网站下载的示例代码。在

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)

Tags: toimportassysexitexceptiondetailsch
1条回答
网友
1楼 · 发布于 2024-05-07 18:08:54

在我自己的项目中,我与此斗争了一段时间。。。 我发现刷新电压读数的唯一方法是重新连接电压传感器。 采样率将不准确(因为“等待附件”可能需要几百毫秒),但如果允许测量时间有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”阵列将包括一个时间戳和一个电压读数,它是样本1000ms内所有变化的平均值。之后,发出9000ms暂停。在

相关问题 更多 >