//=================================================================================================== /// <summary> /// Creates one or more BulkInRequest objects that contain the overlapped struct and data buffer /// These are used by the SubmitBulkInRequest and CompleteBulkInRequest methods /// </summary> //=================================================================================================== protected override void CreateBulkInputRequestObjects(int transferSize) { m_bulkInRequests.Clear(); int byteRatio = m_criticalParams.DataInXferSize; int channelCount = m_criticalParams.AiChannelCount; for (int i = 0; i < m_numberOfWorkingInputRequests; i++) { UsbBulkInRequest request = new UsbBulkInRequest(); request.Index = i; request.Overlapped = new Overlapped(); if (m_criticalParams.InputTransferMode == TransferMode.SingleIO) { request.Buffer = new byte[byteRatio * channelCount]; if (m_criticalParams.NumberOfSamplesForSingleIO > 1) { // use n bytes per channel... request.BytesRequested = byteRatio * channelCount; } else { // only n bytes per transfer regardless of channel count... request.BytesRequested = byteRatio; } } else { request.Buffer = new byte[transferSize]; request.BytesRequested = request.Buffer.Length; } request.NativeOverlapped = request.Overlapped.Pack(CompleteBulkInRequest, request.Buffer); if (sizeof(IntPtr) == 8) request.NativeOverLappedIntPtr = new IntPtr((long)request.NativeOverlapped); else request.NativeOverLappedIntPtr = new IntPtr((int)request.NativeOverlapped); if (i > 0) m_bulkInRequests[i - 1].Next = request; m_bulkInRequests.Add(request); } m_bulkInRequests[m_bulkInRequests.Count - 1].Next = m_bulkInRequests[0]; }
//=================================================================================================== /// <summary> /// Sumbimits one or more read pipe requests using overlapped I/O /// The I/O completion callback is handled by CompleteBulkInRequest /// </summary> /// <param name="request">A UsbBulkInRequest object</param> /// <returns>The result of the read pipe method</returns> //=================================================================================================== protected bool SubmitBulkInRequest(UsbBulkInRequest request) { bool result; uint lengthTransfered = 0; if (m_deviceInfo.EndPointIn == 0) { m_inputScanErrorCode = ErrorCodes.BulkInputTransfersNotSupported; return false; } if (m_criticalParams.InputSampleMode == SampleMode.Finite) { // for finite mode, the number of bytes in the last transfer may need to be reduced so that the // number of bytes transfered equals the number of bytes requested if (m_totalNumberOfInputBytesTransferred + request.BytesRequested > m_totalNumberOfInputBytesRequested) { request.BytesRequested = (m_totalNumberOfInputBytesRequested - m_totalNumberOfInputBytesTransferred); } } m_totalNumberOfInputBytesTransferred += request.BytesRequested; //DebugLogger.WriteLine("Submitting bulk in index# {0}, request# {1} ", request.Index, request.RequestNumber); result = McUsb_ReadPipe(m_deviceHandle, m_deviceInfo.EndPointIn, request.Buffer, (uint)request.BytesRequested, ref lengthTransfered, request.NativeOverLappedIntPtr); // store this request object so we know which one to submit next m_lastInputRequestSubmitted = request; // increment number of requests submitted m_numberOfInputRequestsSubmitted++; // for continuous mode keep incrementing the total number of requests if (m_criticalParams.InputSampleMode == SampleMode.Continuous) { m_totalNumberOfInputRequests++; if (m_totalNumberOfInputRequests < 0) { m_numberOfInputRequestsSubmitted = 0; m_totalNumberOfInputRequests = m_numberOfInputRequestsSubmitted + 1; } } if (!result) { int errorCode = GetLastError(); // error 997 is Overlapped I/O operation is in progress (so this is good). if (errorCode == 997) { m_inputScanErrorCode = ErrorCodes.NoErrors; result = true; } else { m_inputScanErrorCode = ErrorCodes.UnknownError; } } return result; }
//=================================================================================================== /// <summary> /// Creates one or more BulkInRequest objects that contain the overlapped struct and data buffer /// These are used by the SubmitBulkInRequest and CompleteBulkInRequest methods /// </summary> //=================================================================================================== protected override void CreateBulkInRequestObjects() { m_bulkInRequests.Clear(); for (int i = 0; i < m_numberOfWorkingInputRequests; i++) { // create bulk in request object UsbBulkInRequest request = new UsbBulkInRequest(); // assign index request.Index = i; // allocate the bulk in request buffer request.Buffer = new byte[m_bulkInXferSize]; request.BytesRequested = request.Buffer.Length; m_bulkInRequests.Add(request); } }