private void btnWrite_Click(object sender, RoutedEventArgs e) { const string str = "FTDI"; if (dev == null) { return; } var t = Task.Factory.StartNew(async() => { byte[] bytes = new byte[str.Length * sizeof(char)]; Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); await dev.WriteAsync(bytes, (uint)bytes.Length); }); }
private async Task WriteBytes(byte[] bytesToWrite) { try { var nrBytesToWrite = bytesToWrite.Length; var bytesWritten = await device.WriteAsync(bytesToWrite, (uint)nrBytesToWrite); if (bytesWritten != nrBytesToWrite) { WriteToLog("Write failed, bytes written: '{0}', count: {1}.", BitConverter.ToString(bytesToWrite), nrBytesToWrite); } else { WriteToLog("Written: '{0}' to device, count: {1}.", BitConverter.ToString(bytesToWrite), nrBytesToWrite); } } catch (Exception ex) { WriteToLog("Failed to write: '{0}' to device, Exception={1}.", BitConverter.ToString(bytesToWrite), ex.Message); } }
/// <summary> /// Tests the Purging of a device /// </summary> /// <param name="ftManager">The ftManager instance.</param> /// <param name="deviceID">The device identifier.</param> /// <returns>Task{Boolean}.</returns> public async Task <Boolean> PurgeTest(FTManager ftManager, String deviceID) { try { int errorCode = (int)ERROR_CODES.SUCCESS; double start = DateTime.Now.TimeOfDay.TotalSeconds; String s = "\r\n\r\nStarted PurgeLoopbackTest\r\n"; AppendLogFile(s); AppendConsole(s); Boolean res = await Task.Run <Boolean>(async() => { for (int i = 0; i < 100; i++) { byte[] dataTx = new byte[10]; byte[] dataRx = new byte[10]; // 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); // Generate some random data... Random rnd = new Random(); for (int j = 0; j < dataTx.Length; j++) { dataTx[j] = (byte)rnd.Next(0, 0xff); } // Write then read back the data... await dev.WriteAsync(dataTx, (uint)dataTx.Length); dev.Purge(true, false); for (int j = 0; j < dataTx.Length; j++) { dataTx[j] = (byte)rnd.Next(0, 0xff); } // Write then read back the data... await dev.WriteAsync(dataTx, (uint)dataTx.Length); uint count = await dev.ReadAsync(dataRx, (uint)dataTx.Length); if (count < dataTx.Length) { errorCode = (int)ERROR_CODES.FAILED_TO_READ_ALL_DATA; dev.Close(); return(false); } for (int j = 0; j < dataTx.Length; 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 PurgeLoopbackTest: 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); } }
private async Task MultiThreadedReadWrite() { int errorCode = (int)ERROR_CODES.SUCCESS; // 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(devList[0].DeviceId); if (dev == null) { errorCode = (int)ERROR_CODES.FAILED_TO_OPEN; return; } await SetUARTSettings(dev); byte[] dataTx = new byte[10]; byte[] dataRx = new byte[10]; var task1 = Windows.System.Threading.ThreadPool.RunAsync( async(workItem) => { while (true) { // Generate some random data... Random rnd = new Random(); for (int j = 0; j < dataTx.Length; j++) { dataTx[j] = (byte)rnd.Next(0, 0xff); } await dev.WriteAsync(dataTx, (uint)10); } }).AsTask(); var task2 = Windows.System.Threading.ThreadPool.RunAsync( async(workItem) => { while (true) { uint count = await dev.ReadAsync(dataRx, (uint)10); if (count < 10) { return; } } }).AsTask(); #if DAVE var task1 = Windows.System.Threading.ThreadPool.RunAsync( async(workItem) => { byte[] dataTx = new byte[10]; byte[] dataRx = new byte[10]; // 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(devList[0].DeviceId); if (dev == null) { errorCode = (int)ERROR_CODES.FAILED_TO_OPEN; return; } await SetUARTSettings(dev); // Generate some random data... Random rnd = new Random(); for (int j = 0; j < dataTx.Length; j++) { dataTx[j] = (byte)rnd.Next(0, 0xff); } // Write then read back the data... await dev.Write(dataTx, (uint)10); uint count = dev.Read(dataRx, (uint)10); if (count < 10) { errorCode = (int)ERROR_CODES.FAILED_TO_READ_ALL_DATA; return; } for (int j = 0; j < 10; j++) { if (dataTx[j] != dataRx[j]) { errorCode = (int)ERROR_CODES.DATA_INTEGRITY; return; } } dev.Close(); }).AsTask(); //await task1; task1.Wait(); #endif #if DAVE while (task1.Status == TaskStatus.Running) { ; } var task2 = Windows.System.Threading.ThreadPool.RunAsync( async(workItem) => { byte[] dataTx = new byte[10]; byte[] dataRx = new byte[10]; // Create device list... var devList = ftManager.GetDeviceList(); if (devList.Count == 0) { errorCode = (int)ERROR_CODES.NO_DEVICES_FOUND; return; } // Find device in the list again... IFTDevice dev = ftManager.OpenByDeviceID(devList[0].DeviceId); if (dev == null) { errorCode = (int)ERROR_CODES.FAILED_TO_OPEN; return; } await SetUARTSettings(dev); // Generate some random data... Random rnd = new Random(); for (int j = 0; j < dataTx.Length; j++) { dataTx[j] = (byte)rnd.Next(0, 0xff); } // Write then read back the data... await dev.Write(dataTx, (uint)10); uint count = dev.Read(dataRx, (uint)10); if (count < 10) { errorCode = (int)ERROR_CODES.FAILED_TO_READ_ALL_DATA; return; } for (int j = 0; j < 10; j++) { if (dataTx[j] != dataRx[j]) { errorCode = (int)ERROR_CODES.DATA_INTEGRITY; return; } } dev.Close(); }).AsTask(); await task2; #endif }
/// <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); } }
/// <summary> /// Loopback test that transmits 1MB of data to the device and reads it back. /// </summary> public async Task <Boolean> LoopbackTest(FTManager ftManager, String deviceID) { try { String s = ""; double start = 0; int errorCode = (int)ERROR_CODES.SUCCESS; int TOTAL_BYTES = 65535; int PACKET_SIZE = 1024; int iterations = TOTAL_BYTES / PACKET_SIZE; Boolean res = await Task.Run <Boolean>(async() => { byte[] dataTx = new byte[TOTAL_BYTES]; byte[] dataRx = new byte[PACKET_SIZE]; // 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 LoopbackTest\r\n"; AppendLogFile(s); AppendConsole(s); for (int i = 0; i < iterations; i++) { byte[] buf = new byte[PACKET_SIZE]; Buffer.BlockCopy(dataTx, i *PACKET_SIZE, buf, 0, PACKET_SIZE); // Write then read back the data... double t1 = DateTime.Now.TimeOfDay.TotalSeconds; await dev.WriteAsync(buf, (uint)PACKET_SIZE); double t2 = DateTime.Now.TimeOfDay.TotalSeconds; double t3 = DateTime.Now.TimeOfDay.TotalSeconds; uint count = await dev.ReadAsync(dataRx, (uint)PACKET_SIZE); double t4 = DateTime.Now.TimeOfDay.TotalSeconds; if (count < PACKET_SIZE) { errorCode = (int)ERROR_CODES.FAILED_TO_READ_ALL_DATA; return(false); } double span = t4 - t3; //AppendConsole(span.ToString()); double t5 = DateTime.Now.TimeOfDay.TotalSeconds; for (int j = 0; j < PACKET_SIZE; j++) { if (buf[j] != dataRx[j]) { errorCode = (int)ERROR_CODES.DATA_INTEGRITY; return(false); } } double t6 = DateTime.Now.TimeOfDay.TotalSeconds; } dev.Close(); return(true); }).AsAsyncOperation(); double finish = DateTime.Now.TimeOfDay.TotalSeconds; s = String.Format( @"Finished LoopbackTest: 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 Task <uint> WriteAsync(byte[] bytesToWrite, uint nrBytesToWrite) { return(ftDevice.WriteAsync(bytesToWrite, nrBytesToWrite).AsTask()); }