示例#1
0
        ///  <summary>
        ///  Initiates a read operation from a bulk IN endpoint.
        ///  To enable reading without blocking the main thread, uses an asynchronous delegate.
        ///  </summary>
        ///
        ///  <remarks>
        ///  To enable reading more than 64 bytes (with device firmware support), increase bytesToRead.
        ///  </remarks>
        public static void ReadBulkData()
        {
            if (DeviceDetected)
            {
                const uint bytesToRead = BulkBufferSize; // CH1+CH2+CHD+frame number

                //  Define a delegate for the ReadViaBulkTransfer method of WinUsbDevice.
                ReadFromDeviceDelegate myReadFromDeviceDelegate =
                    _usbDevice.ReadViaBulkTransfer;

                //  The BeginInvoke method calls MyWinUsbDevice.ReadViaBulkTransfer to attempt
                //  to read data. The method has the same parameters as ReadViaBulkTransfer,
                //  plus two additional parameters:
                //  GetReceivedBulkData is the callback routine that executes when
                //  ReadViaBulkTransfer returns.
                //  MyReadFromDeviceDelegate is the asynchronous delegate object.

                var readState = new AsyncReadState {
                    Success = false, BytesRead = 0, Data = new byte[BulkBufferSize]
                };


                myReadFromDeviceDelegate.BeginInvoke(
                    Convert.ToByte(_usbDevice.DeviceInfo.BulkInPipe),
                    bytesToRead,
                    ref readState.Data,
                    ref readState.BytesRead,
                    out readState.Success,
                    GetReceivedBulkData,
                    readState
                    );
            }
        }
        ///  <summary>
        ///  Retrieves received data from a bulk endpoint.
        ///  This routine is called automatically when m_WinUsbDevice.ReadViaBulkTransfer
        ///  returns. The routine calls several marshaling routines to access the main form.
        ///  </summary>
        ///
        ///  <param name="ar"> An object containing status information about the
        ///  asynchronous operation.</param>
        ///
        private void GetReceivedBulkData(IAsyncResult ar)
        {
            UInt32 bytesRead = 0;
            Byte   pipeId    = 0;

            Byte[]  receivedDataBuffer;
            Boolean success = false;

            try
            {
                receivedDataBuffer = null;

                // Define a delegate using the IAsyncResult object.

                ReadFromDeviceDelegate deleg =
                    ((ReadFromDeviceDelegate)(ar.AsyncState));

                // Get the IAsyncResult object and the values of other paramaters that the
                // BeginInvoke method passed ByRef.

                deleg.EndInvoke
                    (ref pipeId,
                    ref receivedDataBuffer,
                    ref bytesRead,
                    ref success, ar);

                // protect the follow data using mutex
                readSem.WaitOne();
                // update the total received byte count.
                if ((ar.IsCompleted && success))
                {
                    m_bytesRx += bytesRead;
                }
                else
                {
                    MyMarshalToForm("AddItemToTextBox", "The attempt to read bulk data has failed.");
                    m_DeviceDetected = false;
                }
                // release mutex
                readSem.Release();
                // Enable requesting another transfer.
                if (m_stopTest == false)
                {
                    ReadDataViaBulkTransfer(pipeId);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        ///  <summary>
        ///  Initiates a read operation from a bulk IN endpoint.
        ///  To enable reading without blocking the main thread, uses an asynchronous delegate.
        ///  </summary>
        ///
        ///  <remarks>
        ///  To enable reading more than 64 bytes (with device firmware support), increase bytesToRead.
        ///  </remarks>

        private void ReadDataViaBulkTransfer(Byte pipeID)
        {
            IAsyncResult ar          = null;
            UInt32       bytesRead   = 0;
            UInt32       bytesToRead = Convert.ToUInt32(m_frameBuf.Length);
            Boolean      success     = false;

            //  Define a delegate for the ReadViaBulkTransfer method of WinUsbDevice.

            ReadFromDeviceDelegate MyReadFromDeviceDelegate =
                new ReadFromDeviceDelegate(m_WinUsbDevice.ReadViaBulkTransfer);

            try
            {
                //  The BeginInvoke method calls m_WinUsbDevice.ReadViaBulkTransfer to attempt
                //  to read data. The method has the same parameters as ReadViaBulkTransfer,
                //  plus two additional parameters:
                //  GetReceivedBulkData is the callback routine that executes when
                //  ReadViaBulkTransfer returns.
                //  MyReadFromDeviceDelegate is the asynchronous delegate object.

                ar = MyReadFromDeviceDelegate.BeginInvoke
                         (ref pipeID,
                         bytesToRead,
                         ref m_frameBuf,
                         ref bytesRead,
                         ref success,
                         new AsyncCallback(GetReceivedBulkData),
                         MyReadFromDeviceDelegate);

                if (success)
                {
                    //m_bytesRx += bytesRead;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#4
0
        ///  <summary>
        ///  Initiates a read operation from a bulk IN endpoint.
        ///  To enable reading without blocking the main thread, uses an asynchronous delegate.
        ///  </summary>
        ///  
        ///  <remarks>
        ///  To enable reading more than 64 bytes (with device firmware support), increase bytesToRead.
        ///  </remarks> 
        private void ReadDataViaBulkTransfer(Byte pipeID)
        {
            IAsyncResult ar = null;
            UInt32 bytesRead = 0;
            UInt32 bytesToRead = Convert.ToUInt32(m_frameBuf.Length);
            Boolean success = false;

            //  Define a delegate for the ReadViaBulkTransfer method of WinUsbDevice.

            ReadFromDeviceDelegate MyReadFromDeviceDelegate =
                new ReadFromDeviceDelegate(m_WinUsbDevice.ReadViaBulkTransfer);

            try
            {
                //  The BeginInvoke method calls m_WinUsbDevice.ReadViaBulkTransfer to attempt
                //  to read data. The method has the same parameters as ReadViaBulkTransfer,
                //  plus two additional parameters:
                //  GetReceivedBulkData is the callback routine that executes when
                //  ReadViaBulkTransfer returns.
                //  MyReadFromDeviceDelegate is the asynchronous delegate object.

                ar = MyReadFromDeviceDelegate.BeginInvoke
                    (ref pipeID,
                    bytesToRead,
                    ref m_frameBuf,
                    ref bytesRead,
                    ref success,
                    new AsyncCallback(GetReceivedBulkData),
                    MyReadFromDeviceDelegate);

                if (success)
                {
                    //m_bytesRx += bytesRead;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#5
0
        ///  <summary>
        ///  Initiates a read operation from an interrupt IN endpoint.
        ///  To enable reading without blocking the main thread, uses an asynchronous delegate.
        ///  </summary>
        ///  
        ///  <remarks>
        ///  To enable reading more than 2 bytes (with device firmware support), increase bytesToRead.
        ///  </remarks>
        private void ReadDataViaInterruptTransfer()
        {
            IAsyncResult ar = null;
            Byte[] buffer = new Byte[2];
            UInt32 bytesRead = 0;
            UInt32 bytesToRead = System.Convert.ToUInt32(2);
            Boolean success = false;

            try
            {
                //  Define a delegate for the ReadViaInterruptTransfer method of WinUsbDevice.

                ReadFromDeviceDelegate MyReadFromDeviceDelegate = new ReadFromDeviceDelegate(myWinUsbDevice.ReadViaInterruptTransfer);

                //  The BeginInvoke method calls MyWinUsbDevice.ReadViaInterruptTransfer to attempt
                //  to read data. The method has the same parameters as ReadViaInterruptTransfer,
                //  plus two additional parameters:
                //  GetReceivedInterruptData is the callback routine that executes when
                //  ReadViaInterruptTransfer returns.
                //  MyReadFromDeviceDelegate is the asynchronous delegate object.

                ar = MyReadFromDeviceDelegate.BeginInvoke
                    (System.Convert.ToByte(myWinUsbDevice.myDevInfo.interruptInPipe),
                    bytesToRead,
                    ref buffer,
                    ref bytesRead,
                    ref success,
                    new AsyncCallback(GetReceivedInterruptData),
                    MyReadFromDeviceDelegate);
            }
            catch (Exception ex)
            {
                throw;
            }
        }