示例#1
0
        //====================================================================================================================================================================
        /// <summary>
        /// Gets the endpoint addresses for the specified interface number an pipe
        /// </summary>
        /// <param name="deviceHandle">The device handle</param>
        /// <param name="alternateInterfaceNumber">The interface number</param>
        /// <param name="pipeIndex">The pipe index</param>
        /// <param name="pipeInformation">An instance of UsbPipeInformation</param>
        /// <returns>True if the call succeeded otherwise false</returns>
        //====================================================================================================================================================================
        private bool McUsb_QueryPipe(SafeFileHandle deviceHandle, 
            byte alternateInterfaceNumber,
            byte pipeIndex,
            ref UsbPipeInformation pipeInformation)
        {
            int bytesReturned = 0;

            byte[] inBuffer = new byte[] { alternateInterfaceNumber, pipeIndex };
            byte[] outBuffer = new byte[sizeof(UsbPipeInformation)];

            bool result = DeviceIoControl(deviceHandle,
                                          DeviceIOControlCodes.QueryPipe,
                                          inBuffer,
                                          inBuffer.Length,
                                          outBuffer,
                                          outBuffer.Length,
                                          ref bytesReturned,
                                          IntPtr.Zero);

            pipeInformation.PipeType = (UsbPipeType)BitConverter.ToInt32(outBuffer, 0);
            pipeInformation.PipeId = (byte)BitConverter.ToChar(outBuffer, 4);
            pipeInformation.MaximumPacketSize = BitConverter.ToUInt16(outBuffer, 5);
            pipeInformation.Interval = (byte)BitConverter.ToChar(outBuffer, 6);

            return result;
        }
示例#2
0
        //=========================================================================
        /// <summary>
        /// Initialize and configures the USB device
        /// </summary>
        /// <param name="deviceHandle">The handle to the device</param>
        /// <param name="deviceInfo">The device number</param>
        //=========================================================================
        protected void InitializeDevice(SafeFileHandle deviceHandle, DeviceInfo deviceInfo)
        {
            UsbInterfaceDescriptor uid = new UsbInterfaceDescriptor();
            UsbPipeInformation pipeInfo = new UsbPipeInformation();

            uid.Length = 0;
            uid.DescriptorType = 0;
            uid.InterfaceNumber = 0;
            uid.AlternateSetting = 0;
            uid.NumEndpoints = 0;
            uid.InterfaceClass = 0;
            uid.InterfaceSubClass = 0;
            uid.InterfaceProtocol = 0;
            uid.Interface = 0;

            pipeInfo.PipeType = 0;
            pipeInfo.PipeId = 0;
            pipeInfo.MaximumPacketSize = 0;
            pipeInfo.Interval = 0;

            if (!deviceHandle.IsInvalid)
            {
                if (McUsb_QueryInterfaceSettings(deviceHandle, 0, ref uid) == true)
                {
                    m_deviceInitialized = true;

                    // get the bulk pipes info
                    for (byte i = 0; i < uid.NumEndpoints; i++)
                    {
                        McUsb_QueryPipe(deviceHandle, 0, i, ref pipeInfo);

                        unsafe
                        {
                            if (pipeInfo.PipeType == UsbPipeType.Bulk)
                            {
                                if ((pipeInfo.PipeId & 0x80) == 0x80)
                                {
                                    deviceInfo.EndPointIn = pipeInfo.PipeId;

                                    deviceInfo.MaxPacketSize = pipeInfo.MaximumPacketSize;
                                }
                                else
                                {
                                    deviceInfo.EndPointOut = pipeInfo.PipeId;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                m_errorCode = ErrorCodes.DeviceHandleAlreadyCreated;
            }
        }