Skip to main content
 首页 » 编程设计

Python/Arduino : PySerial, Matplotlib,numpy;树莓派

2025年04月02日26yxwkf

**编辑:我收到以下错误消息:* “检索辅助功能总线地址时出错:or.freedesktop.DBus.Error.ServiceUnknown:名称 org.a11y.Bus 未由任何 .service 文件提供”

我使用 Python 为 PC 平台编写了代码,它工作得很好。我想让这个程序在连接到显示器的 rasberry pi 上运行。

我安装了 Raspbian 和上述所需功能的所有库。

代码如下:

############################################################ 
# Visualizing data from the Arduino serial monotor         # 
# and creating a graph in a nice format. Later work will   # 
# look into outputting this to a Android device            # 
############################################################ 
############################################################ 
 
############################################################ 
############################################################ 
 
#Becuase we can always use more time 
import time 
 
#Inport the fancy plotting library  
import matplotlib.pyplot as plt 
 
#Inport the serial library 
import serial 
 
#We want to do array math so we need Numpy 
import numpy 
 
#We also want to draw things so we need drawnow and 
#everything that comes with it 
from drawnow import * 
 
 
############################################################ 
############################################################ 
 
#Creating an array to hold data for plotting. See several 
#lines below for more information 
 
tempArray = [] 
pressureArray = [] 
altitudeArray = [] 
 
#The object that is created to interact with the Serial Port 
arduinoSerialInput = serial.Serial('/dev/ttyACM0', 230400) 
 
time.sleep(3) 
 
#Here we set the definition oto look for interactive mode 
#and plot live data 
plt.ion() 
 
############################################################ 
############################################################ 
 
clippingCounter = 0 
 
#Here we define a function to make a plot, or figure of 
#the data aquired 
 
def plotTempData(): 
 
    plt.ylim(150,320) 
    plt.title('Plots from data of the BMP Sensor') 
    plt.grid(True) 
    plt.ylabel('Tempature') 
    plt.subplot(2, 2, 1) 
    plt.plot(tempArray, 'ro-', label='Degrees K', linewidth=2) 
    plt.legend(loc='upper left') 
 
    #plt2 = plt.twinx() 
    #plt2.plot(pressureArray, 'bx-') 
 
def plotPresData(): 
 
    #plt.ylim(260,320) 
    plt.title('Plots from data of the BMP Sensor') 
    plt.grid(True) 
    plt.ylabel('Pressure') 
    plt.subplot(2, 2, 2) 
    plt.plot(pressureArray, 'bo-', label='Pascals', linewidth=2) 
    plt.legend(loc='upper left') 
    plt.plot(pressureArray, 'bo-') 
    plt.show() 
 
############################################################ 
############################################################ 
 
while (1==1): 
    #First things first, lets wait for data prior to reading 
    if (arduinoSerialInput.inWaiting()>0): 
        myArduinoData = arduinoSerialInput.readline() 
#        print myArduinoData -Line commented out, see below 
 
#The arduino should be set up to proovide one string of data 
#from here we will seperate that one string into two or more 
#to make it easier to plot 
 
        #This will create an array seperated by the comma 
        #we specified in the Arduino IDE aasign it to a 
        #variable of our choice and convert the string 
        #back into a number using the float command 
        dataArray = myArduinoData.split(',') 
        temp =  float(dataArray[0]) 
        pressure = float(dataArray[1]) 
 
        #Used to test the printing of values to output 
        #print temp, " , ", pressure, " , ", altitude 
 
#We need to create an array to to hold the values in question 
#Looking at the top, you will see the empty array we need to 
#create that will hold the values we need to plot 
        tempArray.append(temp) 
        pressureArray.append(pressure) 
 
        plt.figure(1) 
        drawnow(plotTempData) 
        plt.pause(.000001) 
 
        #plt.figure(2) 
        drawnow(plotPresData) 
        plt.pause(.000001) 
 
 
 
#We want to clip some data off since the graph keeps 
#expanding and too many points are building up. 
        clippingCounter = clippingCounter + 1 
        if(clippingCounter>50): 
          tempArray.pop(0) 
          pressureArray.pop(0) 
 
 
 
############################################################ 
############################################################ 

请您参考如下方法:

我认为您提到的错误是“at-spi2-core”模块的问题。该问题似乎已在 Ubuntu 2.9.90-0ubuntu2 版本上修复,您是否尝试升级“at-spi2-core”模块?或者强制启用它可能会解决您的问题。

有关确保启用 SPI 的信息如下: http://www.raspberrypi-spy.co.uk/2014/08/enabling-the-spi-interface-on-the-raspberry-pi/

我会在评论中添加这个,因为我不是 100% 对此但我没有代表。