//DialogResult dialogResult = MessageBox.Show("Save as: " + filename, "Save File", MessageBoxButtons.YesNo);
        //if (dialogResult == DialogResult.Yes)
        //{
        //    //do something
        //    string path = AppDomain.CurrentDomain.BaseDirectory + filename;
        //    using (StreamWriter sw = new StreamWriter(File.Create(path)))
        //    {
        //        try
        //        {
        //            sw.Write(sb.ToString());
        //        }
        //        catch (Exception ex)
        //        {
        //            MessageBox.Show(ex.Message);
        //        }
        //    }

        //}
        //else if (dialogResult == DialogResult.No)
        //{
        //    //You dont want to save data :(
        //}



        public static void SaveRaw(MeasurementHeaderClass head, int[] values, int ch)
        {
            StringBuilder sb = new StringBuilder();

            // Infer channel gain
            float chGain;

            if (ch == 0)
            {
                chGain = ConfigClass.deviceGainCH0;
            }
            else if (ch == 1)
            {
                chGain = ConfigClass.deviceGainCH1;
            }
            else
            {
                MessageBox.Show("Channel gain error");
                return;
            }


            foreach (var item in values)
            {
                sb.Append(SupercapHelperClass.ConvertToFloatInMiliVolts(item, chGain).ToString() + ",");
            }
            // Remove last ','
            sb.Remove(sb.Length - 1, 1);

            // Create file name

            string filename = "J00_" + SupercapHelperClass.ConvertChannelToSymbolicString(ch) + "_";

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter           = "CSV files |*.csv";
            saveFileDialog1.RestoreDirectory = true;     // Remember where we saved last file
            saveFileDialog1.FileName         = filename; // Default name

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter sw = new StreamWriter(File.Create(saveFileDialog1.FileName)))
                {
                    try
                    {
                        sw.Write(sb.ToString());
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Data is downloaded, extract raw values from data packet
        /// </summary>
        /// <param name="raw"></param>
        public FormMeasurementSingleChannelPresenter(byte[] raw)
        {
            InitializeComponent();



            ByteArrayDecoderClass dec = new ByteArrayDecoderClass(raw);
            // Extract data len without header
            int dataLen = (dec.Get2BytesAsInt() - ConfigClass.HEADER_LENGTH) / 2;

            // Remove header from data packet
            selectedHeader               = new MeasurementHeaderClass(0); // Dummy address
            selectedHeader.timestamp     = dec.Get6BytesAsTimestamp();
            selectedHeader.prescaler     = dec.Get4BytesAsInt();
            selectedHeader.numOfPoints   = dec.Get2BytesAsInt();
            selectedHeader.operatingMode = dec.Get1ByteAsInt();
            selectedHeader.channel       = dec.Get1ByteAsInt();

            // Set default gain in textbox from configuration
            if (selectedHeader.channel == 0)
            {
                textBoxGain.Text = ConfigClass.deviceGainCH0.ToString();
            }
            else if (selectedHeader.channel == 1)
            {
                textBoxGain.Text = ConfigClass.deviceGainCH1.ToString();
            }
            else
            {
                throw new Exception("Invalid channel value in FormMeasurementSingleChannelPresenter");
            }



            // Form timestamp string
            string time = (selectedHeader.timestamp[0] + 2000).ToString() + "/" + selectedHeader.timestamp[1].ToString() + "/" +
                          selectedHeader.timestamp[2].ToString() + " " +
                          selectedHeader.timestamp[3].ToString() + ":" + selectedHeader.timestamp[4].ToString() + ":" + selectedHeader.timestamp[5].ToString();
            string channel = SupercapHelperClass.ConvertChannelToSymbolicString(selectedHeader.channel);

            // Change name of this form
            this.Text = channel + "  " + time;


            values = new int[dataLen];
            //int value = dec.Get2BytesAsInt16();

            for (int i = 0; i < dataLen; i++)
            {
                values[i] = dec.Get2BytesAsInt16();
            }
        }
        public FormMeasurementDualChannelPresenter(byte[] raw)
        {
            InitializeComponent();
            ByteArrayDecoderClass dec = new ByteArrayDecoderClass(raw);
            // Extract data len without header (2 measurements are stored in raw thats why / 4)
            int dataLen = (dec.Get2BytesAsInt() - ConfigClass.HEADER_LENGTH) / 4;

            // Remove header from data packet
            selectedHeader               = new MeasurementHeaderClass(0); // Dummy address
            selectedHeader.timestamp     = dec.Get6BytesAsTimestamp();
            selectedHeader.prescaler     = dec.Get4BytesAsInt();
            selectedHeader.numOfPoints   = dec.Get2BytesAsInt();
            selectedHeader.operatingMode = dec.Get1ByteAsInt();
            selectedHeader.channel       = dec.Get1ByteAsInt();

            // Set gain textboxes default values from config class
            textBoxGainCH0.Text = ConfigClass.deviceGainCH0.ToString();
            textBoxGainCH1.Text = ConfigClass.deviceGainCH1.ToString();

            // Form timestamp string
            string time = (selectedHeader.timestamp[0] + 2000).ToString() + "/" + selectedHeader.timestamp[1].ToString() + "/" +
                          selectedHeader.timestamp[2].ToString() + " " + selectedHeader.timestamp[3].ToString() + ":" +
                          selectedHeader.timestamp[4].ToString() + ":" + selectedHeader.timestamp[5].ToString();
            string channel = SupercapHelperClass.ConvertChannelToSymbolicString(selectedHeader.channel);

            // Change name of this form
            this.Text = channel + "  " + time;


            valuesCH0 = new int[dataLen];
            valuesCH1 = new int[dataLen];

            for (int i = 0; i < dataLen; i++)
            {
                valuesCH0[i] = dec.Get2BytesAsInt16();
                valuesCH1[i] = dec.Get2BytesAsInt16();
            }
        }
        /// <summary>
        /// Save measurements in CSV file, named by dev address, date, channel etc... Gain from config file will be included
        /// </summary>
        /// <param name="head"></param>
        /// <param name="values"></param>
        /// <param name="ch"></param>
        public static void Save(MeasurementHeaderClass head, int[] values, int ch)
        {
            // Infer channel gain
            float chGain;

            if (ch == 0)
            {
                chGain = ConfigClass.deviceGainCH0;
            }
            else if (ch == 1)
            {
                chGain = ConfigClass.deviceGainCH1;
            }
            else
            {
                MessageBox.Show("Channel gain error");
                return;
            }

            // First convert to desired format
            StringBuilder sb = new StringBuilder();

            sb.Append("Device Addess: " + ConfigClass.deviceAddr + "\r\n");
            sb.Append("Channel: " + ch + "\r\n");
            sb.Append("Included gain value: " + chGain.ToString() + "\r\n");
            sb.Append("Prescaler: " + head.prescaler + "\r\n");
            sb.Append("Number of data points: " + head.numOfPoints + "\r\n");
            sb.Append("Start:\r\n");

            foreach (var item in values)
            {
                sb.Append(SupercapHelperClass.ConvertToFloatInMiliVolts(item, chGain).ToString() + ",");
            }
            // Remove last ','
            sb.Remove(sb.Length - 1, 1);

            // Create file name
            string time = (head.timestamp[0] + 2000).ToString() + "_" + head.timestamp[1].ToString() + "_" + head.timestamp[2].ToString() + "_" +
                          head.timestamp[3].ToString() + "_" + head.timestamp[4].ToString() + "_" + head.timestamp[5].ToString();
            string filename = "dev" + ConfigClass.deviceAddr + "_" + time + "_" + SupercapHelperClass.ConvertChannelToSymbolicString(ch);



            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter           = "CSV files |*.csv";
            saveFileDialog1.RestoreDirectory = true;     // Remember where we saved last file
            saveFileDialog1.FileName         = filename; // Default name

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //if ((myStream = saveFileDialog1.OpenFile()) != null)
                //{
                //    // Code to write the stream goes here.
                //    myStream.Close();
                //}
                using (StreamWriter sw = new StreamWriter(File.Create(saveFileDialog1.FileName)))
                {
                    try
                    {
                        sw.Write(sb.ToString());
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }