最近我还不太了解python和面向对象编程。我需要用Phidget接口kit8/8/8为Phidgets温度传感器设备编写一个python程序。我查看了Phidgets的示例代码,但它对我帮助不大。在
这是Phidgets python代码示例的一部分。这些事件用作程序后面Phidgets类TemperatureSensor()的参数。 最让人困惑的是“e”。什么是“e”?是“自我”还是其他什么?什么是e.device(和 e、 换句话说)mean/do?在
另外,如果有人对如何为Phidgets温度传感器编写python代码有任何建议,我将不胜感激。在
#Event Handler Callback Functions
def TemperatureSensorAttached(e):
attached = e.device
print("TemperatureSensor %i Attached!" % (attached.getSerialNum()))
def TemperatureSensorDetached(e):
detached = e.device
print("TemperatureSensor %i Detached!" % (detached.getSerialNum()))
def TemperatureSensorError(e):
try:
source = e.device
if source.isAttached():
print("TemperatureSensor %i: Phidget Error %i: %s" % (source.getSerialNum(), e.eCode, e.description))
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
def TemperatureSensorTemperatureChanged(e):
try:
ambient = temperatureSensor.getAmbientTemperature()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
ambient = 0.00
source = e.device
print("TemperatureSensor %i: Ambient Temp: %f -- Thermocouple %i temperature: %f -- Potential: %f" % (source.getSerialNum(), ambient, e.index, e.temperature, e.potential))
此代码用于湿度+温度传感器,因此没有在更简单的温度传感器上进行测试。此外,它是用新的phidget22制造的。我希望它能帮助:
如上所述,代码中的e是事件的句柄。我使用了一种稍微不同的方法来作为温度传感器对象的实例(仅使用attach事件)。两种方法都有效。在
e
变量作为参数传递给事件处理程序。它是一个事件对象,包含有关事件的信息。在事件处理常用于异步编程。它使得编写程序能够对外部变化做出反应,例如用户点击按钮或传感器读数变化。大多数时候,你不需要处理所有可能的事件。相反,您可以编写一个事件处理程序并将其注册到事件源中。在您的例子中,事件源是一个设备,事件处理程序是使用
device.setOnAttachHandler(AttachHandler)
之类的调用注册的。在例如,
TemperatureSensorTemperatureChanged
在传感器读数更新(或定期更新)时触发。在e.something
只是与事件相关的数据,传递给事件处理程序。似乎e.temperature
可能有用。在看起来你的思路是对的,你正在阅读示例代码和文档,因此你会发现:)
相关问题 更多 >
编程相关推荐