金牌会员
- 积分
- 1461
- 金钱
- 1461
- 注册时间
- 2014-5-21
- 在线时间
- 339 小时
|
楼主 |
发表于 2015-8-25 13:50:30
|
显示全部楼层
本帖最后由 cornrn 于 2016-5-3 09:41 编辑
[mw_shl_code=csharp,true][/mw_shl_code][mw_shl_code=objc,true]主要程序部分展示:
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;
using System.Runtime.InteropServices;
namespace Oscilloscope
{
public partial class DisWaveFrm : Form
{
#region 变量定义
public Int32 recFrameNum = 0, recByteNum = 0, waveRefreshCon = 0; //接收到的数据帧数//接收到的数据字节数//波形刷新计数//波形刷新计数
public string chartTitles = "心电信号", SeriesName1 = "CH1", SeriesName2 = "CH2", SeriesName3 = "CH3";
PackClass PackClass = new PackClass();//实例化PackClass的对象
Global Global = new Global();//实例化Global的对象
private bool handerListening = false;
private bool comClosing = false;
public bool[] tabControl_index = { false, false, false, false, false, false };
#endregion
#region 初始化所有窗体和空间
public DisWaveFrm()
{
InitializeComponent();
}
#endregion
#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);//现实异常信息给客户。
serialPort1 = new SerialPort();//捕获到异常信息,创建一个新的comm对象,之前的不能用了。
}
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)
{
if (comClosing == true) return; //如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环
try
{
handerListening = true;//设置标记,说明已经开始处理数据,一会儿要使用系统UI的。
int n = serialPort1.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
if (n == Global.PackSize)
{
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
serialPort1.Read(buf, 0, n);//读取串口内部缓冲区数据到buf数组
serialPort1.DiscardInBuffer();//清空串口内部缓存
recByteNum += n;//接收字节数计数
recFrameNum++;//帧计数
BitConverter.ToUInt16(buf, 0);
#region 帧入包
//通过接收到的数据设置PackClass类中的属性值
PackClass.CodeVersion = (ushort)((buf[Global.CodeVersion_Index] << 8) + buf[Global.CodeVersion_Index + 1]);
PackClass.Length = (ushort)((buf[Global.Length_Index] << 8) + buf[Global.Length_Index + 1]);
PackClass.Crc = (ushort)((buf[Global.Crc_Index] << 8) + buf[Global.Crc_Index + 1]);
PackClass.Id = (ushort)((buf[Global.Id_Index] << 8) + buf[Global.Id_Index + 1]);
PackClass.EcgDataClass.Type = (ushort)((buf[Global.EcgData_Index] << 8) + buf[Global.EcgData_Index + 1]);
PackClass.EcgDataClass.Length = (ushort)((buf[Global.EcgData_Index + 2] << 8) + buf[Global.EcgData_Index + 3]);
PackClass.EcgMarkClass.Type = (ushort)((buf[Global.EcgMark_Index] << 8) + buf[Global.EcgMark_Index + 1]);
PackClass.EcgMarkClass.Length = (ushort)((buf[Global.EcgMark_Index + 2] << 8) + buf[Global.EcgMark_Index + 3]);
PackClass.EcgMarkClass.Value = (ushort)((buf[Global.EcgMark_Index + 4] << 8) + buf[Global.EcgMark_Index + 5]);
PackClass.BatDataClass.Type = (ushort)((buf[Global.BatData_Index] << 8) + buf[Global.BatData_Index + 1]);
PackClass.BatDataClass.Length = (ushort)((buf[Global.BatData_Index + 2] << 8) + buf[Global.BatData_Index + 3]);
PackClass.BatDataClass.Value = (ushort)((buf[Global.BatData_Index + 4] << 8) + buf[Global.BatData_Index + 5]);
PackClass.AccDataClass.Type = (ushort)((buf[Global.AccData_Index] << 8) + buf[Global.AccData_Index + 1]);
PackClass.AccDataClass.Length = (ushort)((buf[Global.AccData_Index + 2] << 8) + buf[Global.AccData_Index + 3]);
PackClass.GyrDataClass.Type = (ushort)((buf[Global.GyrData_Index] << 8) + buf[Global.GyrData_Index + 1]);
PackClass.GyrDataClass.Length = (ushort)((buf[Global.GyrData_Index + 2] << 8) + buf[Global.GyrData_Index + 3]);
PackClass.MagDataClass.Type = (ushort)((buf[Global.MagData_Index] << 8) + buf[Global.MagData_Index + 1]);
PackClass.MagDataClass.Length = (ushort)((buf[Global.MagData_Index + 2] << 8) + buf[Global.MagData_Index + 3]);
PackClass.ErrDataClass.Type = (ushort)((buf[Global.ErrData_Index] << 8) + buf[Global.ErrData_Index + 1]);
PackClass.ErrDataClass.Length = (ushort)((buf[Global.ErrData_Index + 2] << 8) + buf[Global.ErrData_Index + 3]);
for (sbyte i = 0, j = 0; i < Global.EcgSize; i++)//Global.PackSize=96
{
PackClass.EcgDataClass.LeadiValue = (ushort)((buf[Global.EcgData_Index + 4 + 2 * i] << 8) + buf[Global.EcgData_Index + 5 + 2 * i]);
if (i % 3 == 0)
{
PackClass.AccDataClass.xValue[j] = (short)((buf[Global.AccData_Index + 4 + 2 * j] << 8) + buf[Global.AccData_Index + 5 + 2 * j]);
PackClass.AccDataClass.yValue[j] = (short)((buf[Global.AccData_Index + 68 + 2 * j] << 8) + buf[Global.AccData_Index + 69 + 2 * j]);
PackClass.AccDataClass.zValue[j] = (short)((buf[Global.AccData_Index + 132 + 2 * j] << 8) + buf[Global.AccData_Index + 133 + 2 * j]);
PackClass.GyrDataClass.xValue[j] = (short)((buf[Global.GyrData_Index + 4 + 2 * j] << 8) + buf[Global.GyrData_Index + 5 + 2 * j]);
PackClass.GyrDataClass.yValue[j] = (short)((buf[Global.GyrData_Index + 68 + 2 * j] << 8) + buf[Global.GyrData_Index + 69 + 2 * j]);
PackClass.GyrDataClass.zValue[j] = (short)((buf[Global.GyrData_Index + 132 + 2 * j] << 8) + buf[Global.GyrData_Index + 133 + 2 * j]);
PackClass.MagDataClass.xValue[j] = (short)((buf[Global.MagData_Index + 4 + 2 * j] << 8) + buf[Global.MagData_Index + 5 + 2 * j]);
PackClass.MagDataClass.yValue[j] = (short)((buf[Global.MagData_Index + 68 + 2 * j] << 8) + buf[Global.MagData_Index + 69 + 2 * j]);
PackClass.MagDataClass.zValue[j] = (short)((buf[Global.MagData_Index + 132 + 2 * j] << 8) + buf[Global.MagData_Index + 133 + 2 * j]);
j++;
}
}
#endregion
//对接收到的数据进行显示 //对接收到的数据进行示波
this.Invoke((EventHandler)(delegate//因为要访问ui资源,所以需要使用invoke方式同步ui。
{
labelRecByteCount.Text = Convert.ToString(recByteNum);
labelRecFrameCount.Text = Convert.ToString(recFrameNum);
label_LeadNum.Text = "单";
label_Axis.Text = "九";
if (tabControl_index[0] == true) { BuildChartSeries(PackClass, "ECG"); }
else if (tabControl_index[1] == true) { BuildChartSeries(PackClass, "ACC"); }
else if (tabControl_index[2] == true) { BuildChartSeries(PackClass, "GYR"); }
else if (tabControl_index[3] == true) { BuildChartSeries(PackClass, "MAG"); }
else if (tabControl_index[4] == true) { DisplayDataInRichTextBox(PackClass); }
else if (tabControl_index[5] == true) { ; }
else { BuildChartSeries(PackClass, "ECG"); }
}));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
handerListening = false;
}
}
#endregion
#region 创建richTextBox控件中显示帧数据
public void DisplayDataInRichTextBox(PackClass pack)
{
richTextBox.AppendText("\r\n帧头->");
richTextBox.AppendText(string.Format("{0:X4}", pack.CodeVersion) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.Length) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.Crc) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.Id) + " ");
richTextBox.AppendText("\r\nECG 数据->");
richTextBox.AppendText(string.Format("{0:X4}", pack.EcgDataClass.Type) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.EcgDataClass.Length) + " ");
for (int i = 0; i < Global.EcgSize; i++)
{//int类型数变成16进制后转化成string类型输出,小写将 X->x就好
if (i % Global.AxisSize == 0) richTextBox.AppendText("\r\n"); //换行显示
richTextBox.AppendText(string.Format("{0:X4}", pack.EcgDataClass.LeadiValue) + " ");//显示心电信号
}
richTextBox.AppendText("\r\n打标数据->");
richTextBox.AppendText(string.Format("{0:X4}", pack.EcgMarkClass.Type) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.EcgMarkClass.Length) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.EcgMarkClass.Value) + " ");
for (int i = 0; i < Global.AxisNum; i++)
{
if (i == 0)
{
richTextBox.AppendText("\r\nACC 数据->");
richTextBox.AppendText(string.Format("{0:X4}", pack.AccDataClass.Type) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.AccDataClass.Length) + "\r\n");
}
else if (i == 3)
{
richTextBox.AppendText("\r\nGYR 数据->");
richTextBox.AppendText(string.Format("{0:X4}", pack.GyrDataClass.Type) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.GyrDataClass.Length) + "\r\n");
}
else if (i == 6)
{
richTextBox.AppendText("\r\nMAG 数据->");
richTextBox.AppendText(string.Format("{0:X4}", pack.MagDataClass.Type) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.MagDataClass.Length) + "\r\n");
}
else
{
richTextBox.AppendText("\r\n"); //换行显示
}
for (int j = 0; j < Global.AxisSize; j++)
{
switch (i)
{
case 0: richTextBox.AppendText(string.Format("{0:X4}", pack.AccDataClass.xValue[j]) + " "); break;//显示9轴信号
case 1: richTextBox.AppendText(string.Format("{0:X4}", pack.AccDataClass.yValue[j]) + " "); break;
case 2: richTextBox.AppendText(string.Format("{0:X4}", pack.AccDataClass.zValue[j]) + " "); break;
case 3: richTextBox.AppendText(string.Format("{0:X4}", pack.GyrDataClass.xValue[j]) + " "); break;
case 4: richTextBox.AppendText(string.Format("{0:X4}", pack.GyrDataClass.yValue[j]) + " "); break;
case 5: richTextBox.AppendText(string.Format("{0:X4}", pack.GyrDataClass.zValue[j]) + " "); break;
case 6: richTextBox.AppendText(string.Format("{0:X4}", pack.MagDataClass.xValue[j]) + " "); break;
case 7: richTextBox.AppendText(string.Format("{0:X4}", pack.MagDataClass.yValue[j]) + " "); break;
case 8: richTextBox.AppendText(string.Format("{0:X4}", pack.MagDataClass.zValue[j]) + " "); break;
default: break;
}
}
}
richTextBox.AppendText("\r\n电量数据->");
richTextBox.AppendText(string.Format("{0:X4}", pack.BatDataClass.Type) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.BatDataClass.Length) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.BatDataClass.Value) + " ");
richTextBox.AppendText("\r\n错误信息->");
richTextBox.AppendText(string.Format("{0:X4}", pack.ErrDataClass.Type) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.ErrDataClass.Length) + " ");
richTextBox.AppendText(string.Format("{0:X4}", pack.ErrDataClass.Value) + "\r\n");
//设定光标所在位置
richTextBox.SelectionStart = richTextBox.TextLength;
//滚动到当前光标处
richTextBox.ScrollToCaret();
}
#endregion[/mw_shl_code] |
|