/* * /// <summary> * /// ReadAsync: Task that waits on data and reads asynchronously from the serial device InputStream * /// </summary> * /// <param name="cancellationToken">CancellationTokenSource ReadCancellationTokenSource, for determining if read is allowed or has been cancelled</param> * /// <returns>string received from connected serial device</returns> * private async Task<string> ReadAsync(CancellationToken cancellationToken) * { * Task<UInt32> loadAsyncTask; * * uint ReadBufferLength = 1024; * * // If task cancellation was requested, comply * cancellationToken.ThrowIfCancellationRequested(); * * // Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available * dataReaderObject.InputStreamOptions = InputStreamOptions.Partial; * * using (var childCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken)) * { * // Create a task object to wait for data on the serialPort.InputStream * loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(childCancellationTokenSource.Token); * * // Launch the task and wait * UInt32 bytesRead = await loadAsyncTask; * if (bytesRead > 0) * { * return dataReaderObject.ReadString(bytesRead); * //status.Text = "bytes read successfully!"; * } * } * * return null; * } */ /// <summary> /// - Creates an async task that performs the write operation to the serial device output stream /// </summary> /// <param name="text">string to send serially</param> public async Task SendText(string text) { try { if (serialPort != null) { // Create the DataWriter object and attach to OutputStream dataWriteObject = new DataWriter(serialPort.OutputStream); //Launch the WriteAsync task to perform the write await ss.WriteAsync(dataWriteObject, text); //await WriteAsync(text); } else { //status.Text = "Select a device and connect"; } } catch (Exception ex) { //status.Text = "SendTextk: " + ex.Message; } finally { // Cleanup once complete if (dataWriteObject != null) { dataWriteObject.DetachStream(); //disconnect from output stream dataWriteObject = null; } } }