public byte[] ControlTransferIn(byte requestType, byte request, UInt16 value, UInt16 index, UInt16 length) { NativeMethods.WINUSB_SETUP_PACKET setupPacket = new NativeMethods.WINUSB_SETUP_PACKET(); setupPacket.RequestType = (byte)(requestType | ControlDirectionIn); setupPacket.Request = request; setupPacket.Value = value; setupPacket.Index = index; setupPacket.Length = length; byte[] output = new byte[length]; UInt32 actualLength = 0; if (!NativeMethods.WinUsb_ControlTransfer(WinusbHandle, setupPacket, output, (uint)output.Length, out actualLength, IntPtr.Zero)) { throw new Exception("ControlTransfer failed. " + (new Win32Exception()).ToString()); } if (actualLength != output.Length) { byte[] copyTo = new byte[actualLength]; Array.Copy(output, copyTo, actualLength); output = copyTo; } return(output); }
public void ControlTransferOut(byte requestType, byte request, UInt16 value, UInt16 index, byte[] data) { NativeMethods.WINUSB_SETUP_PACKET setupPacket = new NativeMethods.WINUSB_SETUP_PACKET(); setupPacket.RequestType = (byte)(requestType | ControlDirectionOut); setupPacket.Request = request; setupPacket.Value = value; setupPacket.Index = index; if (data != null) { setupPacket.Length = (ushort)data.Length; } UInt32 actualLength = 0; if (!NativeMethods.WinUsb_ControlTransfer(WinusbHandle, setupPacket, data, setupPacket.Length, out actualLength, IntPtr.Zero)) { throw new Exception("ControlTransfer failed. " + (new Win32Exception()).ToString()); } if (data != null && actualLength != data.Length) { throw new Exception("Not all data transferred"); } }
public byte[] ControlTransferIn(byte requestType, byte request, UInt16 value, UInt16 index, UInt16 length) { NativeMethods.WINUSB_SETUP_PACKET setupPacket = new NativeMethods.WINUSB_SETUP_PACKET(); setupPacket.RequestType = (byte)(requestType | ControlDirectionIn); setupPacket.Request = request; setupPacket.Value = value; setupPacket.Index = index; setupPacket.Length = length; byte[] output = new byte[length]; UInt32 actualLength = 0; if(!NativeMethods.WinUsb_ControlTransfer(WinusbHandle, setupPacket, output, (uint)output.Length, out actualLength, IntPtr.Zero)) { throw new Exception("ControlTransfer failed. " + (new Win32Exception()).ToString()); } if(actualLength != output.Length) { byte[] copyTo = new byte[actualLength]; Array.Copy(output, copyTo, actualLength); output = copyTo; } return output; }