资源简介
硬件背景:基于stm32芯片,挂载心率监测传感器通过蓝牙传输数据的 心率监测仪
本文将介绍下如何根据 接收到的数据进行动态绘制心率折线图(大神请绕步,此乃菜鸟分享show)
本案例是比较粗糙的动态绘制心率折线图,所以这里拿时间变量来刷新绘制折线图
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Text.Regularexpressions;
namespace SerialportSample
{
public partial class SerialportSampleForm : Form
{
private SerialPort comm = new SerialPort();
private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。
private long received_count = 0;//接收计数
private long send_count = 0;//发送计数
//画图需要
private int[] RawY = new int[600];
private int RawY0 = 0;
//##
public SerialportSampleForm()
{
InitializeComponent();
}
//窗体初始化
private void Form1_Load(object sender EventArgs e)
{
//初始化下拉串口名称列表框
string[] ports = SerialPort.GetPortNames();
Array.Sort(ports);
comboPortName.Items.AddRange(ports);
comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;
comboBaudrate.SelectedIndex = comboBaudrate.Items.IndexOf(“9600“);
//初始化SerialPort对象
comm.NewLine = “\r\n“;
comm.RtsEnable = true;//根据实际情况吧。
comm.ReceivedBytesThreshold = 6; //触发字节数是11 ##特别重要
//添加事件注册
comm.DataReceived += comm_DataReceived;
}
void comm_DataReceived(object sender SerialDataReceivedEventArgs e)
{
int n = comm.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
received_count += n;//增加接收计数
comm.Read(buf 0 n);//读取缓冲数据
builder.Clear();//清除字符串构造器的内容
//因为要访问ui资源,所以需要使用invoke方式同步ui。
this.Invoke((EventHandler)(delegate
{
//判断是否是显示为16禁止
if (checkBoxHexView.Checked)
{
//依次的拼接出16进制字符串
foreach (byte b in buf)
{
builder.Append(b.ToString(“X2“) + “ “);
}
}
else
{
//直接按ASCII规则转换成字符串
builder.Append(Encoding.ASCII.GetString(buf));
}
//######## 数据包 分析
//
byte h = 0;
//String str1 = builder.ToString(); // 通过分析缓冲区接收到的数据包的首字符,
String str1 = this.comm.ReadTo(“\n“).ToString();
if (str1.Length > 0)
{
if (str1[0] == ‘S‘)
{ h = 0;
str1 = str1.Substring(1);
}
else if (str1[0] == ‘B‘)
{ h = 1;
str1 = str1.Substring(1);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
....SH. 77 2016-11-02 19:57 SerialportSample\Desktop.ini
文件 340992 2016-11-12 10:05 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Debug\Serialport.exe
文件 36352 2016-11-12 10:05 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Debug\Serialport.pdb
文件 23168 2016-11-12 10:05 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Debug\Serialport.vshost.exe
文件 490 2013-03-18 17:00 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Debug\Serialport.vshost.exe.manifest
文件 339968 2016-05-13 21:33 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Debug\SerialportSample.exe
文件 34304 2016-05-13 21:33 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Debug\SerialportSample.pdb
文件 24224 2016-05-13 22:18 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Debug\SerialportSample.vshost.exe
文件 490 2015-10-30 15:19 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Debug\SerialportSample.vshost.exe.manifest
文件 340480 2016-07-29 10:17 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Release\Serialport.exe
文件 40448 2016-07-29 10:17 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Release\Serialport.pdb
文件 23168 2016-08-02 11:08 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Release\Serialport.vshost.exe
文件 490 2013-03-18 17:00 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Release\Serialport.vshost.exe.manifest
文件 490 2015-10-30 15:19 SerialportSample\SerialportSample\SerialportSample\SerialportSample\bin\Release\SerialportSample.vshost.exe.manifest
文件 11569 2016-08-10 07:24 SerialportSample\SerialportSample\SerialportSample\SerialportSample\Form1.cs
文件 27919 2016-08-02 11:16 SerialportSample\SerialportSample\SerialportSample\SerialportSample\Form1.Designer.cs
文件 483338 2016-08-02 11:16 SerialportSample\SerialportSample\SerialportSample\SerialportSample\Form1.resx
文件 5426 2016-05-10 22:48 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
文件 7171 2016-08-02 11:09 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 324 2010-05-17 01:19 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\GenerateResource.read.1.tlog
文件 812 2010-05-17 01:19 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\GenerateResource.write.1.tlog
文件 340992 2016-11-12 10:05 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\Serialport.exe
文件 36352 2016-11-12 10:05 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\Serialport.pdb
文件 2835 2016-11-12 10:05 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.csproj.FileListAbsolute.txt
文件 977 2016-08-02 11:16 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.csproj.GenerateResource.Cache
文件 9180 2016-11-12 10:05 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.csprojResolveAssemblyReference.cache
文件 339968 2016-05-13 21:33 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.exe
文件 34304 2016-05-13 21:33 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.pdb
文件 180 2016-08-02 11:10 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.Properties.Resources.resources
文件 318866 2016-08-02 11:16 SerialportSample\SerialportSample\SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.SerialportSampleForm.resources
............此处省略42个文件信息
- 上一篇:自己用C#写的tcp_ip服务器
- 下一篇:操作系统实验之银行家算法C# GUI界面
评论
共有 条评论