2013/07/17

C#.Net HRV(Heart rate variability) 8Z11

最近做夢突然想到,從廠商那邊提供的HRV Source code是C++/CLI的…
那作用不就在擴充.Net平台XDDD

那我就直接用.Net寫就好了啊,聽廠商講得好像很精華XD
不過我也學習到另一種C++的寫法,不過這似乎還沒有被ANSI C認可
畢竟是for .Net XDDD,或許有一些不為人知的用法XD

基本上這程式跟HRV Source code應該是沒有太大出路,如果看得懂HRV Source code應該就看得懂這邊
還打算在這隻程式上面加點料,後來因為一些特殊的問題存在,就暫時打住了





Designer.cs:
namespace HRV
{
    partial class HRVForm
    {
        /// <summary>
        /// 設計工具所需的變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清除任何使用中的資源。
        /// </summary>
        /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form 設計工具產生的程式碼

        /// <summary>
        /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器
        /// 修改這個方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btn_Start = new System.Windows.Forms.Button();
            this.btn_Stop = new System.Windows.Forms.Button();
            this.comboBox = new System.Windows.Forms.ComboBox();
            this.label_ComPort = new System.Windows.Forms.Label();
            this.label_FileName = new System.Windows.Forms.Label();
            this.textBox_FileName = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // btn_Start
            // 
            this.btn_Start.Location = new System.Drawing.Point(26, 72);
            this.btn_Start.Name = "btn_Start";
            this.btn_Start.Size = new System.Drawing.Size(75, 23);
            this.btn_Start.TabIndex = 0;
            this.btn_Start.Text = "Start";
            this.btn_Start.UseVisualStyleBackColor = true;
            this.btn_Start.Click += new System.EventHandler(this.btn_Start_Click);
            // 
            // btn_Stop
            // 
            this.btn_Stop.Location = new System.Drawing.Point(126, 72);
            this.btn_Stop.Name = "btn_Stop";
            this.btn_Stop.Size = new System.Drawing.Size(75, 23);
            this.btn_Stop.TabIndex = 1;
            this.btn_Stop.Text = "Stop";
            this.btn_Stop.UseVisualStyleBackColor = true;
            this.btn_Stop.Click += new System.EventHandler(this.btn_Stop_Click);
            // 
            // comboBox
            // 
            this.comboBox.FormattingEnabled = true;
            this.comboBox.Location = new System.Drawing.Point(80, 12);
            this.comboBox.Name = "comboBox";
            this.comboBox.Size = new System.Drawing.Size(121, 20);
            this.comboBox.TabIndex = 2;
            // 
            // label_ComPort
            // 
            this.label_ComPort.AutoSize = true;
            this.label_ComPort.Location = new System.Drawing.Point(24, 12);
            this.label_ComPort.Name = "label_ComPort";
            this.label_ComPort.Size = new System.Drawing.Size(50, 12);
            this.label_ComPort.TabIndex = 3;
            this.label_ComPort.Text = "Com Port";
            // 
            // label_FileName
            // 
            this.label_FileName.AutoSize = true;
            this.label_FileName.Location = new System.Drawing.Point(24, 44);
            this.label_FileName.Name = "label_FileName";
            this.label_FileName.Size = new System.Drawing.Size(52, 12);
            this.label_FileName.TabIndex = 4;
            this.label_FileName.Text = "File Name";
            // 
            // textBox_FileName
            // 
            this.textBox_FileName.Location = new System.Drawing.Point(80, 44);
            this.textBox_FileName.Name = "textBox_FileName";
            this.textBox_FileName.Size = new System.Drawing.Size(121, 22);
            this.textBox_FileName.TabIndex = 5;
            // 
            // HRVForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(230, 106);
            this.Controls.Add(this.textBox_FileName);
            this.Controls.Add(this.label_FileName);
            this.Controls.Add(this.label_ComPort);
            this.Controls.Add(this.comboBox);
            this.Controls.Add(this.btn_Stop);
            this.Controls.Add(this.btn_Start);
            this.Name = "HRVForm";
            this.Text = "HRV By C.Y. Fang";
            this.Load += new System.EventHandler(this.HRVForm_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btn_Start;
        private System.Windows.Forms.Button btn_Stop;
        private System.Windows.Forms.ComboBox comboBox;
        private System.Windows.Forms.Label label_ComPort;
        private System.Windows.Forms.Label label_FileName;
        private System.Windows.Forms.TextBox textBox_FileName;
    }
}





Code:
using System;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;

namespace HRV
{
    public partial class HRVForm : Form
    {
        private static SerialPort mSerial = new SerialPort();
        private static Thread mThread = null;
        private static StreamWriter mWriter = null;

        private static bool mContinue { set; get; }

        private static readonly Byte[] TryDetect = { 0x00, 0x00, 0x00 };
        private static readonly Byte[] setupRFDB = { 0x0b, 0x08, 0xff, 0x00, 0x00 };
        private static readonly Byte[] setupXMIT = { 0x07, 0x80, 0x07, 0x01, 0xff, 0xff, 0x00, 0x14, 0x21 };
        private static readonly Byte[] StpDetect = { 0x22, 0x22, 0x22 };

        public HRVForm()
        {
            InitializeComponent();
        }

        private void HRVForm_Load(object sender, EventArgs e)
        {
            this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            this.textBox_FileName.Text = "HRV.raw";

            if (SerialPort.GetPortNames().Length > 0)
            {
                foreach (String com in SerialPort.GetPortNames())
                {
                    comboBox.Items.Add(com);
                }

                comboBox.SelectedIndex = 0;

                mContinue = false;
            }
            else
            {
                MessageBox.Show("Bye");
                Application.Exit();
            }
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            if (IsSetFileName())
            {
                Init_mSerial();
                mSerial.Open();
                if (mSerial.IsOpen == true)
                {
                    MessageBox.Show("SerialPort opened successfully!");
                    mContinue = true;
                    Thread.Sleep(100);
                    mThread.Start();
                    mSerial.RtsEnable = true;
                    Thread.Sleep(1);
                    mSerial.RtsEnable = false;
                    Thread.Sleep(500);
                    mSerial.Write(TryDetect, 0, 1);
                    mSerial.Write(TryDetect, 0, 1);
                    mSerial.Write(setupRFDB, 0, setupRFDB.Length);
                    Thread.Sleep(10);
                    mSerial.Write(setupXMIT, 0, setupXMIT.Length);
                }
                else
                {
                    mContinue = false;
                    mSerial.Close();
                    mWriter.Close();
                    MessageBox.Show("Serialport open failed.");
                }
            }
            else
            {
                MessageBox.Show("Input file name!!");
                textBox_FileName.Focus();
            }
        }

        private void btn_Stop_Click(object sender, EventArgs e)
        {
            if (mThread.ThreadState == ThreadState.Running)
            {
                mContinue = false;

                mThread.Join();

                mSerial.Write(TryDetect, 0, 1);
                mSerial.Write(TryDetect, 0, 1);
                mSerial.Write(TryDetect, 0, 1);
                mSerial.Write(TryDetect, 0, 1);
                mSerial.Write(TryDetect, 0, 1);
                mSerial.Write(TryDetect, 0, 1);

                mWriter.Flush();
                mWriter.Close();
                mSerial.Close();

                MessageBox.Show("Thread join");
            }
        }

        private void Init_mSerial()
        {
            mSerial.PortName = comboBox.SelectedItem.ToString();
            mSerial.BaudRate = 250000;
            mSerial.Parity = Parity.None;
            mSerial.DataBits = 8;
            mSerial.StopBits = StopBits.One;
            mSerial.Handshake = Handshake.None;
            mSerial.ReadTimeout = 500;
            mSerial.WriteTimeout = 500;

            mThread = new Thread(new ThreadStart(Read));
        }

        private bool IsSetFileName()
        {
            if (textBox_FileName.Text.Length < 0)
            {
                return false;
            }
            else
            {
                mWriter = new StreamWriter(textBox_FileName.Text, true);
            }
            return true;
        }

        private void Read()
        {
            MessageBox.Show("Reading...");

            while (mContinue)
            {
                int count = mSerial.BytesToRead;

                if (count > 0)
                {
                    byte[] buffer = new byte[count];
                    mSerial.Read(buffer, 0, count);

                    foreach (byte b in buffer)
                    {
                        mWriter.Write(Convert.ToString(b, 16) + " ");
                    }
                    mWriter.WriteLine("");

                    mWriter.Flush();
                }
            }
        }
    }
}