实例介绍
【实例简介】使用的蓝牙数据波形显示
【实例截图】
【核心代码】
using 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(); } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论