MainPage.xaml.cs
/// <summary>
/// ReadAsync: Task that waits on data and reads asynchronously from the serial device InputStream
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task that waits on data and reads asynchronously from the serial device InputStream
Task that waits on data and reads asynchronously from the serial device InputStream
private async Task ReadAsync(CancellationToken cancellationToken)
        {
            Task<UInt32> loadAsyncTask;
            uint ReadBufferLength = 1;
            cancellationToken.ThrowIfCancellationRequested();
            dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;
            loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
            UInt32 bytesRead = await loadAsyncTask;
            string sTemp;
            if (bytesRead > 0)
            {
                sTemp = dataReaderObject.ReadString(bytesRead);
                strDataFromSerialPort += sTemp;
                try
                {
                    processDataToDrawTrajactory();
                }
                catch (Exception ex)
                {
                    errorFrame += 1;
                    tblock_Current_Timer.Text = "frame error: " + strDataFromSerialPort + "Error: " + ex.Message + "No Error: " + errorFrame.ToString();
                }
                SaveTotxt(sTemp);
            }
        }
private async Task ReadAsync ( CancellationToken cancellationToken ) { Task < UInt32 > loadAsyncTask ; uint ReadBufferLength = 1 ; cancellationToken . ThrowIfCancellationRequested ( ) ; dataReaderObject . InputStreamOptions = InputStreamOptions . Partial ; loadAsyncTask = dataReaderObject . LoadAsync ( ReadBufferLength ) . AsTask ( cancellationToken ) ; UInt32 bytesRead = await loadAsyncTask ; string sTemp ; if ( bytesRead > 0 ) { sTemp = dataReaderObject . ReadString ( bytesRead ) ; strDataFromSerialPort += sTemp ; try { processDataToDrawTrajactory ( ) ; } catch ( Exception ex ) { errorFrame += 1 ; tblock_Current_Timer . Text = " " + strDataFromSerialPort + " " + ex . Message + " " + errorFrame . ToString ( ) ; } } }
ReadAsync: Task that waits on data and reads asynchronously from the serial device InputStream
ReadAsync : Task that waits on data and reads asynchronously from the serial device InputStream
//savedatatoTxtFile để lưu data --> .txt //nếu bộ đệm lớn sẽ có thời gian trễ lớn, nhưng dữ liệu không mất //2000 char // If task cancellation was requested, comply // Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available // Create a task object to wait for data on the serialPort.InputStream //save --> .txt //savedatatoTxtFile = dataReaderObject.LoadAsync(2000).AsTask(cancellationToken); // Launch the task and wait //save //UInt32 bytesSave = await savedatatoTxtFile; //if (bytesSave > 0) //{ //    rcvdText.Text = dataReaderObject.ReadString(bytesRead); //    strDataFromSerialPort += rcvdText.Text; //    //dataReaderObject.r //    //status.Text = "bytes read successfully!"; //    //Save data to .txt file //    SaveTotxt(rcvdText.Text); //} //Process and Save //dung để process and save //rcvdText.Text = dataReaderObject.ReadString(bytesRead); //dataReaderObject.r //status.Text = "bytes read successfully!"; ////Save data to .txt file //bufferSavedata += rcvdText.Text; //if ((strDataFromSerialPort != " ") && (strDataFromSerialPort != "")) //ProcessData(); // //SaveTotxt(sTemp);
None
cancellationToken
CancellationToken
None