Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine($"sizeof(int): {sizeof(int)}");
            Console.WriteLine($"sizeof(long): {sizeof(long)}");
            Console.WriteLine($"{FakeADWrapper.ErrorToString(Error.Success)}");

#if I_FAILED
            int cursor                   = 0;
            var pinnedCursor             = new PinnedObject(cursor);
            var pinnedCursorOpaqueHandle = pinnedCursor.ToIntPtr();
#endif
            var fakeAd = new FakeADWrapper(
                bytesPerSecond: CharsPerSecond,
                timeSliceInMilliseconds: 100,
                timeoutSliceCount: 3,
                port: 7373,
                callback: Typer.TypeIn,
#if I_FAILED
#if false
                context: pinnedCursor);
#elif false
                context : pinnedCursor.ToIntPtr());
#else
                context : pinnedCursorOpaqueHandle);
#endif
#else
                context : IntPtr.Zero);
#endif

            var buffer = new UnmanagedBuffer(sizeof(byte));
            while (fakeAd.Read(buffer, sizeof(byte)) == sizeof(byte))
            {
                Console.Write((char)buffer.Read <byte>());
            }

#if I_FAILED
            Console.WriteLine($"Cursor: {cursor}");
#endif

            Console.ReadKey();
        }
Exemplo n.º 2
0
        private void Acquire()
        {
            CancellationToken token = cancellationTokenSource_.Token;

            Stopwatch stopwatch = new Stopwatch();

            var timeSliceInMilliseconds = mainViewModel_.ECGenerator.CalculateBestTimeSlice();

            var fakeAdCallback = new FakeADCallback64(mainViewModel_.ECGenerator.GenerateECG);
            var fakeAd         = new FakeADWrapper(
                bytesPerSecond: (uint)(SamplingRate * ECGenerator.Channels * sizeof(double)),
                timeSliceInMilliseconds: (uint)timeSliceInMilliseconds,
                timeoutSliceCount: 3,
                port: 4321,
                callback: fakeAdCallback,
                context: IntPtr.Zero);

            var samples = SamplingRate * timeSliceInMilliseconds / 1000;

            var bufferSizeInBytes =
                (ulong)samples * ECGenerator.Channels * sizeof(double);
            var buffer = new UnmanagedBuffer((int)bufferSizeInBytes);

            Debug.Assert(ECGenerator.Channels == 1);

            var voltages = new double[samples];

            while (true)
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }

                stopwatch.Restart();

                var result = fakeAd.Read(buffer, bufferSizeInBytes);
                if (result != bufferSizeInBytes)
                {
                    var lastError       = FakeADWrapper.GetLastError();
                    var lastErrorString = FakeADWrapper.ErrorToString(lastError);
                    var errorMessage    = $"{lastError.ToString()} - {lastErrorString}";

                    Action notifyError = () =>
                    {
                        lastErrorTextBlock_.Text = errorMessage;
                    };

                    Dispatcher.BeginInvoke(notifyError);

                    if (lastError == Error.Timeout)
                    {
                        Thread.Sleep(timeSliceInMilliseconds);
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }

                Marshal.Copy(buffer, voltages, 0, voltages.Length);

                var elapsedMs = stopwatch.ElapsedMilliseconds;

                Action updateTextBlock = () =>
                {
                    readDataTextBlock_.Text = $"{elapsedMs}ms";
                };
                Dispatcher.BeginInvoke(updateTextBlock);

#if false
                Dispatcher.Invoke(() => DrawWave(voltages)); // Deadlock when close the window.
#else
                Action drawWave = () => DrawWave(voltages);
                Dispatcher.BeginInvoke(drawWave);
#endif
            }

            fakeAd.Dispose();
        }