导入来自PhidgetHelperFunctions的sys导入时间导入来自Phidget22.Devices.TemperatureSensor导入来自Phidget22.PhidgetException导入*来自Phidget22.Phidget import *来自Phidget22.Net import *

msgstr“*”*配置设备的DataInterval和ChangeTrigger . *显示有关附加的phidget Channels 的信息 . 当注册onAttachHandler的Phidget Channels 注册时触发 * @param self触发附加事件的Phidget Channels “”“def onAttachHandler(self ):

ph = self

"""
* Set the DataInterval inside of the attach handler to initialize the device with this value.
* DataInterval defines the minimum time between TemperatureChange events.
* DataInterval can be set to any value from MinDataInterval to MaxDataInterval.
"""
print("\tSetting DataInterval to 5000ms")
try:
    ph.setDataInterval(5000)
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Setting DataInterval: \n\t")
    DisplayError(e)
    return

"""
* Set the TemperatureChangeTrigger inside of the attach handler to initialize the device with this value.
* TemperatureChangeTrigger will affect the frequency of TemperatureChange events, by limiting them to only occur when
* the temperature changes by at least the value set.
"""
print("\tSetting Temperature ChangeTrigger to 0.0")
try:
    ph.setTemperatureChangeTrigger(0.0)
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Set TemperatureChangeTrigger: \n\t")
    DisplayError(e)
    return

try:
    serialNumber = ph.getDeviceSerialNumber()
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Get DeviceSerialNumber: \n\t")
    DisplayError(e)
    return

try:
    channel = ph.getChannel()
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Get Channel: \n\t")
    DisplayError(e)
    return

# Check if this is a VINT device
try:
    hub = ph.getHub()
except PhidgetException as e:
    if(e.code == ErrorCode.EPHIDGET_WRONGDEVICE):
        print("\nAttach Event:\n\t-> Serial Number: " + str(serialNumber) +
                "\n\t-> Channel " + str(channel) + "\n")
    return
try:
    hubPort = ph.getHubPort()
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Get HubPort: \n\t")
    DisplayError(e)
    return

print("\nAttach Event:\n\t-> Serial Number: " + str(serialNumber) +
      "\n\t-> Hub Port: " + str(hubPort) + "\n\t-> Channel " + str(channel) + "\n")
return

“”*显示有关分离的phidget Channels 的信息 . 当onTetachHandler注册的Phidget Channels 已分离时触发 * @param self触发附加事件的Phidget Channels “”“def onDetachHandler(self):

ph = self

try:
    serialNumber = ph.getDeviceSerialNumber()
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Get DeviceSerialNumber: \n\t")
    DisplayError(e)
    return

try:
    channel = ph.getChannel()
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Get Channel: \n\t")
    DisplayError(e)
    return

 # Check if this is a VINT device
try:
    hub = ph.getHub()
except PhidgetException as e:
    if(e.code == ErrorCode.EPHIDGET_WRONGDEVICE):
        print("\nDetach Event:\n\t-> Serial Number: " + str(serialNumber) +
                "\n\t-> Channel " + str(channel) + "\n")
    return

try:
    hubPort = ph.getHubPort()
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Get HubPort: \n\t")
    DisplayError(e)
    return

print("\nDetach Event:\n\t-> Serial Number: " + str(serialNumber) +
      "\n\t-> Hub Port: " + str(hubPort) + "\n\t-> Channel " + str(channel) + "\n")
return

“”将phidget错误信息写入stderr . 当注册了onErrorHandler的Phidget通道遇到库中的错误时被触发 * @param self启动了附加事件的Phidget通道 @param errorCode与错误相关的代码枚举类型ph.ErrorEventCode * @param errorString字符串,包含触发错误的描述“”“def onErrorHandler(self,errorCode,errorString):

sys.stderr.write("[Phidget Error Event] -> " + errorString + " (" + str(errorCode) + ")\n")

“”设置Phidget Attach,Phidget Detach,Phidget Error事件的事件处理程序 * @param ph如果操作成功则将事件处理程序添加到* @return的Phidget通道* @raise PhidgetException如果失败“”“def SetAttachDetachError_Handlers (ph):print(“\ n --------------------------------------”)打印(“\ n设置OnAttachHandler ...”)尝试:ph.setOnAttachHandler(onAttachHandler),除了PhidgetException为e:sys.stderr.write(“运行时错误 - >设置附加处理程序:\ n \ t”)DisplayError(e)raise

print("Setting OnDetachHandler...")
try:
    ph.setOnDetachHandler(onDetachHandler)
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Set Detach Handler: \n\t")
    DisplayError(e)
    raise

print("Setting OnErrorHandler...")
try:
    ph.setOnErrorHandler(onErrorHandler)
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Set Error Handler: \n\t")
    DisplayError(e)
    raise

return

“”输出TemperatureSensor最近报告的温度 . 当注册了onTemperatureChangeHandler的TemperatureSensor通道符合DataInterval和ChangeTrigger条件时触发 * @param self触发TemperatureChange事件的TemperatureSensor通道 @param温度来自TemperatureSensor通道的报告温度“””

print(“输入Filenmae”)filename = input()

open(文件名“.csv”,“a”)as outfile:outfile.write(“C in Temperature”)def onTemperatureChangeHandler(self,temperature):

print( str(temperature))N

with open (filename + ".csv","a") as outfile:
    outfile.write("\n"+str(temperature))

msgstr“*”创建一个TemperatureSensor通道的新实例 . * * @param pvih指向PhidgetTemperatureSensorHandle通道的指针,如果操作成功则创建 @return * @raise PhidgetException如果失败“”“def CreateTemperatureSensor(pvih):

global start
global outfile
print("Creating TemperatureSensor Channel...")
try:
    pvih.create()
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Creating TemperatureSensor: \n\t")
    DisplayError(e)
    raise

return

“”设置TemperatureSensor的TemperatureChange事件的事件处理程序 * @param pvih PhidgetTemperatureSensorHandle通道将事件添加到* @param fptr触发TemperatureChange事件时要调用的回调函数* @return如果操作成功* @如果失败则引发PhidgetException“”“def SetTemperatureHandler(pvih,fptr):

if (not (fptr is None)):
    print("\n--------------------\n"
        "\n  | Temperature change events contain the most recent temperature received from the device.\n"
        "  | The linked TemperatureChange function will run as an event at every DataInterval.\n"
        "  | These events will not occur until a change in temperature >= to the set ChangeTrigger has occurred.\n"
        "  | DataInterval and ChangeTrigger should initially be set in the device AttachHandler function.")

print("")
if (fptr is None):
    print("Clearing OnTemperatureChangeHandler...")
else:
    print("Setting OnTemperatureChangeHandler...")

print("\n--------------------")
try:
    pvih.setOnTemperatureChangeHandler(fptr)
except PhidgetException as e:
    sys.stderr.write("Runtime Error -> Setting TemperatureChangeHandler: \n\t")
    DisplayError(e)
    raise

return

msgstr“*”创建,配置和打开TemperatureSensor通道 . 显示温度事件10秒关闭TemperatureSensor通道 * @return 0如果程序退出成功,1退出时有错误 . “”“def main( ):try:“”“*分配一个新的Phidget Channel对象”“”try:ch = TemperatureSensor(),除了PhidgetException为e:sys.stderr.write(“运行时错误 - >创建TemperatureSensor:\ n \ t”)DisplayError (e)除了RuntimeError之外引发e:sys.stderr.write(“运行时错误 - >创建TemperatureSensor:\ n \ t”e)引发

"""
    * Set matching parameters to specify which channel to open
    """
    #InputSerialNumber(ch)

    #InputVINTProperties(ch)

    #InputChannel(ch)

    #InputupNetwork(ch)

    """
    * Add event handlers before calling open so that no events are missed.
    """
    SetAttachDetachError_Handlers(ch)

    SetTemperatureHandler(ch, onTemperatureChangeHandler)

    """
    * Open the channel with a timeout
    """
    ch.openWaitForAttachment(5000)


    print("Input duration of data collection in seconds...")

    dataduration = input()
    dataduration = int(dataduration)


    #TIME IS IN SECONDS THIS IS THE LINE TO EDIT TO YOUR EXPIRIMENT LENGHT
    time.sleep(int(dataduration))
    #EDIT THE LINE ABOVE TO YOUR EXPIRIMENT LENGHT
    """
     * Perform clean up and exit
     """

    SetTemperatureHandler(ch, None)
    print("\nDone Sampling...")
    print("Cleaning up...")
    ch.close()
    print("\nExiting...")
    print("Press ENTER to end program.")
    readin = sys.stdin.readline(1)
    return 0

except PhidgetException as e:
    sys.stderr.write("\nExiting with error(s)...")
    DisplayError(e)
    print("Press ENTER to end program.")
    readin = sys.stdin.readline(1)
    return 1

主要()