Ejemplo n.º 1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            //This is getting called when the ND command is sent - why?

            //Note, it's not clear why BinarySerializer ends up calling this method.
            //The following is a work around which needs review.

            //Do the same checks as the aync version.
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            if (offset != 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), offset, "Must be zero.");
            }

            //Receive the data in a blocking fashion.
            Task <byte[]> dataTask = _serialDevice.ReadAsync((uint)count, new CancellationToken());

            dataTask.Wait();

            byte[] data = dataTask.Result;

            Array.Copy(data, buffer, data.Length);
            return(data.Length);
        }
Ejemplo n.º 2
0
        public override Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            if (offset != 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), offset, "Must be zero.");
            }

            if (count != buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(count), count, "Must match buffer length.");
            }

            return(_serialDevice.ReadAsync(buffer, offset, count, cancellationToken));
        }
Ejemplo n.º 3
0
        public override async Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            if (offset != 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), offset, "Must be zero.");
            }

            if (count != buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(count), count, "Must match buffer length.");
            }

            var data = await _serialDevice.ReadAsync((uint)count, cancellationToken);

            Array.Copy(data, buffer, data.Length);
            return(data.Length);
        }
Ejemplo n.º 4
0
        public override async Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            if (offset != 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), offset, "Must be zero.");
            }

            var data = await _serialDevice.ReadAsync((uint)count, cancellationToken).ConfigureAwait(false);

            Array.Copy(data, buffer, data.Length);
            return(data.Length);
        }
Ejemplo n.º 5
0
        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);

                    ReadBuffer = ReadBuffer.Concat(bytesRead).ToArray();
                }
                catch (Exception ex)
                {
                    WriteToLog(string.Format("Exception occurred: {0}", ex.Message));
                }
            }
        }