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