//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);
                    }
                }
            }
        }
Пример #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();
            }
        }
Пример #3
0
        /// <summary>
        /// Populate DataGridView which is used for storing headers with current received header
        /// </summary>
        /// <param name="head"></param>
        /// <param name="currentHeader"></param>
        private void PopulateDataGridWithHeader(MeasurementHeaderClass head, int currentHeader)
        {
            // Form timestamp string first
            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();


            this.Invoke(new Action(() =>
            {
                dataGridViewDataDownloadMesHeaders.Rows.Add(head.headerAddress.ToString(), time, head.prescaler.ToString(), head.numOfPoints.ToString(),
                                                            head.operatingMode.ToString(), head.channel.ToString(), "FETCH");
            }));
        }
        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);
                    }
                }
            }
        }
Пример #6
0
        private void MultiSuccess35A(List <byte[]> list)
        {
            Invoke((MethodInvoker) delegate
            {
                labelMultiDownload.Text      = "Success!";
                labelMultiDownload.ForeColor = System.Drawing.Color.Green;
                const string delimiter       = "\r\n";
                Console.WriteLine("Success from MULTI");
                //string path = AppDomain.CurrentDomain.BaseDirectory;
                string filenameCH0;
                string filenameCH1;
                string midleFix = textBoxDataDownloadCapacitorDescription35A.Text;
                int[] valuesCH0, valuesCH1;
                MeasurementHeaderClass selectedHeader;
                string[] postFixes = { "_R", "_C" };
                int postFixIndex   = 0;


                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.SelectedPath        = defaultPath;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    defaultPath = dialog.SelectedPath + "\\";
                }
                else
                {
                    // If no folder is selected just abort everything :(
                    labelMultiDownload.Text      = "Canceled!";
                    labelMultiDownload.ForeColor = System.Drawing.Color.Black;
                    return;
                }


                foreach (var item in list)
                {
                    ByteArrayDecoderClass dec = new ByteArrayDecoderClass(item);
                    // 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();

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

                    for (int i = 0; i < dataLen; i++)
                    {
                        valuesCH0[i] = dec.Get2BytesAsInt16();
                        valuesCH1[i] = dec.Get2BytesAsInt16();
                    }
                    // Values are extracted so far, now form filename

                    // 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);

                    filenameCH0 = "dev" + ConfigClass.deviceAddr + "_" + midleFix + "_" + time + "_" + "CH0" + postFixes[postFixIndex];
                    filenameCH1 = "dev" + ConfigClass.deviceAddr + "_" + midleFix + "_" + time + "_" + "CH1" + postFixes[postFixIndex];
                    SaveMeasurementsToCSVClass.Save(valuesCH0, 0, defaultPath + filenameCH0 + ".csv", delimiter);
                    SaveMeasurementsToCSVClass.Save(valuesCH1, 1, defaultPath + filenameCH1 + ".csv", delimiter);
                    postFixIndex++;
                }
            }
                   );

            Console.WriteLine("All Done!!!!");
        }