使用Neurosky设备记录特定命令的数据

时间:2015-06-09 04:12:16

标签: c#

我试图使用Neurosky设备记录原始数据,我设法做到了。但我需要编写一个命令(例如移动右臂)然后记录该命令的数据,但问题是OnDataReceived事件一直被触发,我不知道在哪里可以添加一些代码记录数据前的命令。有帮助吗? 我的代码:

using System;

using System.Collections.Generic;
using System.Text;
using System.Threading;

using System.IO;
using System.IO.Ports;

using NeuroSky.ThinkGear;
using NeuroSky.ThinkGear.Algorithms;
using System.Collections;

namespace testprogram
{
    class Program
    {

        static Connector connector;
        static HeartRateAcceleration heartRateAcceleration;
        static HeartRateRecovery heartRateRecovery;

        static byte poorSig;
        static int train;

        public static void Main(string[] args)
        {

            Console.WriteLine("Hello EEG!");

            // Initialize a new Connector and add event handlers
            connector = new Connector();
            connector.DeviceConnected += new EventHandler(OnDeviceConnected);
            connector.DeviceConnectFail += new EventHandler(OnDeviceFail);
            connector.DeviceValidating += new EventHandler(OnDeviceValidating);

            Connector.EKGPersonalizationEvent += new EventHandler(OnEKGPersonalizationEvent);

            heartRateRecovery = new HeartRateRecovery();
            heartRateAcceleration = new HeartRateAcceleration();

            // Scan for devices
            connector.ConnectScan("COM40");

            // Enable Energy level calculation
            // connector.StartEnergyLevel();
            //connector.GetHeartAge(90, "Neraj");

            connector.EKGstartLongTraining("Neraj");

            Thread.Sleep(450000);

            //Console.WriteLine(connector.GetHeartRiskAware("Neraj"));
            //Console.WriteLine(connector.GetHeartAgeStatus("Neraj"));
            //Console.WriteLine(connector.ResetHeartAge("Neraj"));

            System.Console.WriteLine("Goodbye.");

            // Close all open connections
            connector.Close();
            Environment.Exit(0);
        }

        static void OnEKGPersonalizationEvent(object sender, EventArgs e)
        {

            EKGPersonalizationEventArgs ekgArgs = (EKGPersonalizationEventArgs)(e);
            int status = ekgArgs.statusMessage;

            switch (status)
            {
                case 268:
                    string data = (string)(ekgArgs.dataMessage);
                    Console.WriteLine("Handler Message: status = MSG_EKG_IDENTIFIED " + " and username = " + data);
                    break;

                case 269:
                    Console.WriteLine("Handler Message: status = MSG_EKG_TRAINED");

                    connector.EKGstartDetection();
                    break;

                case 270:
                    int trainStep = (int)ekgArgs.dataMessage;
                    Console.WriteLine("Handler Message: status = MSG_EKG_TRAIN_STEP " + " and training step = " + trainStep);
                    break;

                case 271:
                    Console.WriteLine("Handler Message: status = MSG_EKG_TRAIN_TOUCH");
                    break;
            }
        }


        /**
         * Called when a device is connected 
         */
        static void OnDeviceConnected(object sender, EventArgs e)
        {
            Connector.DeviceEventArgs de = (Connector.DeviceEventArgs)e;

            Console.WriteLine("Device found on: " + de.Device.PortName);

            de.Device.DataReceived += new EventHandler(OnDataReceived);
        }

        /**
         * Called when scanning fails
         */
        static void OnDeviceFail(object sender, EventArgs e)
        {
            Console.WriteLine("No devices found! :(");
        }

        /**
         * Called when each port is being validated
         */
        static void OnDeviceValidating(object sender, EventArgs e)
        {
            Console.WriteLine("Validating: ");

            heartRateRecovery.enableHeartRateRecovery();
        }

        /**
         * Called when data is received from a device
         */
        static void OnDataReceived(object sender, EventArgs e)
        {
            //Device d = (Device)sender;
            Device.DataEventArgs de = (Device.DataEventArgs)e;
            DataRow[] tempDataRowArray = de.DataRowArray;

            TGParser tgParser = new TGParser();
            tgParser.Read(de.DataRowArray);

            /* Loop through new parsed data */
            ArrayList data = new ArrayList();
            for (int i = 0; i < tgParser.ParsedData.Length; i++)
            {
                if (tgParser.ParsedData[i].ContainsKey("Raw"))
                {
                    Console.WriteLine("Raw Value:" + tgParser.ParsedData[i]["Raw"]);
                    data.Add(tgParser.ParsedData[i]["Raw"]);
                }
            }
            AnalyizeData(data);
        }

        private static void AnalyizeData(ArrayList data)
        {

        }
    }
}

0 个答案:

没有答案