コード例 #1
0
        //*****************************************************************

        /// <summary>
        /// Initiates a USB control transfer with optional data sent to the USB device.
        /// </summary>
        /// <param name="bmRequestType">The USB <i>bmRequestType</i>field.</param>
        /// <param name="bRequest">The USB <i>bRequest</i>field.</param>
        /// <param name="wValue">The USB <i>wValue</i>field.</param>
        /// <param name="wIndex">The USB <i>wIndex</i>field.</param>
        /// <param name="buffer">An array of bytes with the data to write to the
        /// USB device. May be <b>null</b>.</param>
        /// <exception cref="IOException">The <see cref="UsbDevice"/> is not open,
        /// or the transfer did not complete.</exception>
        /// <exception cref="Win32Exception">An error was reported by
        /// the operating system.</exception>
        /// <remarks>
        /// <para>
        /// The first four arguments of this method correspond precisely with the
        /// like-named fields of a USB Setup packet. See the USB specification
        /// for the meaning and use of these parameters.</para>
        /// <para>
        /// The <i>wLength</i> field of the USB Setup packet is set to the
        /// length of <paramref name="buffer"/>, or zero if <paramref name="buffer"/>
        /// is <b>null</b>.
        /// </para>
        /// </remarks>
        public void ControlWrite(byte bmRequestType, byte bRequest, ushort wValue, ushort wIndex, byte[] buffer)
        {
            bool retval;
            uint cbWritten;

            WinUsbApi.WINUSB_SETUP_PACKET packet;

            packet.RequestType = (byte)(bmRequestType & WinUsbApi.PipeMask);                    // ensure OUT transfer
            packet.Request     = bRequest;
            packet.Value       = wValue;
            packet.Index       = wIndex;
            if (buffer == null)
            {
                buffer = new byte[0];
            }
            packet.Length = (ushort)buffer.Length;

            retval = WinUsbApi.WinUsb_ControlTransfer(
                UsbHandle,
                packet,
                buffer,
                (uint)buffer.Length,
                out cbWritten,
                IntPtr.Zero);

            if (!retval)
            {
                throw new Win32Exception();
            }

            if (cbWritten != buffer.Length)
            {
                throw new IOException();
            }
        }
コード例 #2
0
        //*****************************************************************

        /// <summary>
        /// Initiates a USB control transfer that reads data from the USB device.
        /// </summary>
        /// <param name="bmRequestType">The USB <i>bmRequestType</i>field.</param>
        /// <param name="bRequest">The USB <i>bRequest</i>field.</param>
        /// <param name="wValue">The USB <i>wValue</i>field.</param>
        /// <param name="wIndex">The USB <i>wIndex</i>field.</param>
        /// <param name="wLength">The USB <i>wLength</i>field.</param>
        /// <returns>
        /// <para>A byte array containing the bytes read from the USB device.</para>
        /// </returns>
        /// <exception cref="IOException">The <see cref="UsbDevice"/> is not open.</exception>
        /// <exception cref="Win32Exception">An error was reported by
        /// the operating system.</exception>
        /// <remarks>
        /// <para>
        /// The arguments of this method correspond precisely with the
        /// like-named fields of a USB Setup packet. See the USB specification
        /// for the meaning and use of these parameters.</para>
        /// <para>
        /// The <paramref name="wLength"/> parameter is the maximum number of
        /// bytes to read from the USB device, and the USB device may send less.
        /// The length of the returned byte array is the actual number of
        /// bytes received.</para>
        /// </remarks>
        public byte[] ControlRead(byte bmRequestType, byte bRequest, ushort wValue, ushort wIndex, ushort wLength)
        {
            byte[] arbBuf;
            byte[] arbData;
            uint   cbRead;
            bool   retval;

            WinUsbApi.WINUSB_SETUP_PACKET packet;

            packet.RequestType = (byte)(bmRequestType | WinUsbApi.ReadFlag);                    // ensure IN transfer
            packet.Request     = bRequest;
            packet.Value       = wValue;
            packet.Index       = wIndex;
            packet.Length      = wLength;
            arbBuf             = new byte[wLength];

            retval = WinUsbApi.WinUsb_ControlTransfer(
                UsbHandle,
                packet,
                arbBuf,
                (uint)arbBuf.Length,
                out cbRead,
                IntPtr.Zero);

            if (!retval)
            {
                throw new Win32Exception();
            }

            if (cbRead == arbBuf.Length)
            {
                return(arbBuf);
            }

            arbData = new byte[cbRead];
            Array.Copy(arbBuf, arbData, cbRead);
            return(arbData);
        }