欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

PC端的蓝牙数据波形显示软件,搭配单片机使用

介绍 评论 失效链接反馈

使用的蓝牙数据波形显示
from clipboardusing System;using System.Threading;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Windows.Forms.DataVisualization.Charting;using InTheHand.Net;using InTheHand.Net.Bluetooth;using InTheHand.Net.Sockets;using InTheHand.Windows.Forms;using System.Net.Sockets;namespace 蓝牙示波器{ public partial class 蓝牙示波器 : Form { BluetoothRadio Radio = null; //蓝牙适配器 BluetoothAddress SendAdder = null; //发送的目的地址 BluetoothClient BlueClient = null; //监听远程蓝牙设备 BluetoothEndPoint BluetoothEndPoint = null; NetworkStream networkStream; //Thread LabThread = null; // 显示的线程 private Queue<double> DataQueue = new Queue<double>(50); //定义数据队列 private int curValue = 0; private int Num = 1; //每次删除增加几个点 static private int Value1 = 0; string Name; bool Status = false; private bool LinkStatus = false; private bool ButtonStatus = true; public 蓝牙示波器() { InitializeComponent(); } private void 蓝牙示波器_Load_1(object sender, EventArgs e) { /*放程序主体的地方*/ Radio = BluetoothRadio.PrimaryRadio; //获取当前PC的蓝牙适配器 CheckForIllegalCrossThreadCalls = false; //不检测跨线程使用 if (Radio == null) { MessageBox.Show("电脑蓝牙未打开或者未发现!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } else { label1.Text = "电脑蓝牙已经打开"; button1.Text = "连接蓝牙...."; InitChart(); //初始化图表 ChoiceBluetooth(); //选择蓝牙的函数,选择蓝牙 } } //初始化图表的函数 private void InitChart() { //定义图表区 this.chart1.ChartAreas.Clear(); ChartArea chartArea = new ChartArea("C1"); this.chart1.ChartAreas.Add(chartArea); //定义存储和显示点的容器 this.chart1.Series.Clear(); Series series = new Series("S1"); series.ChartArea = "C1"; this.chart1.Series.Add(series); //设置图表样式 this.chart1.ChartAreas[0].AxisY.Minimum = 0; this.chart1.ChartAreas[0].AxisY.Maximum = 25; this.chart1.ChartAreas[0].AxisX.Interval = 5; this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Silver; this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Silver; //设置标题 this.chart1.Titles.Clear(); this.chart1.Titles.Add("S01"); this.chart1.Titles[0].Text = "压力波形显示"; this.chart1.Titles[0].ForeColor = Color.RoyalBlue; this.chart1.Titles[0].Font = new Font("Microsoft Sans Serif", 12F); //设置图表显示样式 this.chart1.Series[0].Color = Color.Red; this.chart1.Series[0].ChartType = SeriesChartType.FastLine; //显示的类型是条形图 this.chart1.Series[0].Points.Clear(); //清除数据点集合 } private void LinkBluetooth() //连接蓝牙的函数 { if (BlueClient != null) { Thread.Sleep(1000); BlueClient.Close(); } LinkStatus = false; try { BlueClient = new BluetoothClient(); BluetoothEndPoint = new BluetoothEndPoint(SendAdder, BluetoothService.SerialPort); BlueClient.Connect(BluetoothEndPoint); //连接蓝牙 if (BlueClient.Connected) { label3.Text = "成功与 " Name " 连接成功>>>>>"; LinkStatus = true; //代连接成功蓝牙 } } catch (SocketException) { label3.Text = "连接 " Name " 设备不成功>>>>>>"; LinkStatus = false; //代表连接不成功蓝牙 if (BlueClient != null) { // [注意3]:要延迟一定时间(例如1000毫秒) //避免因连接后又迅速断开而导致蓝牙进入异常(傻逼)状态 Thread.Sleep(1000); BlueClient.Close(); } } } private void ChoiceBluetooth() //选择蓝牙的函数 { MessageBox.Show("请选择连接的蓝牙设备", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); SelectBluetoothDeviceDialog Dialog = new SelectBluetoothDeviceDialog { ShowRemembered = true, //显示记住的蓝牙设备 ShowAuthenticated = true, //显示认证过的蓝牙设备 ShowUnknown = true //显示位置蓝牙的设备 }; //创建一个船体 if (Dialog.ShowDialog() == DialogResult.OK) { SendAdder = Dialog.SelectedDevice.DeviceAddress;//获取选择的远程蓝牙的地址 Name = Dialog.SelectedDevice.DeviceName; label2.Text = "设备名:" Dialog.SelectedDevice.DeviceName; label3.Text = "连接设备信息"; //try //{ // BlueClient = new BluetoothClient(); // BluetoothEndPoint = new BluetoothEndPoint(SendAdder, BluetoothService.SerialPort); // BlueClient.Connect(BluetoothEndPoint); //连接蓝牙 //} //catch //{ //} Status = true; } else { label2.Text = "没有选择蓝牙设备。。。。"; Status = false; } } private void button1_Click(object sender, EventArgs e) { if (ButtonStatus == true) // 采集数据的操作 { if (LinkStatus == false) //检测连接的状态 { if (Status == true) { LinkBluetooth(); //连接蓝牙 /* * 由于正确选择蓝牙, * 进入数据采集和画波形的地方 */ } else { /* 由于没有正确选择蓝牙发生的,所以重新选择蓝牙 */ ChoiceBluetooth(); //选择蓝牙 if (Status == true) { LinkBluetooth(); //连接蓝牙 /* * 由于正确选择蓝牙, * 进入数据采集和画波形的地方 */ } else { MessageBox.Show("没有选择蓝牙设备,应用将自动关闭。。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); Thread.Sleep(1000); this.Close(); } } if (LinkStatus == true) //检测是否连接了蓝牙 { /*连接成功,就采集数据*/ timer1.Start(); //开启定时器 button1.Text = "暂停压力波形显示"; ButtonStatus = false; } else { MessageBox.Show("蓝牙设备没有连接!系统系统自动重新连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LinkBluetooth(); //连接蓝牙 /* * 由于正确选择蓝牙, * 进入数据采集和画波形的地方 */ if (LinkStatus == true) { /*连接成功,就采集数据*/ timer1.Start(); //开启定时器 button1.Text = "暂停压力波形显示"; ButtonStatus = false; } else { MessageBox.Show("没有连接蓝牙设备!系统将自动退出", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); Thread.Sleep(1000); this.Close(); } } /* * 上面是具体的连接蓝牙的操作 * 将获得一个蓝牙连接成功的形线 * 如果没有选择蓝牙或者连接不成功多次 * 系统将自动结束 */ } else { /* * 开启定时器,采集数据显示数据波形什么的 蓝牙是成功连接的 ,启发定时器采集数据 */ timer1.Start(); button1.Text = "暂停压力波形显示"; ButtonStatus = false; } } else { /* * 关闭数据采集的操作 * * */ timer1.Stop(); //暂停定时器 SendStopData(); //发送到单片机停止信息 button1.Text = "开始压力波形显示"; ButtonStatus = true; } } private void SendStopData() { byte[] SendData = { 0x0f, 0xf0, 0x15, 0x0a, 0x0d }; networkStream = BlueClient.GetStream(); if (networkStream.CanWrite) { networkStream.Write(SendData, 0, SendData.Length); //发送数据使单片机开始采集数 } } //发送停止信息函数 private void SendStartData() //发送到单片机开始信息函数 { byte[] StartData = { 0x0f, 0xf0, 0x05, 0x0a, 0x0d }; networkStream = BlueClient.GetStream(); if (networkStream.CanWrite) { networkStream.Write(StartData, 0, StartData.Length); //发送数据使单片机开始采集数据 } Thread.Sleep(300); } private int ReceiceDataProc() //接收数据并处理的函数 { int ValueData = 0; byte[] ReceiceData = new byte[512]; networkStream = BlueClient.GetStream(); //获取网络流 if (networkStream.CanRead) //接收数据 { int DataSize = 0; DataSize = networkStream.Read(ReceiceData, 0, 512); //label6.Text = "接收到" DataSize.ToString() "字节数据"; if (DataSize != 0) { ValueData = DataHandle(ReceiceData, DataSize); } /*调用数据处理函数*/ } return ValueData; } private int DataHandle(byte[] receiceData, int dataSize) //数据平均值滤波 { int Sum = Value1; int Count = 1; int ValueSum = 0; for (int i = 0; (dataSize - i) > 5; i ) { ValueSum = 0; if ((receiceData[i] == 0x0f && receiceData[i 1] == 0xf0) || receiceData[i] == 0xf0) //查找帧头 { if (receiceData[i] == 0x0f && receiceData[i 1] == 0xf0) { for (int j = i 2; j < dataSize; j ) { if (receiceData[j] == 0x0a && receiceData[j 1] == 0x0d) { i = j 1; break; } else { ValueSum = (ValueSum << 8) | receiceData[j]; } } Sum = Sum ValueSum; Count ; } else if (receiceData[i] == 0xf0) { for (int j = i 1; j < dataSize; j ) { if (receiceData[j] == 0x0a && receiceData[j 1] == 0x0d) { i = j 1; break; } else { ValueSum = (ValueSum << 8) | receiceData[j]; } } Sum = Sum ValueSum; Count ; } } } Sum = Sum / Count; Value1 = Sum; return Sum; } private int DataGreat() //获取数据 { int DataValue = 0; if (LinkStatus == true) //判断是否连接蓝牙呢! { try { SendStartData(); //发送开始信号 DataValue = ReceiceDataProc();//接收数据 SendStopData();//发送结束信号 } catch { DataValue = Value1; } } else { return 0; } return DataValue; } private void UpDataQueueValue() { if (DataQueue.Count > 50) { //先出列 for (int i = 0; i < Num; i ) { DataQueue.Dequeue(); //移除队列中的值 } } for (int i = 0; i < Num; i ) { curValue = DataGreat(); //采样AD转换值 double Voltage_value = ((curValue)* 5.0) / 1024; //压力的测试范围是0.2——20N double F = ((Voltage_value * (20 - 0.2)) / 5) 0.2; DataQueue.Enqueue(F); //显示数据放入队列 label4.Text = F.ToString() "N"; byte[] SendData = { 0x0f, 0xf0, 0x15, 0x0a, 0x0d }; networkStream = BlueClient.GetStream(); if (networkStream.CanWrite) { networkStream.Write(SendData, 0, SendData.Length); //发送数据使单片机开始采集数据 } } } private void timer1_Tick(object sender, EventArgs e) { try { UpDataQueueValue(); //更新数据 this.chart1.Series[0].Points.Clear(); for (int i = 0; i < DataQueue.Count; i ) { this.chart1.Series[0].Points.AddXY((i 1), DataQueue.ElementAt(i)); } } catch { byte[] SendData = { 0x0f, 0xf0, 0x15, 0x0a, 0x0d }; networkStream = BlueClient.GetStream(); if (networkStream.CanWrite) { networkStream.Write(SendData, 0, SendData.Length); //发送数据使单片机开始采集数据 } this.Close(); } } private void 蓝牙示波器_FormClosed(object sender, FormClosedEventArgs e) { try { timer1.Stop(); byte[] SendData = { 0x0f, 0xf0, 0x15, 0x0a, 0x0d }; networkStream = BlueClient.GetStream(); if (networkStream.CanWrite) { networkStream.Write(SendData, 0, SendData.Length); //发送数据使单片机开始采集数据 } } catch { } if (BlueClient != null) { // [注意3]:要延迟一定时间(例如1000毫秒) //避免因连接后又迅速断开而导致蓝牙进入异常(傻逼)状态 Thread.Sleep(1000); BlueClient.Close(); } } }}

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复