【脉冲示波器】pyqt5联合arduino实现高速采样及绘图
·

上位机
基于pyqt5、pyqtgraph、pyserial

# -*- coding: utf-8 -*-
"""
Update a simple plot as rapidly as possible to measure pulse.
Author: GHOST
@USTC
"""
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time
app = QtGui.QApplication([])
import serial
import atexit
p = pg.plot()
p.setWindowTitle('Pulse measurement')
p.setRange(QtCore.QRectF(0, 0, 500, 2))
p.setLabel('bottom', 'Index', units='B')
curveR = p.plot(pen = "r")
curveG = p.plot(pen = "g")
curveB = p.plot(pen = "b")
curveY = p.plot(pen = "y")
dataListB = []
dataListG = []
dataListR = []
dataListY = []
#Connecting to Arduino
global serialArduino
serialArduino = serial.Serial('COM9', 9600, timeout = 1)#change 115200 to reach a high communication speed
data = []
ptr = 0
lastTime = time()
fps = None
def update():
global curve, data, ptr, p, lastTime, fps
#Get the message
response = serialArduino.readline(50)
message = str(response)
strB = message[2 : 3]
dataListB.append(int(strB))
strR = message[4 : 5]
dataListR.append(int(strR))
strG = message[6 : 7]
dataListG.append(int(strG))
strY = message[8 : 9]
dataListY.append(int(strY))
#Cut the message to reach a high plotspeed
if ptr > 500:
del(dataListB[0])
del(dataListR[0])
del(dataListG[0])
del(dataListY[0])
curveB.setData(dataListB)
curveR.setData(dataListR)
curveG.setData(dataListG)
curveY.setData(dataListY)
ptr += 1
curveB.setPos(ptr, 0)
curveR.setPos(ptr, 0)
curveG.setPos(ptr, 0)
curveY.setPos(ptr, 0)
p.setXRange(max = ptr + 500, min = ptr)
#Show FPS
now = time()
dt = now - lastTime
lastTime = now
if fps is None:
fps = 1.0/dt
else:
s = np.clip(dt*3., 0, 1)
fps = fps * (1-s) + (1.0/dt) * s
p.setTitle('%0.2f fps' % fps)
app.processEvents() ## force complete redraw for every plot
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(1)
#Close serial
def doAtExit():
serialArduino.close()
print("Close serial")
print("serialArduino.isOpen() = " + str(serialArduino.isOpen()))
## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
import sys
print("serialArduino.isOpen() = " + str(serialArduino.isOpen()))
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
atexit.register(doAtExit)
下位机
#define pinR 10
#define pinG 9
#define pinB 8
#define pinY 5
int valR, valG, valB, valY;
void setup() {
// put your setup code here, to run once:
pinMode(pinR, INPUT);
pinMode(pinG, INPUT);
pinMode(pinB, INPUT);
pinMode(pinY, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
valR = analogRead(pinR);
valG = analogRead(pinG);
valB = analogRead(pinB);
valY = analogRead(pinY);
if (valR > 600)
valR = 1;
else
valR = 0;
if (valG > 600)
valG = 1;
else
valG = 0;
if (valB > 600)
valB = 1;
else
valB = 0;
if (valY > 600)
valY = 1;
else
valY = 0;
Serial.print(valB);
Serial.print(",");
Serial.print(valR);
Serial.print(",");
Serial.print(valG);
Serial.print(",");
Serial.print(valY);
Serial.print(",");
Serial.print("\r\n");
delay(10);
}
更多推荐


所有评论(0)