在社区的帮助下,我能够为matplotlib安装新的后端,并将代码从Arduino串行运行到python窗口的输出。AttributeError:'str'对象没有属性'inWaiting'
当时我能够做出一个漂亮的曲线图,它显示,但崩溃时,我得到以下错误:
AttributeError的:“海峡”对象有没有属性“inWaiting”
这被纠正通过@elethan的帮助
不过,我现在有错误:
pulse = float(dataArray[0])
ValueError: could not convert string to float:
这个错误每次都不会发生
就好像这还不够,绘图图上的输出显示大部分图的值为10,这不是Arduino序列的输出值。
我不确定为什么: 1)错误是有间歇(也许它抓住一个“”而不是值) 2)为什么我得到的10稳态值时,该图确实情节
的代码如下:
import time
import serial
import matplotlib.pyplot as plt
import numpy
from drawnow import *
import os,sys
pulseArray = []
plt.ion()
clippingCounter = 0
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='/dev/ttyACM0',
baudrate=115200,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS
)
ser.isOpen()
input=1
def plotPulseData():
plt.ylim(0,120)
plt.title('Plots from data of the Pulse Sensor')
plt.grid(True)
plt.ylabel('Pulse')
#plt.subplot(2, 2, 1)
plt.plot(pulseArray, 'ro-', label='BPM', linewidth=2)
plt.legend(loc='upper left')
while True:
#First things first, lets wait for data prior to reading
time.sleep(1)
if (ser.inWaiting()>4):
s = ser.read(4)
#print ser
dataArray = s.split(',')
pulse = float(dataArray[0])
pulseArray.append(pulse)
plt.figure(1)
drawnow(plotPulseData)
plt.pause(.000001)
clippingCounter = clippingCounter + 1
if(clippingCounter>50):
pulseArray.pop(0)
任何帮助,非常感谢,提前谢谢。
我不确定您正在处理的数据的性质。现在发生的情况是,有时'dataArray'将成为一个列表,其中第一个元素('dataArray [0]'是一个不能转换为浮点数的字符串(即,不是像'1'或' '1.0',有些方法可以处理,但我不确定你想要做什么,如果你在分配pulse之前加上'print s',你看到了什么样的字符串? – elethan
我正在使用在心率数据的BPM方面,arduino正在输出正确的BPM,正如它的串行监视器一样,当添加'print s'行时,我会看到一堆值,就像我期望的那样,即146, 146, 152, 152, 152, 152, 156 ... 我将不得不运行这个更多的时间去查看是什么导致了错误,这次我得到了一个不同的错误,它是'bpArray.pop(0) NameError:名称'bpArray'未定义' –