コード例 #1
0
        private byte[] ReceiveResponseBytes(int responseBytesLength)
        {
            int timeout = _stream.ReadTimeout;

            if (WaitUntilAvailable(timeout))
            {
                _stopwatch.Restart();
                int totalReceivedBytes = 0;
                var response           = new byte[responseBytesLength];

                do
                {
                    int receivedBytes = _stream.Read(response, totalReceivedBytes, response.Length - totalReceivedBytes);
                    totalReceivedBytes += receivedBytes;
                    if (totalReceivedBytes >= response.Length)
                    {
                        break;
                    }
                    while ((_stream.Available < 1) && (timeout > _stopwatch.ElapsedMilliseconds))
                    {
                        Thread.Sleep(1);
                    }
                } while (timeout > _stopwatch.ElapsedMilliseconds);

                _stopwatch.Stop();

                return(response);
            }
            return(null);
        }
コード例 #2
0
        private int Read(ICommStream stream, byte[] buffer, int offset, int length, int timeout)
        {
            _stopwatch.Restart();
            int totalReceivedBytes = 0;

            do
            {
                int receivedBytes = stream.Read(buffer, offset + totalReceivedBytes, length - totalReceivedBytes);
                totalReceivedBytes += receivedBytes;
                if (totalReceivedBytes >= length)
                {
                    break;
                }

                while ((stream.Available < 1) && (timeout > _stopwatch.ElapsedMilliseconds))
                {
                    Thread.Sleep(1);
                }
            } while (timeout > _stopwatch.ElapsedMilliseconds);

            _stopwatch.Stop();

            return(totalReceivedBytes);
        }