我遇到了一个一直困扰着我的错误,我无法找到解决方法。我有一个python程序,它利用PySerial从脉搏血氧仪导入串行端口值。问题是,当函数读写线()被调用(换句话说,当python程序被告知从Arduino的串行监视器读取值时,Arduino的串行值将被扭曲,程序将返回值解压缩错误。在
这是python程序。在
import serial
import time
import pylab
import matplotlib.pyplot as plt
import numpy as np
import os
import csv
#time load
timestr = time.strftime("%Y_%m_%d")
#establish serial connection with ACM0
ser = serial.Serial('/dev/ttyACM0', 115200)
#establish variables
thymeL = [ ]
bpmL = [ ]
sp02L = [ ]
array_data = thymeL, bpmL, sp02L
#declare time
thyme = 1
#graph attributes
plt.ion()
plt.title("Pulse [BPM] & SPo2 [%] v. Time [s]", fontsize = "16")
plt.xlabel("Time [s]", fontsize = "14")
plt.ylabel("Pulse (red) [BPM] & SPo2 (blue) [%]", fontsize = "14")
while True:
data_in = ser.readline()
print data_in
data_in = data_in.strip('\n')
bpm,sp02 = data_in.split(",")
#convert string vals to float
thyme = float(thyme)
bpm = float(bpm)
sp02 = float(sp02)
#print to terminal
print "Time [s]: %s" % (thyme)
print "HR [BPM]: %s" % (bpm)
print "SPO2 [%%]: %s" % (sp02)
print
#append vectors
thymeL.append(thyme)
bpmL.append(bpm)
sp02L.append(sp02)
#print values to plot
plt.scatter(thyme,bpm,color="red")
plt.scatter(thyme,sp02,color="blue")
plt.pause(0.1)
time.sleep(0.05)
#update time
thyme = thyme + 0.5
#write to .csv
with open(full_path, 'w') as f:
writer = csv.writer(f)
for t, b, s in zip(array_data[0], array_data[1], array_data[2]):
writer.writerow([t, b, s])
最重要的片段是:
Arduino程序如下:
#include <PinChangeInt.h>
#include <eHealth.h>
int cont = 0;
void setup() {
Serial.begin(115200);
eHealth.initPulsioximeter();
PCintPort::attachInterrupt(6, readPulsioximeter, RISING);
}
void loop() {
char buffer[32]; // make sure buffer is large enough
sprintf(buffer,"%d,%d \n",eHealth.getBPM(),eHealth.getOxygenSaturation());
Serial.print(buffer);
delay(500);
}
//=========================================================================
void readPulsioximeter(){
cont ++;
if (cont == 50) { //Get only of one 50 measures to reduce the latency
eHealth.readPulsioximeter();
cont = 0;
}
}
因此,串行监视器输出的值如下所示:
67,95
66,95
67,96
等等。在
但是只有在读写线调用()时,值将倾斜,无法通过split(',')函数解压。在下面的照片(1)和(2)中,可以看到当读写线()被调用。在
我如何重新编写python或Arduino程序,以避免这种扭曲,并允许分割和解包值而不出现任何错误?在
那么,是否存在其他东西异步调用loop()的可能性,例如从一个中断例程,它可能试图在arduino对循环函数的“主线”调用的同时,同时传输另一个读数字符串?旁白:如果中断例程readPulsioximeter()在调用mainline loop()函数之间调用eHealth.getBPM()和eHealth.getOxygenSaturation公司()并更新了Pulsioximeter属性,您的代码是否保证将相同读数的值发送到串行端口?在
相关问题 更多 >
编程相关推荐