2017-07-11 86 views
-1

シリアルポートから浮動小数点数をリアルタイムでプロットする必要があります。pyqtgraphを使ったpythonリアルタイムプロット

どのようにデータをプロットでしょう... X1、X2 X3 :これらの値は、データ・シーケンスは、このようなものであるので、「\ n」は文字でsepparatedていますか? Arduinoボードを使用しています。データレートは200サンプル/秒、PCはWindows7 64ビットで動作しています。 私はpyqtgraphライブラリを使うことをお勧めします。私はpyttgraphでPlotting.pyの例を使い始めましたが、私のニーズに合わせてこのコードをどのように適応させるかはわかりません(下記参照)。 ありがとうございます。

from pyqtgraph.Qt import QtGui, QtCore 
import numpy as np 
import pyqtgraph as pg 

# Set graphical window, its title and size 
win = pg.GraphicsWindow(title="Sample process") 
win.resize(1000,600) 
win.setWindowTitle('pyqtgraph example') 

# Enable antialiasing for prettier plots 
pg.setConfigOptions(antialias=True) 

# Random data process 
p6 = win.addPlot(title="Updating plot") 
curve = p6.plot(pen='y') 
data = np.random.normal(size=(10,1000)) # If the Gaussian distribution shape is, (m, n, k), then m * n * k samples are drawn. 

# plot counter 
ptr = 0 

# Function for updating data display 
def update(): 
    global curve, data, ptr, p6 
    curve.setData(data[ptr%10]) 
    if ptr == 0: 
     p6.enableAutoRange('xy', False) ## stop auto-scaling after the first data set is plotted 
    ptr += 1 

# Update data display  
timer = QtCore.QTimer() 
timer.timeout.connect(update) 
timer.start(50) 


## Start Qt event loop unless running in interactive mode or using pyside. 
if __name__ == '__main__': 
    import sys 
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): 
     QtGui.QApplication.instance().exec_() 
+0

問題が十分に定義されていません。現在のコードは、 'data'という名前のオブジェクトに含まれる浮動小数点のリストをプロットします。それはあなたの仕事の一部ですが、Arduinoのデータストリームはどうですか?それはどのようにフォーマットされ、どのように数字に解析することを提案しますか?あなたがそれに対処するまで、誰もが言えることはほとんどありません。 –

+0

私のarduinoボードは、 '\ n'文字の後に一連のfloat値を送信しています。 x1 x2 x3 ... これらの値をリアルタイムでプロットしたいと思います。どうすればいいですか? お時間をいただきありがとうございます。 – fmarengo

+0

これは私がやることです:シリアルポート(PySerial)にアクセスするPythonパッケージを入手してください。それをインストールします。 COM10を正しいボーレートと他のポートパラメータで開くためのスクリプトを作成します。タイトなループで、Adruinoからデータを収集します。各文字列を浮動小数点数に変換し、コンソールに出力します。エラーがないことを確認してください。その作業を済ませて初めて、リアルタイムプロットの問題に取り組むべきです。そのデータ取得ループが機能するように助けが必要な場合は、ここに別の質問を投稿して、必要なヘルプを入手する必要があります。 –

答えて

0

私はpyqtgraphでリアルタイムプロットを実装しています。 https://github.com/Jonksar/cowculator/blob/master/examples/plotting_example.py

仕事をしているコードはここにある:ここでhttps://github.com/Jonksar/cowculator/blob/master/cowculator/plotting.py

+0

ありがとう、Joonatan、しかし私はPythonの初心者です。シリアルポート経由のデータにコードを適用するにはどうすればよいですか? – fmarengo

+0

シリアルポート経由で読書できますか? –

0

は正常に動作コードであるあなたはここに例があり、私の実装を使用することができます。メインプロセスは、update()機能に含まれています。シリアルポートから入力値を読み取り、配列Xm(入力値を含む)を更新し、関連するカーブを更新します。

このコードは簡潔にするために掲載されたもので、低データレート(100サンプル未満)でのみ動作します。より高いデータレートの場合は、update()の機能の中で次のように変更する必要があります。 1つの値ではなく、一連の値をシリアルポートから読み取る必要があります。次に、そのようなセットは配列に追加する必要がありますXm

私はこの答えがあなたにとって有益であることを願っており、大変ありがとうございます。

# Import libraries 
from numpy import * 
from pyqtgraph.Qt import QtGui, QtCore 
import pyqtgraph as pg 
import serial 

# Create object serial port 
portName = "COM12"      # replace this port name by yours! 
baudrate = 9600 
ser = serial.Serial(portName,baudrate) 

### START QtApp ##### 
app = QtGui.QApplication([])   # you MUST do this once (initialize things) 
#################### 

win = pg.GraphicsWindow(title="Signal from serial port") # creates a window 
p = win.addPlot(title="Realtime plot") # creates empty space for the plot in the window 
curve = p.plot()      # create an empty "plot" (a curve to plot) 

windowWidth = 500      # width of the window displaying the curve 
Xm = linspace(0,0,windowWidth)   # create array that will contain the relevant time series  
ptr = -windowWidth      # set first x position 

# Realtime data plot. Each time this function is called, the data display is updated 
def update(): 
    global curve, ptr, Xm  
    Xm[:-1] = Xm[1:]      # shift data in the temporal mean 1 sample left 
    value = ser.readline()    # read line (single value) from the serial port 
    Xm[-1] = float(value)     # vector containing the instantaneous values  
    ptr += 1        # update x position for displaying the curve 
    curve.setData(Xm)      # set the curve with this data 
    curve.setPos(ptr,0)     # set x position in the graph to 0 
    QtGui.QApplication.processEvents() # you MUST process the plot now 

### MAIN PROGRAM #####  
# this is a brutal infinite loop calling your realtime data plot 
while True: update() 

### END QtApp #### 
pg.QtGui.QApplication.exec_() # you MUST put this at the end 
################## 
関連する問題