private void btnRead_Click(object sender, RoutedEventArgs e) { if (dev == null) { return; } Task <String> t = Task.Run(async() => { uint len = dev.GetQueueStatus(); if (len == 0) { return(null); } byte[] data = new byte[len]; uint read = await dev.ReadAsync(data, len); if (read != len) { return(null); } string result = System.Text.Encoding.UTF8.GetString(data, 0, (int)len); return(result); }); AppendConsole(t.Result); }
private async void StartReadingData() { while (true) // todo: build in cancellation support { try { var bytesInQueue = device.GetQueueStatus(); bytesInQueue = Math.Max(bytesInQueue, 1); // to make sure we don't create a cpu eating loop var buffer = new byte[bytesInQueue]; var bytesRead = await device.ReadAsync(buffer, bytesInQueue); if (bytesRead != 0) { ReadBuffer = ReadBuffer.Concat(buffer.Take((int)bytesRead)).ToArray(); } } catch (Exception ex) { WriteToLog(string.Format("Exception occurred: {0}", ex.Message)); } } }
/// <summary> /// Tests the QueueStatus property of an IFTDevice. /// </summary> /// <param name="ftManager">The ftManager instance.</param> /// <param name="deviceID">The device identifier.</param> /// <returns>Task{Boolean}.</returns> public async Task <Boolean> QueueStatusTest(FTManager ftManager, String deviceID) { try { String s = ""; double start = 0; int errorCode = (int)ERROR_CODES.SUCCESS; uint TOTAL_BYTES = 32769; uint PACKET_SIZE = 1024; uint iterations = TOTAL_BYTES / PACKET_SIZE; Boolean res = await Task.Run <Boolean>(async() => { byte[] dataTx = new byte[TOTAL_BYTES]; byte[] dataRx = new byte[TOTAL_BYTES]; // Generate some random data... Random rnd = new Random(); for (int j = 0; j < dataTx.Length; j++) { dataTx[j] = (byte)rnd.Next(0, 0xff); } // Create device list... var devList = ftManager.GetDeviceList(); if (devList.Count == 0) { errorCode = (int)ERROR_CODES.NO_DEVICES_FOUND; } // Find device in the list again... IFTDevice dev = ftManager.OpenByDeviceID(deviceID); if (dev == null) { errorCode = (int)ERROR_CODES.FAILED_TO_OPEN; } await SetUARTSettings(dev); start = DateTime.Now.TimeOfDay.TotalSeconds; s = "\r\n\r\nStarted QueueStatusTest\r\n"; AppendLogFile(s); AppendConsole(s); await dev.WriteAsync(dataTx, 128); dev.Purge(true, false); for (int j = 0; j < TOTAL_BYTES; j++) { dataTx[j] = (byte)j; } await dev.ResetAsync(); await dev.WriteAsync(dataTx, 10); while (dev.GetQueueStatus() != 10) { //return false; } if (await dev.ReadAsync(dataRx, 10) != 10) { return(false); } for (int j = 0; j < 10; j++) { if (dataTx[j] != dataRx[j]) { errorCode = (int)ERROR_CODES.DATA_INTEGRITY; return(false); } } dev.Close(); return(true); }).AsAsyncOperation(); double finish = DateTime.Now.TimeOfDay.TotalSeconds; s = String.Format( @"Finished QueueStatusTest: Result: {0} ErrorCode: {1} Duration: {2}secs", res.ToString().ToLower(), errorCode, Math.Round(finish - start, 2)); AppendLogFile(s); AppendConsole(s); return(res); } catch { return(false); } }
public uint GetQueueStatus() { return(ftDevice.GetQueueStatus()); }