使用 PulseSensor 脉搏传感器测量心率之二:数据采集及保存( Processing&Python)

通常使用Pulse sensor心率传感器和arduino UNO搭建完硬件平台后,使用上位机PulseSensor_Amped_Processing_Visualizer软件,就可以查看实时心率图、心率值 BPM 和 脉搏间隔 IBI,然而传感器采集到数据只是前提,对数据的处理才是一切应用的核心,因此,需要对arduino发送过来的串口数据进行保存。
通过两种方式进行保存
1、修改现有的上位机软件(Processing)
2、使用Python读取串口数据并保存
一、修改现有的上位机软件(Processing)
在本例中使用PrintWriter对象,官方参考示列如下:

PrintWriter output;   //Create Object

void setup() {
  // Create a new file in the sketch directory
  output = createWriter("positions.txt"); 
}

void draw() {
  point(mouseX, mouseY);
  output.println(mouseX);  // Write the coordinate to the file
}

void keyPressed() {
  output.flush();  // Writes the remaining data to the file
  output.close();  // Finishes the file
  exit();  // Stops the program
}

修改上位机软件方式如下:
1、在主程序 PulseSensorAmpd_Processing_Visualizer.pde中,添加代码,当程序打开时,生成一个TXT文本,用于记录串口数据。
在void setup() { 语句前声明一个 PrintWriter 对象,名字叫output
PrintWriter的使用,请参考

PrintWriter output; //output as txt  新增代码

void setup() {      //原程序代码

数据采集后,按照年月日格式命名TXT 文件

String outputname = "PulseSensordata-"+str(year())+str(month())+ str(day())+"-"+str(hour())+" "+str(minute()) +" "+ str(second()) +".txt";    //  新增代码
output = createWriter(outputname);   //  新增代码

2 在串口读取程序serialEvent.pde添加代码

inData = trim(inData); // cut off white space (carriage return)新增代码

output.println("b'"+ inData );   // 原程序代码

程序运行后,在主程序所在目录下,将会生成Txt文本,实例如下:
PulseSensordata-2018616-18 54 42.txt
内容如下:

b'S0
b'S413
b'S439
b'S484
b'S490
b'S468
b'S450
b'S430
b'S424
b'S405

二、使用Python读取串口数据并保存
由于Python在数据处理方面的优势,上位机软件可以使用Python实现,本章节将演示Python读取串口数据并保存,需要确认设备管理中查询串口使用哪个口,本例为COM5口,演示如下:

import serial
import serial.tools.list_ports
from datetime import datetime

now = datetime.now()
outputname="PulseSensordata-" + str(now.year)+ str(now.month) + str(now.day) +"-" +str(now.hour)+" "+ str(now.minute)+" "+ str(now.second)+".txt"

t = serial.Serial('com5',115200)
while 1: 
     with open(outputname, 'a+') as f:      #a在 原文件 追加中
          print(t.readline() , file=f)

后续还有很大的空间进行改写上位机以及深度开发和优化,目前我们将精力集中到后续章节:使用 PulseSensor 脉搏传感器测量心率之三:时域波形显示及频域波形显示

  • 8
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值