public static bool Download(List <Tuple <int, int> > adrList, Action <List <byte[]> > sucCb, Action fCb)
        {
            if (busy)
            {
                return(false); // Downloader is busy, abort request
            }
            busy          = true;
            finalDataList = new List <byte[]>();
            _adrList      = adrList;
            currentIndex  = 0;
            targetIndex   = adrList.Count;
            _sucCb        = sucCb;
            _fCb          = fCb;
            // Bootstrap
            CommandFormerClass cm = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);

            cm.AppendReadFromAddress(_adrList[currentIndex].Item1, _adrList[currentIndex].Item2);
            var data = cm.GetFinalCommandList();

            if (SerialDriver.Send(data, Fetcher, FailCallback))
            {
                currentIndex++;
                return(true);
            }
            else
            {
                // Serial driver is busy
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Message which contains header is received, extract header from it, populate DataGridView and isue a new header request if needed
        /// </summary>
        /// <param name="b"> Message which contains header </param>
        private void FetchHeaders(byte[] b)
        {
            ByteArrayDecoderClass decoder = new ByteArrayDecoderClass(b);

            decoder.Get2BytesAsInt(); // Remove first 2 values (number of data received)

            // Fill header with remaining data
            headers[currentHeader].timestamp     = decoder.Get6BytesAsTimestamp();
            headers[currentHeader].prescaler     = decoder.Get4BytesAsInt();
            headers[currentHeader].numOfPoints   = decoder.Get2BytesAsInt();
            headers[currentHeader].operatingMode = decoder.Get1ByteAsInt();
            headers[currentHeader].channel       = decoder.Get1ByteAsInt();

            PopulateDataGridWithHeader(headers[currentHeader], currentHeader);

            currentHeader++;
            if (currentHeader == headers.Count) // Last time in here, reset everything
            {
                //Console.WriteLine("Done fetching headers");
                return;
            }

            // Send command to get measurement header
            CommandFormerClass cm = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);

            cm.AppendReadFromAddress(headers[currentHeader].headerAddress, ConfigClass.HEADER_LENGTH);
            var data = cm.GetFinalCommandList();

            //Console.WriteLine("Fetching headers from Fetching");
            Thread.Sleep(10); // MCU cant handle very fast UART tasks
            SerialDriver.Send(data, FetchHeaders, FailCallback);
        }
        private void GetVoltage(int devId, int delayMs)
        {
            ConfigClass.UpdateWorkingDeviceAddress(devId);
            form.Text = "Charger Controller   DEV_ADDR=" + ConfigClass.deviceAddr.ToString() + "     GainCH0=" + ConfigClass.deviceGainCH0 + "  GainCH1=" + ConfigClass.deviceGainCH1;

            // Send command to get last ADC sample
            FormCustomConsole.WriteLineWithConsole("\r\n ------------------------");
            // form test sequence
            var com = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);

            com.GetLastADCSample(1);
            var data = com.GetFinalCommandList();

            try
            {
                Thread.Sleep(delayMs);
                if (forceStop)
                {
                    FormCustomConsole.WriteLineWithConsole("Multi sending aborted\r\n");
                    busy = false;
                    return;
                }
                FormCustomConsole.WriteLineWithConsole("\r\nSending commands to ID:" + devId + "\r\n");
                if (!SerialDriver.Send(data, SuccessCallback, FailCallback))
                {
                    Console.WriteLine("Serial Driver busy!!");
                    busy = false;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Problem occured while trying to send data to serial port!");
                FormCustomConsole.WriteLine("------- Commands not sent --------\r\n");
            }
        }
Ejemplo n.º 4
0
        private void dataGridViewDataDownloadMesHeaders_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // Check is it button and if it is get that header
            var senderGrid = (DataGridView)sender;

            // Check if button is clicked
            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                // First select selected header
                MeasurementHeaderClass header;
                try
                {
                    header = headers[e.RowIndex];
                }
                catch (Exception)
                {
                    MessageBox.Show("Header not present");
                    return;
                }

                //uartReceiver.Reset();
                //processFunction = ProcessReceivedMeasurements;

                // Send command to get measurement header + data
                CommandFormerClass cm = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);
                cm.AppendReadFromAddress(header.headerAddress, header.numOfPoints * 2 + ConfigClass.HEADER_LENGTH); // 1 data point = 2 bytes
                var data = cm.GetFinalCommandList();
                SerialDriver.Send(data, ProcessReceivedMeasurements, FailCallback);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Fetch all selected addresses and send command sequence to all of them with given delay in seconds
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void buttonCap1SendTestSeq_Click(object sender, EventArgs e)
        {
            int delay;

            // First parse float value
            try
            {
                delay  = int.Parse(textBoxCap1DebugDelay.Text);
                delay *= 1000; // Delay is in ms and user input is in seconds
            }
            catch (Exception)
            {
                MessageBox.Show("Insert valid float value!");
                return;
            }
            DataGridHelperClass.ClearStatusColorsFromDataGrid(dataGridViewCap1);

            list = DataGridHelperClass.GetSelectedIndexes(dataGridViewCap1);

            foreach (var index in list)
            {
                ConfigClass.UpdateWorkingDeviceAddress(index);
                this.Text = "Charger Controller   DEV_ADDR=" + ConfigClass.deviceAddr.ToString() + "     GainCH0=" + ConfigClass.deviceGainCH0 + "  GainCH1=" + ConfigClass.deviceGainCH1;

                FormCustomConsole.WriteLineWithConsole("\r\n ------------------------");
                // form test sequence
                com = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);
                AppendTestSequence();
                var data = com.GetFinalCommandList();
                labelDebugBytesUsed.Text = "Bytes Used : " + data.Length;
                try
                {
                    FormCustomConsole.WriteLineWithConsole("\r\nSending commands to ID:" + index + "\r\n");
                    SerialDriver.Send(data, Cap1ExecuteSuccessCallback, Cap1ExecuteFailCallback);
                }
                catch (Exception)
                {
                    MessageBox.Show("Problem occured while trying to send data to serial port!");
                    FormCustomConsole.WriteLine("------- Commands not sent --------\r\n");
                    return;
                }
                // Reset everything
                com = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);
                textBoxDebugInstructionPool.Text = "";
                // Wait delay async

                await Task.Delay(delay);
            }
            Thread t = new Thread(() =>
            {
                MessageBox.Show("******All commands sent!*****", DateTime.Now.ToString());
            });

            t.IsBackground = true;
            t.Start();
            FormCustomConsole.WriteLineWithConsole("\r\n ******All commands sent!*****");
        }
        private void buttonTakeSample_Click(object sender, EventArgs e)
        {
            // Send command to get last ADC sample
            CommandFormerClass cm = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);

            cm.GetLastADCSample(1);
            var data = cm.GetFinalCommandList();

            SerialDriver.Send(data, SuccessCallback, FailCallback);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Get current voltage from device (current ADC sample from CH1)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDataDownloadGetVoltage_Click(object sender, EventArgs e)
        {
            // Send command to get last ADC sample
            CommandFormerClass cm = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);

            cm.GetLastADCSample(1);
            var data = cm.GetFinalCommandList();

            SerialDriver.Send(data, GetADCResultCallbackCH1, FailCallback);
        }
Ejemplo n.º 8
0
        private void buttonDataDownloadErase_Click(object sender, EventArgs e)
        {
            // Send command to get measurement header
            CommandFormerClass cm = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);

            cm.AppendEraseMeasurements();
            cm.ReturnACK();
            var data = cm.GetFinalCommandList();

            SerialDriver.Send(data, SuccsessCallback, FailCallback);
        }
Ejemplo n.º 9
0
        private void buttonDataDownloadGetMeasures_Click(object sender, EventArgs e)
        {
            // Send command to get measurement header
            CommandFormerClass cm = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);

            cm.AppendReadFromAddress(ConfigClass.MEASURE_INFO_BASE, ConfigClass.MEASURE_INFO_LEN);
            var data = cm.GetFinalCommandList();

            //Console.WriteLine("ProcessGetMeasurementInfo from buttonDataDownloadGetMeasures_Click");
            SerialDriver.Send(data, ProcessGetMeasurementInfo, FailCallback);
        }
        private static void Fetcher(byte[] b)
        {
            finalDataList.Add(b); // First add received data to list (we had bootstrap)
            if (currentIndex >= targetIndex)
            {
                // All went well, call successful callback with results
                _sucCb(finalDataList);
                busy = false;
                return;
            }
            Thread.Sleep(10); // Device cant manage requests so fast
            // Send command to get measurement header + data
            CommandFormerClass cm = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);

            cm.AppendReadFromAddress(_adrList[currentIndex].Item1, _adrList[currentIndex].Item2);
            var data = cm.GetFinalCommandList();

            SerialDriver.Send(data, Fetcher, FailCallback);
            currentIndex++;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Measurement info is received, decode data and send requests to read measurement headers
        /// </summary>
        /// <param name="b"></param>
        private void ProcessGetMeasurementInfo(byte[] b)
        {
            ByteArrayDecoderClass decoder = new ByteArrayDecoderClass(b);

            decoder.Get2BytesAsInt(); // Ignore first 2 values (message length)
            this.Invoke(new Action(() =>
            {
                dataGridViewDataDownloadMesHeaders.DataSource = null;
                dataGridViewDataDownloadMesHeaders.Rows.Clear();
                dataGridViewDataDownloadMesHeaders.Refresh();
            }));

            // Get number of measurements and their header starting addresses
            int numOfMeasurements = decoder.Get2BytesAsInt();

            FormCustomConsole.WriteLine("Number of measurements: " + numOfMeasurements);
            Console.WriteLine("Number of measurements: " + numOfMeasurements);
            if (numOfMeasurements == 0)
            {
                // if no measurements leave it as it is
                MessageBox.Show("No saved measurements in device");
                return;
            }
            headers = new List <MeasurementHeaderClass>(); // Reset headers list
            // For now only header addresses are received, which can be used to fetch headers
            for (int i = 0; i < numOfMeasurements; i++)
            {
                headers.Add(new MeasurementHeaderClass(decoder.Get4BytesAsInt()));
            }
            currentHeader = 0; // Reset header index

            // Bootstrap
            // Send command to get measurement header
            CommandFormerClass cm = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);

            cm.AppendReadFromAddress(headers[0].headerAddress, ConfigClass.HEADER_LENGTH);
            var data = cm.GetFinalCommandList();

            //Console.WriteLine("Fetching headers from ProcessGetMeasurementInfo");
            SerialDriver.Send(data, FetchHeaders, FailCallback);
        }
Ejemplo n.º 12
0
        private void buttonDebugExecute_Click(object sender, EventArgs e)
        {
            var data = com.GetFinalCommandList();

            labelDebugBytesUsed.Text = "Bytes Used : " + data.Length;
            try
            {
                SerialDriver.Send(data, DebugExecuteSuccessCallback, DebugExecuteFailCallback);
            }
            catch (Exception)
            {
                MessageBox.Show("Problem occured while trying to send data to serial port!");
                FormCustomConsole.WriteLine("------- Commands not sent --------\r\n");
                return;
            }
            labelExecuteStatus.Text      = "Pending";
            labelExecuteStatus.ForeColor = System.Drawing.Color.Black;
            // Reset everything
            com = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);
            textBoxDebugInstructionPool.Text = "";
            FormCustomConsole.WriteLine("------- Commands sent --------\r\n");
        }
        private void buttonCalib100A_Click(object sender, EventArgs e)
        {
            // Send commands for calibration
            CommandFormerClass cm = new CommandFormerClass(ConfigClass.startSeq, ConfigClass.deviceAddr);

            cm.ReturnACK();
            cm.AppendDischarger100AOn();
            cm.AppendWaitForMs(2000);
            cm.AppendDataRecorderTask(0, 2, 0, 150, DateTime.Now.AddSeconds(2)); // Take into consideration 2 sec wait above
            cm.AppendWaitForMs(1000);
            cm.AppendDischarger100AOffS1();
            cm.AppendWaitForMs(200);
            cm.AppendDischarger100AOffS2();
            var data = cm.GetFinalCommandList();

            SerialDriver.Send(data, SuccessCallbackCurrent100A, FailCallback);

            // Now enable GW INSTEC recording

            Point pt = new Point(Convert.ToInt32(dataGridView1.Rows[0].Cells[0].Value), Convert.ToInt32(dataGridView1.Rows[0].Cells[1].Value));

            Cursor.Position = pt;
            mouse_event(MOUSEEVENTF_LEFTDOWN, pt.X, pt.Y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, pt.X, pt.Y, 0, 0);
            Thread.Sleep(100);
            // Configrm recording
            pt = new Point(Convert.ToInt32(dataGridView1.Rows[1].Cells[0].Value), Convert.ToInt32(dataGridView1.Rows[1].Cells[1].Value));
            Cursor.Position = pt;
            mouse_event(MOUSEEVENTF_LEFTDOWN, pt.X, pt.Y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, pt.X, pt.Y, 0, 0);
            Thread.Sleep(2900);
            // Disable measures
            pt = new Point(Convert.ToInt32(dataGridView1.Rows[0].Cells[0].Value), Convert.ToInt32(dataGridView1.Rows[0].Cells[1].Value));
            Cursor.Position = pt;
            mouse_event(MOUSEEVENTF_LEFTDOWN, pt.X, pt.Y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, pt.X, pt.Y, 0, 0);
        }
Ejemplo n.º 14
0
 private void buttonDataDownloadSerialDisconnect_Click(object sender, EventArgs e)
 {
     SerialDriver.Close();
     labelDataDownloadSerialConnectionStatus.Text = "Not connected!";
 }
Ejemplo n.º 15
0
 private void buttonDataDownloadSerialConnect_Click(object sender, EventArgs e)
 {
     labelDataDownloadSerialConnectionStatus.Text = SerialDriver.Open((string)comboBoxDataDownloadSerial.SelectedItem);
 }