Пример #1
0
        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
        }