James Hayek James Hayek - 18 days ago 11
Python Question

AttributeError: 'str' object has no attribute 'inWaiting'

From the help of the community I was able to install al new backend for matplotlib and run code from the Arduino serial to the output of the python window.

I was then able to make a pretty graph and it displays, but crashes when I get the following error:

AttributeError: 'str' object has no attribute 'inWaiting'

This was remedied by the help of @elethan

However, I now have the error:

pulse = float(dataArray[0])
ValueError: could not convert string to float:


This error does not happen everytime

As if that wasn't enough, the output on the plotted graph shows a value of 10 for most of the plot, this is not the value output from the Arduino serial.

I am unsure why:
1) The error is intermittend (maybe its grabbing a ',' and not a value)
2) Why I get a steady value of 10 when the graph does plot

The code is as follows:

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):
bpArray.pop(0)


Any help is greatly appreciated, thank you in advance.

Answer

You reassign ser the first time through your while loop from a Serial object to a string:

ser = ser.read(4)

The next time through the loop when you call the inWaiting() method on that object, you get an error, because you are calling it on a string, instead of the original Serial object:

ser.inWaiting() > 4 

Change the variable name for the output of ser.read(4) to a name that is not already taken, and this error should go away:

s = ser.read(4)
...
dataArray = s.split(',')
Comments