예제 #1
0
        private static void ReadData(object context)
        {
            UsbTransfer       overlappedTransferContext = (UsbTransfer)context;
            UsbEndpointReader reader = (UsbEndpointReader)overlappedTransferContext.EndpointBase;

            reader.mDataReceivedEnabled = true;
            EventHandler <DataReceivedEnabledChangedEventArgs> dataReceivedEnabledChangedEvent;

            dataReceivedEnabledChangedEvent = reader.DataReceivedEnabledChanged;
            if (!ReferenceEquals(dataReceivedEnabledChangedEvent, null))
            {
                dataReceivedEnabledChangedEvent(reader, new DataReceivedEnabledChangedEventArgs(reader.mDataReceivedEnabled));
            }

            overlappedTransferContext.Reset();

            byte[] buf = new byte[reader.mReadBufferSize];
            try
            {
                while (!overlappedTransferContext.IsCancelled)
                {
                    int       iTransferLength;
                    ErrorCode eReturn = reader.Transfer(buf, 0, buf.Length, Timeout.Infinite, out iTransferLength);
                    if (eReturn == ErrorCode.None)
                    {
                        EventHandler <EndpointDataEventArgs> temp = reader.DataReceived;
                        if (!ReferenceEquals(temp, null) && !overlappedTransferContext.IsCancelled)
                        {
                            temp(reader, new EndpointDataEventArgs(buf, iTransferLength));
                        }
                        continue;
                    }
                    if (eReturn != ErrorCode.IoTimedOut)
                    {
                        break;
                    }
                }
            }
#if !NETSTANDARD1_5 && !NETSTANDARD1_6
            catch (ThreadAbortException)
            {
                UsbError.Error(ErrorCode.ReceiveThreadTerminated, 0, "ReadData:Read thread aborted.", reader);
            }
#endif
            finally
            {
                reader.Abort();
                reader.mDataReceivedEnabled = false;

                dataReceivedEnabledChangedEvent = reader.DataReceivedEnabledChanged;
                if (!ReferenceEquals(dataReceivedEnabledChangedEvent, null))
                {
                    dataReceivedEnabledChangedEvent(reader, new DataReceivedEnabledChangedEventArgs(reader.mDataReceivedEnabled));
                }
            }
        }