public MITesSamplingRateForm(MITesDataFilterer aMITesDataFilterer, SensorAnnotation sensorConfiguration)
        {
            InitializeComponent();            
            this.aMITesDataFilterer = aMITesDataFilterer;
            this.sensorConfiguration = sensorConfiguration;
            InitializeInterface();

        }
        //private MITesDataCollectionForm thisForm = null;

        private bool InitializeMITes(string dataDirectory)
        {

           // thisForm = this;
            SetFormPositions();
            
            //depending on the number of receivers initialize mites objects
            int maxPortSearched = 1;
            for (int i = 0; (i < this.sensors.TotalReceivers); i++)
            {
                progressMessage += "Searching for receiver " +i +"...\r\n";
                this.mitesControllers[i] = new MITesReceiverController(MITesReceiverController.FIND_PORT, BYTES_BUFFER_SIZE);
                int portNumber = MITesReceiverController.FIND_PORT;

//#if (PocketPC)


                try
                {
                    for (int j = maxPortSearched; (j < Constants.MAX_PORT); j++)
                    {
                        portNumber = maxPortSearched = j;                        
                        progressMessage += "Testing COM Port " + portNumber;
                        if (this.mitesControllers[i].TestPort(portNumber, BYTES_BUFFER_SIZE))
                        {
                            progressMessage += "... Success\r\n";
                            break;
                        }
                        else
                            progressMessage += "... Failed\r\n";
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Exiting: Could not find a valid COM port with a MITes receiver!");
#if (PocketPC)
                    Application.Exit();
#else
                    Environment.Exit(0);
#endif
                }

//#else
                //string[] portNames = SerialPort.GetPortNames();
                //Regex comregex = new Regex("COM([0-9]+)");
                //for (int j = 0; (j < portNames.Length); j++)
                //{
                //    Match m = comregex.Match(portNames[j]);
                //    if (m.Success)
                //    {
                //        portNumber = Convert.ToInt32(m.Groups[1].Value);
                //        progressMessage += "Testing COM Port " + portNumber;
                //        if (this.mitesControllers[i].TestPort(portNumber, BYTES_BUFFER_SIZE))
                //        {
                //            progressMessage += "... Success\r\n";
                //            break;
                //        }
                //        else
                //        {
                //            progressMessage += "... Failed\r\n";
                //            portNumber = MITesReceiverController.FIND_PORT;
                //        }

                //    }
                //}
//#endif


                if (portNumber == MITesReceiverController.FIND_PORT)
                {
                    progressMessage += "Could not find a valid COM port with a MITes receiver!";
                    MessageBox.Show("Exiting: Could not find a valid COM port with a MITes receiver!");
#if (PocketPC)
                    Application.Exit();
#else
                    Environment.Exit(0);
#endif
                    return false;
                }
                this.mitesControllers[i].InitializeController(portNumber, BYTES_BUFFER_SIZE, true, MITesReceiverController.USE_THREADS);
                this.mitesDecoders[i] = new MITesDecoder();
            }

            aMITesActivityLogger = new MITesActivityLogger(dataDirectory + "\\data\\activity\\MITesActivityData");
            aMITesActivityLogger.SetupDirectories(dataDirectory);

            //aMITesDecoder = new MITesDecoder();
            aMITesPlotter = new MITesScalablePlotter(this.panel1, MITesScalablePlotter.DeviceTypes.IPAQ, maxPlots,  this.mitesDecoders[0], GetGraphSize(false));

            //for each sensor created a counter
            for (int i = 0; (i < this.sensors.Sensors.Count); i++)
            {
                int sensor_id=Convert.ToInt32(((SXML.Sensor)this.sensors.Sensors[i]).ID);
                if (sensor_id!=0)
                    aMITesActivityCounters.Add(sensor_id,new MITesActivityCounter(this.mitesDecoders[0],sensor_id));
            }
            aMITesHRAnalyzer = new MITesHRAnalyzer(this.mitesDecoders[0]);
            aMITesDataFilterer = new MITesDataFilterer(this.mitesDecoders[0]);
            aMITesLogger = new MITesLoggerNew(this.mitesDecoders[0],
                dataDirectory + "\\data\\raw\\MITesAccelBytes",
                dataDirectory + "\\data\\log\\MITesLogFileBytes");
            //aMITesLogger.SetActive(false);


            aMITesActivityLogger.WriteLogComment("Application started with command line: " +
                 dataDirectory + " " +
                 Constants.ACCEL_ID1 + " " +
                 Constants.ACCEL_ID2 + " " +
                 Constants.ACCEL_ID3 + " " );//+
                //Constants.ACCEL_ID4 + " " +
                //Constants.ACCEL_ID5 + " " +
                //Constants.ACCEL_ID6 + " ");

           

 
            // Set the correct channels based on sannotation automatically
            for (int i = 0; (i < this.sensors.TotalReceivers); i++)
            {
                int[] channels = new int[6];
                int channelCounter = 0;
                for (int j = 0; (j < this.sensors.Sensors.Count); j++)
                {
                    if (Convert.ToInt32(((Sensor)this.sensors.Sensors[j]).Receiver) == i)
                    {
                        channels[channelCounter] = Convert.ToInt32(((Sensor)this.sensors.Sensors[j]).ID);
                        channelCounter++;
                    }
                }
                this.mitesControllers[i].SetChannels(this.sensors.GetNumberSensors(i), channels);
            }
            //isPlotting = true;
            return true;
        }
        //Initialize MITes Receivers
        
        private bool InitializeMITes(string dataDirectory)
        {

                //for (int i = 0; (i < this.sensors.TotalReceivers); i++)
                foreach(Receiver receiver in this.sensors.Receivers)
                {
#if (PocketPC)
                    if (receiver.Type == SXML.Constants.RECEIVER_BLUETOOTH)
                    {
                        progressMessage += "Initializing Bluetooth ...";
                        this.bluetoothControllers[receiver.ID] = new BluetoothController();                     
                        try
                        {
                            //this.bluetoothControllers[i].initialize(this.configuration.MacAddress, this.configuration.Passkey);
                            this.bluetoothControllers[receiver.ID].initialize(receiver.MacAddress, receiver.PassKey);
                        }
                        catch (Exception e)
                        {
                            progressMessage += "Could not find a valid Bluetooth Wockets receiver!";
                            return false;
                        }
                        receiver.Running = true;

                        if (receiver.Decoder==SXML.Constants.DECODER_MITES)
                            this.mitesDecoders[receiver.ID] = new MITesDecoder();
                        else if (receiver.Decoder==SXML.Constants.DECODER_WOCKETS)
                            this.mitesDecoders[receiver.ID]= new MITesDecoder();
                        else if (receiver.Decoder == SXML.Constants.DECODER_SPARKFUN)
                            this.mitesDecoders[receiver.ID] = new MITesDecoder();
                    }
                    else 
#endif                        
                    if (receiver.Type == SXML.Constants.RECEIVER_USB)
                    {

                        progressMessage += "Searching for receiver " + receiver.ID + "...\r\n";
                        this.mitesControllers[receiver.ID] = new MITesReceiverController(MITesReceiverController.FIND_PORT, BYTES_BUFFER_SIZE);
                        int portNumber = MITesReceiverController.FIND_PORT;

                        //#if (PocketPC)


                        try
                        {
#if (PocketPC)
                            portNumber = 9;
                            progressMessage += "Testing COM Port " + portNumber;
                            if (this.mitesControllers[receiver.ID].TestPort(portNumber, BYTES_BUFFER_SIZE))
                            {
                                progressMessage += "... Success\r\n";
                            }
                            else
                            {
                                progressMessage += "... Failed\r\n";
                                portNumber = MITesReceiverController.FIND_PORT;
                            }
#else
                    for (int j = maxPortSearched; (j < Constants.MAX_PORT); j++)
                    {
                        portNumber = maxPortSearched = j;
                        progressMessage += "Testing COM Port " + portNumber;
                        if (this.mitesControllers[receiver.ID].TestPort(portNumber, BYTES_BUFFER_SIZE))
                        {
                            progressMessage += "... Success\r\n";
                            break;
                        }
                        else
                            progressMessage += "... Failed\r\n";
                    }
#endif
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Exiting: Could not find a valid COM port with a MITes receiver!");
                            for (int j = 0; (j < this.sensors.TotalReceivers); j++)
                                this.mitesControllers[j].Close();
#if (PocketPC)
                            Application.Exit();
                            System.Diagnostics.Process.GetCurrentProcess().Kill();
#else
                    Environment.Exit(0);
#endif
                        }


                        if (portNumber == MITesReceiverController.FIND_PORT)
                        {
                            progressMessage += "Could not find a valid COM port with a MITes receiver!";
                            //MessageBox.Show("Exiting: Could not find a valid COM port with a MITes receiver!");
#if (PocketPC)
                            //Application.Exit();
                            //System.Diagnostics.Process.GetCurrentProcess().Kill();    
#else
                    //Environment.Exit(0);
#endif
                            return false;
                        }
                        this.mitesControllers[receiver.ID].InitializeController(portNumber, BYTES_BUFFER_SIZE, true, MITesReceiverController.USE_THREADS);

                        this.mitesDecoders[receiver.ID] = new MITesDecoder();

                    }
                }
           // }

            aMITesActivityLogger = new MITesActivityLogger(dataDirectory + "\\data\\activity\\MITesActivityData");
            aMITesActivityLogger.SetupDirectories(dataDirectory);
            aMITesActivityCounters = new Hashtable();


            //aMITesPlotter = new MITesScalablePlotter(this.panel1, MITesScalablePlotter.DeviceTypes.IPAQ, maxPlots, this.mitesDecoders[0], GetGraphSize(false));

            //for each sensor created a counter
            for (int i = 0; (i < this.sensors.Sensors.Count); i++)
            {
                int sensor_id = Convert.ToInt32(((SXML.Sensor)this.sensors.Sensors[i]).ID);
                if (sensor_id != 0)
                    aMITesActivityCounters.Add(sensor_id, new MITesActivityCounter(this.mitesDecoders[0], sensor_id));
                    //aMITesActivityCounters.Add(sensor_id, new MITesActivityCounter(this.masterDecoder, sensor_id));
            }
            aMITesHRAnalyzer = new MITesHRAnalyzer(this.masterDecoder);//this.mitesDecoders[0]);
            aMITesDataFilterer = new MITesDataFilterer(this.masterDecoder);//this.mitesDecoders[0]);
            aMITesLoggerPLFormat = new MITesLoggerPLFormat(this.masterDecoder,//this.mitesDecoders[0],
                                                         dataDirectory + "\\data\\raw\\PLFormat\\");
            //aMITesLogger.SetActive(false);


            aMITesActivityLogger.WriteLogComment("Application started with command line: " +
                 dataDirectory + " " +
                 Constants.ACCEL_ID1 + " " +
                 Constants.ACCEL_ID2 + " " +
                 Constants.ACCEL_ID3 + " ");//+
            //Constants.ACCEL_ID4 + " " +
            //Constants.ACCEL_ID5 + " " +
            //Constants.ACCEL_ID6 + " ");

            // Set the correct channels based on sannotation automatically
            for (int i = 0; (i < this.sensors.TotalReceivers); i++)
            {
                int[] channels = new int[6];
                int channelCounter = 0;
                for (int j = 0; (j < this.sensors.Sensors.Count); j++)
                {
                    if (Convert.ToInt32(((Sensor)this.sensors.Sensors[j]).Receiver) == i)
                    {
                        int channelID=Convert.ToInt32(((Sensor)this.sensors.Sensors[j]).ID);
#if(PocketPC)
                        if (channelID != PhoneAccelerometers.Constants.BUILT_IN_ACCELEROMETER_CHANNEL_ID)
                        {
#endif
                            channels[channelCounter] = Convert.ToInt32(((Sensor)this.sensors.Sensors[j]).ID);
                            channelCounter++;
#if (PocketPC)
                        }
#endif
                    }
                }
                //Need to do the same thing for the Bluetooth
                if (this.configuration.Connection == MITesFeatures.core.conf.Constants.SOFTWARE_CONNECTION_USB)
                {
                    this.mitesControllers[i].SetChannels(this.sensors.GetNumberSensors(i), channels);
                }
            }

            return true;
        }
 // This method stores a reference to the data filterer for the MITes receiver to
 // individual sensor performance metrics
 public void SetDataFilterer(MITesDataFilterer aMITesDataFilterer)
 {
     this.aMITesDataFilterer = aMITesDataFilterer;
 }
        //Initializes objects that count frames for GUI reporting and quality control
        private void InitializeQuality()
        {
            aMITesActivityCounters = new Hashtable();

            //for each sensor created a counter
            for (int i = 0; (i < this.sensors.Sensors.Count); i++)
            {
                int sensor_id = Convert.ToInt32(((SXML.Sensor)this.sensors.Sensors[i]).ID);
                if (sensor_id != 0)
                    aMITesActivityCounters.Add(sensor_id, new MITesActivityCounter(this.masterDecoder, sensor_id));
            }
            aMITesHRAnalyzer = new MITesHRAnalyzer(this.masterDecoder);
            aMITesDataFilterer = new MITesDataFilterer(this.masterDecoder);

            for (int i = 0; i < MITesData.MAX_MITES_CHANNELS; i++)
                MITesDataFilterer.MITesPerformanceTracker[i] = new MITesPerformanceStats(0);
            //based on how many receivers and to what channels they are listening adjust the good sampling rate
            foreach (Sensor sensor in this.sensors.Sensors)
            {
                int sensor_id = Convert.ToInt32(sensor.ID);
                int receiver_id = Convert.ToInt32(sensor.Receiver);
                if (sensor_id == 0) //HR sensor
                {
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].GoodRate = (int)(Constants.HR_SAMPLING_RATE * 0.8);
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].PerfectRate = Constants.HR_SAMPLING_RATE;
                }
                else if (sensor_id == MITesDecoder.MAX_CHANNEL)
                {
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].GoodRate = (int)(sensor.SamplingRate * (1 - Extractor.Configuration.MaximumNonconsecutiveFrameLoss));
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].PerfectRate = sensor.SamplingRate;
                }
                else
                {
                    int goodSamplingRate = (int)((Extractor.Configuration.ExpectedSamplingRate * (1 - Extractor.Configuration.MaximumNonconsecutiveFrameLoss)) / this.sensors.NumberSensors[receiver_id]);
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].GoodRate = goodSamplingRate;
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].PerfectRate = (int)((Extractor.Configuration.ExpectedSamplingRate) / this.sensors.NumberSensors[receiver_id]);
                }
            }
        }