Python 3 arduino:使用drawow的实时图形

时间:2017-07-24 19:13:00

标签: python-3.x arduino-uno

来自:http://www.toptechboy.com/tutorial/python-with-arduino-lesson-11-plotting-and-graphing-live-data-from-arduino-with-matplotlib/

我使用提供的示例来创建Arduino的实时数据。 arduino的数据输出如下:[" b' 23.00"," 1.00 \ r \ n'&#34]。 运行代码时,我最终得到错误:

File "C:/Users/Intel/Documents/Python Project/ArduinoDatalogger/test2.py", line 36, in <module>
    drawnow(makeFig)                       #Call drawnow to update our live graph

TypeError: 'module' object is not callable

这是我正在使用的代码(最初只想绘制温度):

import serial # import Serial Library
import numpy  # Import numpy
import matplotlib.pyplot as plt #import matplotlib library
import drawnow #import *

tempC= []
Cycle=[]
arduinoData = serial.Serial('com4', 9600) #Creating our serial object named arduinoData
plt.ion() #Tell matplotlib you want interactive mode to plot live data
cnt=0

def makeFig(): #Create a function that makes our desired plot
    plt.ylim(0,50)                                 #Set y min and max values
    plt.title('My Live Streaming Sensor Data')      #Plot the title
    plt.grid(True)                                  #Turn the grid on
    plt.ylabel('Temp F')                            #Set ylabels
    plt.plot(tempC, 'ro-', label='Degrees F')       #plot the temperature
    plt.legend(loc='upper left')                    #plot the legend
#    plt2=plt.twinx()                                #Create a second y axis
#    plt.ylim(0,2)                           #Set limits of second y axis- adjust to readings you are getting
#    plt2.plot(cycl, 'b^-', label='Pressure (Pa)') #plot pressure data
#    plt2.set_ylabel('Pressrue (Pa)')                    #label second y axis
#    plt2.ticklabel_format(useOffset=False)           #Force matplotlib to NOT autoscale y axis
#    plt2.legend(loc='upper right')                  #plot the legend

try:
    while True: # While loop that loops forever
        while (arduinoData.inWaiting()==0): #Wait here until there is data
            pass #do nothing    
        arduinoString = arduinoData.readline() #read the line of text from the serial port
        dataArray = str(arduinoString).split(",")   #Split it into an array called dataArray
        temp = float(dataArray[0][2:7])            #Convert first element to floating number and put in temp
        cycl = int(dataArray[1][1:2])            #Convert second element to floating number and put in P
        tempC.append(temp)                     #Build our tempF array by appending temp readings
        Cycle.append(cycl)                     #Building our pressure array by appending P readings
        drawnow(makeFig)                       #Call drawnow to update our live graph
        plt.pause(.000001)                     #Pause Briefly. Important to keep drawnow from crashing
        cnt=cnt+1
        if(cnt>50):                            #If you have 50 or more points, delete the first one from the array
            tempC.pop(0)                       #This allows us to just see the last 50 data points
            Cycle.pop(0)
except KeyboardInterrupt:
    arduinoData.close()
    pass

0 个答案:

没有答案