ControlTransfer() public méthode

Transmits control data over a default control endpoint.
public ControlTransfer ( UsbSetupPacket &setupPacket, IntPtr buffer, int bufferLength, int &lengthTransferred ) : bool
setupPacket LibUsbDotNet.Main.UsbSetupPacket An 8-byte setup packet which contains parameters for the control request. /// See section 9.3 USB Device Requests of the Universal Serial Bus Specification Revision 2.0 for more information.
buffer System.IntPtr Data to be sent/received from the device.
bufferLength int Length of the buffer param.
lengthTransferred int Number of bytes sent or received (depends on the direction of the control transfer).
Résultat bool
        public static Boolean checkProtocol(UsbDevice device)
        {
            Boolean r = false;

            string message = "";
            short messageLength = 2;

            UsbSetupPacket setupPacket = new UsbSetupPacket();
            setupPacket.RequestType = (byte)((byte)UsbConstants.USB_DIR_IN | (byte) UsbConstants.USB_TYPE_VENDOR);
            setupPacket.Request = (byte)ACCESSORY_GET_PROTOCOL;
            setupPacket.Value = 0;
            setupPacket.Index = 0;
            setupPacket.Length = 0;

            int resultTransferred;

            r = device.ControlTransfer(ref setupPacket, message, messageLength, out resultTransferred);

            return r;
        }
        /// <summary>
        /// Sends the control request to set the test mode
        /// </summary>
        private static int Bench_SetTestType(UsbDevice dev, BENCHMARK_DEVICE_TEST_TYPE testType, int intf)
        {
            int transferred;
            byte[] dataBuffer = new byte[1];
            UsbSetupPacket setTestTypePacket =
                new UsbSetupPacket((byte) (UsbCtrlFlags.Direction_In | UsbCtrlFlags.Recipient_Device | UsbCtrlFlags.RequestType_Vendor),
                                   (byte)BENCHMARK_DEVICE_COMMANDS.SET_TEST,
                                   (short) testType,
                                   (short) intf,
                                   1);

            bool success = dev.ControlTransfer(ref setTestTypePacket, dataBuffer, dataBuffer.Length, out transferred);
            if (!success) return -1;

            return transferred;
        }
        public static bool GetTestType(UsbDevice usbTestDevice, out UsbTestType usbTestType, byte interfaceID)
        {
            if (ReferenceEquals(usbTestDevice, null))
                throw new UsbTestDeviceException("UsbTestDevice must be set before invoking this member!");

            int lengthTransferred;
            byte[] buf = new byte[1];

            UsbSetupPacket cmd = UsbCmdGetTestType;
            cmd.Index = interfaceID;

            bool bSuccess = usbTestDevice.ControlTransfer(ref cmd, buf, buf.Length, out lengthTransferred);

            if (bSuccess && lengthTransferred == 1)
            {
                usbTestType = (UsbTestType) buf[0];
                return true;
            }
            usbTestType = UsbTestType.Invalid;
            return false;
        }
        public static bool WriteEEDATA(UsbDevice usbTestDevice, byte address, byte value)
        {
            if (ReferenceEquals(usbTestDevice, null))
                throw new UsbTestDeviceException("UsbTestDevice must be set before invoking this member!");

            int lengthTransferred;
            byte[] buf = new byte[1];

            UsbSetupPacket cmd = UsbCmdReadEEDATA;
            cmd.Value = address;
            cmd.Index = value;
            bool bSuccess = usbTestDevice.ControlTransfer(ref cmd, buf, buf.Length, out lengthTransferred);

            return bSuccess && lengthTransferred == 1;
        }
        public static bool SetTestType(UsbDevice usbTestDevice, UsbTestType usbTestType, bool bCheck, byte interfaceID)
        {
            if (ReferenceEquals(usbTestDevice, null))
                throw new UsbTestDeviceException("UsbTestDevice must be set before invoking this member!");

            if (bCheck)
            {
                UsbTestType bCurrentTestType;
                if (GetTestType(usbTestDevice, out bCurrentTestType, interfaceID))
                {
                    if (bCurrentTestType == usbTestType) return true;
                }
                else
                    return false;
            }
            int lengthTransferred;
            byte[] buf = new byte[1];

            UsbSetupPacket cmd = UsbCmdSetTestType;
            cmd.Value = (short) usbTestType;
            cmd.Index = interfaceID;

            bool bSuccess = usbTestDevice.ControlTransfer(ref cmd, buf, buf.Length, out lengthTransferred);

            return bSuccess && lengthTransferred == 1;
        }
        public static Boolean sendControlMessage(UsbDevice device, byte requestCode, short index, string message)
        {
            Boolean r = false;

            short messageLength = 0;

            if (message != null)
            {
                messageLength = (short)(message.Length);
            }

            UsbSetupPacket setupPacket = new UsbSetupPacket();
            setupPacket.RequestType = (byte)((byte)UsbConstants.USB_DIR_OUT | (byte)UsbConstants.USB_TYPE_VENDOR);

            setupPacket.Request = requestCode; // byte
            setupPacket.Value = 0;
            setupPacket.Index = index; // short
            setupPacket.Length = messageLength; // short

            int resultTransferred;
            Console.WriteLine("RequestType:" + setupPacket.RequestType);
            Console.WriteLine("setupPacket.Request:" + setupPacket.Request);
            Console.WriteLine("setupPacket.Value:" + setupPacket.Value);
            Console.WriteLine("setupPacket.Index:" + setupPacket.Index);
            Console.WriteLine("setupPacket.Length:" + setupPacket.Length);

            Console.WriteLine("message:" + message);

            byte[] messageBytes = null;
            if (null != message)
            {
                messageBytes = Encoding.UTF8.GetBytes(message);
            }

            r = device.ControlTransfer(ref setupPacket, messageBytes, messageLength, out resultTransferred);

            return r;
        }
Exemple #7
0
        static void Main(string[] args)
        {
            ErrorCode errorCode = ErrorCode.None;

            try
            {
                UsbRegDeviceList regList = UsbDevice.AllDevices.FindAll(MyUsbFinder);
                MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

                // If the device is open and ready
                if (MyUsbDevice == null) throw new Exception("Device Not Found.");

                IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    // This is a "whole" USB device. Before it can be used,
                    // the desired configuration and interface must be selected.

                    // Select config #1
                    wholeUsbDevice.SetConfiguration(1);

                    // Claim interface #0.
                    wholeUsbDevice.ClaimInterface(0);
                }

                UsbSetupPacket packet = new UsbSetupPacket(
                    (byte)(UsbCtrlFlags.Direction_In | UsbCtrlFlags.Recipient_Device | UsbCtrlFlags.RequestType_Vendor),
                    (byte)0x12,
                    (short)0xc8,
                    (0x02 * 0x100) + 0x0a,
                    0);
                var buffer = new byte[] { 0x65, 0x0C, 0x02, 0xFF, 0x00, 0x00, 0x00, 0x00 };

                UsbInterfaceInfo usbInterfaceInfo = null;
                UsbEndpointInfo usbEndpointInfo = null;
                byte recipient = (byte)UsbRequestType.TypeVendor;
                var rubyPacket = new UsbSetupPacket(recipient, 0x0A, (byte)0x0c, 0x04, 0x01);

               // UsbEndpointBase.LookupEndpointInfo(MyUsbDevice.Configs[0], MyUsbDevice.ActiveEndpoints[0].EndpointInfo,
                 //                                  out usbInterfaceInfo, out usbEndpointInfo);
                int temp3;
                var RubyTransfer = MyUsbDevice.ControlTransfer(ref rubyPacket, null, 0, out temp3);
                // 0x21, 0x09, 0x0635, 0x000, "\x65\x0C#{data}\xFF\x00\x00\x00\x00", 0

                //int temp1;
                //var result1 = MyUsbDevice.ControlTransfer(ref packet, new byte[2], 8, out temp1);

                //packet = new UsbSetupPacket((byte)UsbRequestType.TypeVendor, (byte)0x12, (short)0x0C0A, (short)0x0200, 0x000);
                //int temp2;
                //var result2 = MyUsbDevice.ControlTransfer(ref packet, null, 0, out temp2);

                Console.WriteLine(string.Format("Result 1 : {0} - Length: {1}", RubyTransfer, temp3));
                //Console.WriteLine(string.Format("Result 2 : {0}", result2));

                //usb_control_msg(led->udev,
                //      usb_sndctrlpipe(led->udev, 0),
                //      0x12,
                //      0xc8,
                //      (0x02 * 0x100) + 0x0a,
                //      (0x00 * 0x100) + color,
                //      buffer,
                //      8,
                //      2 * HZ);

                // Then turn the LED on
                //Packet.Recipient = 8;
                //Packet.DeviceModel = 18;
                //Packet.Length = 0;
                //Packet.MajorCmd = 10;
                //Packet.MinorCmd = 12;
                ////switch (Color)
                ////{
                ////    case GREENLED: Packet.DataLSB = 1; break;
                ////    case REDLED: Packet.DataLSB = 2; break;
                ////    case BLUELED: Packet.DataLSB = 4; break;
                ////}
                //Packet.DataLSB = 4; // blue
                //Packet.DataMSB = 0;

            } catch(Exception e)
            {
                Console.WriteLine(e.Message);
            } finally
            {
                if (MyUsbDevice != null)
                {
                    if(endpointReader != null)
                        endpointReader.Abort();
                    if (MyUsbDevice.IsOpen)
                    {
                        // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                        // it exposes an IUsbDevice interface. If not (WinUSB) the
                        // 'wholeUsbDevice' variable will be null indicating this is
                        // an interface of a device; it does not require or support
                        // configuration and interface selection.
                        IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                        if (!ReferenceEquals(wholeUsbDevice, null))
                        {
                            // Release interface #0.
                            wholeUsbDevice.ReleaseInterface(0);
                        }

                        MyUsbDevice.Close();
                    }
                    MyUsbDevice = null;

                    // Free usb resources
                    UsbDevice.Exit();

                }
            }

            Console.ReadKey();
        }