实例介绍
【实例简介】
【实例截图】
【核心代码】
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.Windows.Forms.DataVisualization.Charting;
namespace Oscilloscope
{
public partial class Oscilloscope : Form
{
#region 变量定义
//ConfigPrama configPramaClass;// = new ConfigPrama();
public Int32 recFrameNum = 0; //接收到的数据帧数
public Int32 recByteNum = 0; //接收到的数据字节数
private List<byte> recBuffer = new List<byte>();
private bool handerListening = false;
private bool comClosing = false;
private int pointIndex = 0;
private bool axisChange = false;
#endregion
public Oscilloscope()
{
InitializeComponent();
}
#region 串口初始化
private void buttonSwitch_Click(object sender, EventArgs e)
{
try
{
if (serialPort1.IsOpen)
{
comClosing = true;//串口关闭动作正在进行
while (handerListening) Application.DoEvents();//关闭串口前处理完消息队列中的所有事件
serialPort1.Close();
if (!serialPort1.IsOpen)
{
buttonSwitch.Text = "打开串口";
labelStatus.ForeColor = Color.Black;
}
}
else
{
comClosing = false;
serialPort1.PortName = comboBoxPort.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBoxBaudRate.Text);
serialPort1.DataBits = Convert.ToInt32(comboBoxData.Text);
switch (comboBoxCheck.Text)
{
case "None": serialPort1.Parity = System.IO.Ports.Parity.None; break;
case "Odd": serialPort1.Parity = System.IO.Ports.Parity.Odd; break;
case "Even": serialPort1.Parity = System.IO.Ports.Parity.Even; break;
case "Mark": serialPort1.Parity = System.IO.Ports.Parity.Mark; break;
case "Space": serialPort1.Parity = System.IO.Ports.Parity.Space; break;
default: break;
}
serialPort1.StopBits = (System.IO.Ports.StopBits)Convert.ToInt32(comboBoxStopBit.Text);
try
{
serialPort1.Open();
}
catch(Exception ex)
{
//现实异常信息给客户。
MessageBox.Show(ex.Message);
//捕获到异常信息,创建一个新的comm对象,之前的不能用了。
//serialPort1 = new SerialPort();
}
if (serialPort1.IsOpen)
{
buttonSwitch.Text = "关闭串口";
labelStatus.ForeColor = Color.Red;
}
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
#endregion
#region 串口接收
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
//struct _axis
//{
double xValue;
double yValue = 0;
double zValue = 0;
byte[] xVarry = new byte[2];
byte[] yVarry = new byte[2];
byte[] zVarry = new byte[2];
////}axis;
// byte[] a = new byte[2];
// a[0] = 0xfb;
// a[1] = 0xff;
// Int16 b = BitConverter.ToInt16(a, 0);
// double c = Convert.ToDouble(BitConverter.ToInt16(a, 0));
if (comClosing == true) return; //如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环
try
{
handerListening = true;//设置标记,说明已经开始处理数据,一会儿要使用系统UI的。
int n = serialPort1.BytesToRead;
byte[] buf = new byte[n];
recFrameNum ;
recByteNum = n;
serialPort1.Read(buf, 0, n);
recBuffer.AddRange(buf);
//对接收到的数据进行显示
this.Invoke((EventHandler)(delegate
{
labelRecByteCount.Text = Convert.ToString(recByteNum);
labelRecFrameCount.Text = Convert.ToString(recFrameNum);
if (checkBoxRecDisplay.Checked == true)
{
foreach (var temp in buf) { richTextBox1.Text = temp.ToString("X2") " "; }
richTextBox1.AppendText("\r\n");
this.richTextBox1.ScrollToCaret();
}
}));
if(buf.Length >= 8)
{
if (buf[0] == ConfigPrama.FrameHeader && buf[7] == ConfigPrama.FrameTail)
{
//x轴
xVarry[1] = buf[1];
xVarry[0] = buf[2];
xValue = Convert.ToDouble(BitConverter.ToInt16( xVarry,0));
//y轴
yVarry[1] = buf[3];
yVarry[0] = buf[4];
yValue = Convert.ToDouble(BitConverter.ToInt16(yVarry, 0));
//轴
zVarry[1] = buf[5];
zVarry[0] = buf[6];
zValue = Convert.ToDouble(BitConverter.ToInt16(zVarry, 0));
this.Invoke((EventHandler)(delegate { paint_ax(xValue, yValue, zValue); }));
}
}
else
{
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
handerListening = false;
}
}
#endregion
#region 清空接收区和接收记录
private void buttonClear_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
recFrameNum = 0;
recByteNum = 0;
labelRecByteCount.Text = "0";
labelRecFrameCount.Text = "0";
}
#endregion
private void buttonSave_Click(object sender, EventArgs e)
{
}
#region 绘图接口
void paint_ax(double x,double y,double z)
{
// Define some variables
int numberOfPointsInChart = 100;
//int numberOfPointsAfterRemoval = 40;
// Simulate adding new data points
//int numberOfPointsAddedMin = 5;
//int numberOfPointsAddedMax = 10;
try
{
chart1.ResetAutoValues();
if (checkBoxX.Checked == true)
{
//chart1.Series["xAxis"].Points.AddY(x);
chart1.Series["xAxis"].Points.AddXY(pointIndex, x);
;
}
if (checkBoxY.Checked == true)
{
chart1.Series["yAxis"].Points.AddXY(pointIndex,y);
}
if (checkBoxZ.Checked == true)
{
chart1.Series["zAxis"].Points.AddXY(pointIndex,z);
}
pointIndex ;
// Keep a constant number of points by removing them from the left
//if (chart1.Series[0].Points.Count > numberOfPointsInChart)
//{
// // Remove data points on the left side
// while (chart1.Series[0].Points.Count > numberOfPointsAfterRemoval)
// {
// chart1.Series[0].Points.RemoveAt(0);
// //chart1.Series[1].Points.RemoveAt(0);
// //chart1.Series[2].Points.RemoveAt(0);
// }
//}
if(chart1.Series[0].Points.Count > numberOfPointsInChart)
{
axisChange = true;
// Remove data points on the left side
//while (chart1.Series[0].Points.Count > numberOfPointsAfterRemoval)
//{
// chart1.Series[0].Points.RemoveAt(0);
//}
}
// Adjust X axis scale
if(axisChange)
{
chart1.ChartAreas["ChartArea1"].AxisX.Minimum = pointIndex 1 - numberOfPointsInChart;
chart1.ChartAreas["ChartArea1"].AxisX.Maximum = chart1.ChartAreas["ChartArea1"].AxisX.Minimum numberOfPointsInChart;
//chart1.ChartAreas["ChartArea1"].AxisY.Minimum = -100;
}
// Invalidate chart
chart1.Invalidate();
// Set series chart type
//chart1.Series["xAxis"].ChartType = SeriesChartType.Line;
// Set point labels
//chart1.Series["xAxis"].IsValueShownAsLabel = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region 清除绘图
private void buttonGclear_Click(object sender, EventArgs e)
{
chart1.Series["xAxis"].Points.Clear();
chart1.Series["yAxis"].Points.Clear();
chart1.Series["zAxis"].Points.Clear();
pointIndex = 0;
chart1.ChartAreas["ChartArea1"].AxisX.Minimum = 0;
chart1.ChartAreas["ChartArea1"].AxisX.Maximum = 100;
axisChange = false;
}
#endregion
private void comboBoxPort_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void chart1_Click(object sender, EventArgs e)
{
}
}
}
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)