src/UWP.Samples.AsyncResponseFromSerialDevice/PackageManager.cs
/// <summary>
/// Listens the serial port for input packages.
/// </summary>
Listens the serial port for input packages.
Listens the serial port for input packages .
private async void Listen()
        {
            var packageBytes = new List<byte>();
            while (true)
            {                 
                await this.dataReader.LoadAsync(1);
                byte currentByte = this.dataReader.ReadByte();
                if (currentByte == 0xfe) packageBytes.Clear();
                packageBytes.Add(currentByte);
                if (currentByte == 0xff)
                {
                    var responsePackage = new ResponsePackage();
                    responsePackage.ParseBytes(packageBytes.ToArray());
                    if (!this.waitingForResponse.ContainsKey(responsePackage.Id)) continue;
                    var completionSource = this.waitingForResponse[responsePackage.Id];
                    completionSource.SetResult(responsePackage.Data);
                    this.waitingForResponse.Remove(responsePackage.Id);
                }
            }
        }
private async void Listen ( ) { var packageBytes = new List < byte > ( ) ; while ( true ) { await this . dataReader . LoadAsync ( 1 ) ; byte currentByte = this . dataReader . ReadByte ( ) ; if ( currentByte == 0xfe ) packageBytes . Clear ( ) ; packageBytes . Add ( currentByte ) ; if ( currentByte == 0xff ) { var responsePackage = new ResponsePackage ( ) ; responsePackage . ParseBytes ( packageBytes . ToArray ( ) ) ; if ( ! this . waitingForResponse . ContainsKey ( responsePackage . Id ) ) continue ; var completionSource = this . waitingForResponse [ responsePackage . Id ] ; completionSource . SetResult ( responsePackage . Data ) ; this . waitingForResponse . Remove ( responsePackage . Id ) ; } } }
Listens the serial port for input packages.
Listens the serial port for input packages .
// For example, packages have a following format: // [0xfe][PackageId][Data][0xff]