我正在使用 Phidget 8/8/8 板从传感器获取温度和 RH 信号。我想要一个 10 秒的采样间隔,但我只能通过语句将间隔指定为 1ms 到 1000ms setDataInterval()
。我想要的是 10 秒的采样率。我曾尝试time.sleep(10)
在该print
功能后使用,但它不起作用。例如,它在 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)