如何将外部设备输出绘制到图形中?

问题描述 投票:0回答:1

我有一个Phidg​​ets差压传感器设备连接到Python,并使用模板代码输出压力。我已经使它工作了,并且正在将压力值输出到控制台中。但是,我正在寻找输出值的图形,并绘制随时间变化的线性图。有谁知道如何做到这一点?我已经附上了我正在使用的代码。

from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time

def onSensorChange(self, sensorValue, sensorUnit):
    print("SensorValue: " + str(sensorValue))

global buffer
buffer.append(sensorValue)
if len(buffer)>4:
    buffer.sort()
    buffer = []

    print("SensorUnit: " + str(sensorUnit.symbol))
    print("----------")


def main():
    voltageRatioInput4 = VoltageRatioInput()

    voltageRatioInput4.setChannel(4)

    voltageRatioInput4.setOnSensorChangeHandler(onSensorChange)

    voltageRatioInput4.openWaitForAttachment(5000)

    voltageRatioInput4.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_1139)

    try:
        input("Press Enter to Stop\n")
    except (Exception, KeyboardInterrupt):
        pass

    voltageRatioInput4.close()

main()

正在输出sensorValue!

SensorValue:0.223

这就是我想要的。但是,它并没有将其保存为某种形式的变量,因此我可以根据时间进行绘制。任何试图获取值的尝试都将导致

NameError:未定义名称'sensorValue']

有人知道如何将sensorValue中的值获取到数组变量中吗?

在进行MATLAB作业时,总是潜伏在stackoverflow周围。回到我的方式回到这里,再次需要Python作业的帮助,呵呵。任何帮助表示赞赏!

python matplotlib phidgets
1个回答
0
投票

您应该使用一些变量来保留历史记录。由于在函数内部创建的变量仅在函数内部存在-您可以从外部使用全局变量。

...

history = []

def onSensorChange(self, sensorValue, sensorUnit):
    global history
    history.append( sensorValue )
    print("SensorValue: " + str(sensorValue))
    print("History of values: ", history)

...

查看调用onSensorChange函数时历史记录如何变化。

© www.soinside.com 2019 - 2024. All rights reserved.