Open() public abstract method

Opens the USB device for communucation.
public abstract Open ( UsbDevice &usbDevice ) : bool
usbDevice UsbDevice The newly created UsbDevice.
return bool
Esempio n. 1
0
        private void addDevice(UsbRegistry deviceReg, string display)
        {
            if (!deviceReg.Open(out mUsbDevice)) return;
            mUsbRegistry = deviceReg;

            TreeNode tvDevice = tvInfo.Nodes.Add(display);
            string[] sDeviceAdd = mUsbDevice.Info.ToString().Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in sDeviceAdd)
                tvDevice.Nodes.Add(s);

            foreach (UsbConfigInfo cfgInfo in mUsbDevice.Configs)
            {
                TreeNode tvConfig = tvDevice.Nodes.Add("Config " + cfgInfo.Descriptor.ConfigID);
                string[] sCfgAdd = cfgInfo.ToString().Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in sCfgAdd)
                    tvConfig.Nodes.Add(s);

                TreeNode tvInterfaces = tvConfig; //.Nodes.Add("Interfaces");
                foreach (UsbInterfaceInfo interfaceInfo in cfgInfo.InterfaceInfoList)
                {
                    TreeNode tvInterface =
                        tvInterfaces.Nodes.Add("Interface [" + interfaceInfo.Descriptor.InterfaceID + "," + interfaceInfo.Descriptor.AlternateID + "]");
                    string[] sInterfaceAdd = interfaceInfo.ToString().Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string s in sInterfaceAdd)
                        tvInterface.Nodes.Add(s);

                    TreeNode tvEndpoints = tvInterface; //.Nodes.Add("Endpoints");
                    foreach (UsbEndpointInfo endpointInfo in interfaceInfo.EndpointInfoList)
                    {
                        TreeNode tvEndpoint = tvEndpoints.Nodes.Add("Endpoint 0x" + (endpointInfo.Descriptor.EndpointID).ToString("X2"));
                        string[] sEndpointAdd = endpointInfo.ToString().Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string s in sEndpointAdd)
                            tvEndpoint.Nodes.Add(s);
                    }
                }
            }
            mUsbDevice.Close();
        }
Esempio n. 2
0
        private static StringBuilder getDescriptorReport(UsbRegistry usbRegistry)
        {
            UsbDevice usbDevice;
            StringBuilder sbReport = new StringBuilder();

            if (!usbRegistry.Open(out usbDevice)) return sbReport;

            sbReport.AppendLine(string.Format("{0} OSVersion:{1} LibUsbDotNet Version:{2} DriverMode:{3}", usbRegistry.FullName, UsbDevice.OSVersion, LibUsbDotNetVersion, usbDevice.DriverMode));
            sbReport.AppendLine(usbDevice.Info.ToString("",UsbDescriptor.ToStringParamValueSeperator,UsbDescriptor.ToStringFieldSeperator));
            foreach (UsbConfigInfo cfgInfo in usbDevice.Configs)
            {
                sbReport.AppendLine(string.Format("CONFIG #{1}\r\n{0}", cfgInfo.ToString("", UsbDescriptor.ToStringParamValueSeperator, UsbDescriptor.ToStringFieldSeperator), cfgInfo.Descriptor.ConfigID));
                foreach (UsbInterfaceInfo interfaceInfo in cfgInfo.InterfaceInfoList)
                {
                    sbReport.AppendLine(string.Format("INTERFACE ({1},{2})\r\n{0}", interfaceInfo.ToString("", UsbDescriptor.ToStringParamValueSeperator, UsbDescriptor.ToStringFieldSeperator), interfaceInfo.Descriptor.InterfaceID, interfaceInfo.Descriptor.AlternateID));

                    foreach (UsbEndpointInfo endpointInfo in interfaceInfo.EndpointInfoList)
                    {
                        sbReport.AppendLine(string.Format("ENDPOINT 0x{1:X2}\r\n{0}", endpointInfo.ToString("", UsbDescriptor.ToStringParamValueSeperator, UsbDescriptor.ToStringFieldSeperator), endpointInfo.Descriptor.EndpointID));
                    }
                }
            }
            usbDevice.Close();

            return sbReport;
        }
Esempio n. 3
0
        private bool openAsTestDevice(UsbRegistry usbRegistry)
        {
            if (!ReferenceEquals(mUsbDevice, null))
                closeTestDevice(mUsbDevice);

            mUsbDevice = null;

            try
            {
                if (usbRegistry.Open(out mUsbDevice))
                {
                    UsbInterfaceInfo readInterfaceInfo;
                    UsbInterfaceInfo writeInterfaceInfo;

                    UsbDevice.UsbErrorEvent += OnUsbError;

                    if (!UsbEndpointBase.LookupEndpointInfo(mUsbDevice.Configs[0],
                                                       0x80,
                                                       out readInterfaceInfo,
                                                       out mReadEndpointInfo))
                    {
                        throw new Exception("failed locating read endpoint.");
                    }

                    mBenchMarkParameters.BufferSize -= (mBenchMarkParameters.BufferSize%(mReadEndpointInfo.Descriptor.MaxPacketSize));

                    if (!UsbEndpointBase.LookupEndpointInfo(mUsbDevice.Configs[0],
                                                       0x00,
                                                       out writeInterfaceInfo,
                                                       out mWriteEndpointInfo))
                    {
                        throw new Exception("failed locating write endpoint.");
                    }

                    if (((mWriteEndpointInfo.Descriptor.Attributes & 3)==(int) EndpointType.Isochronous) ||
                        ((mReadEndpointInfo.Descriptor.Attributes & 3)==(int) EndpointType.Isochronous))
                    {
                        throw new Exception("buenchmark GUI application does not support ISO endpoints. Use BenchmarkCon instead.");
                    }

                    mBenchMarkParameters.BufferSize -= (mBenchMarkParameters.BufferSize%(mWriteEndpointInfo.Descriptor.MaxPacketSize));

                    if (writeInterfaceInfo.Descriptor.InterfaceID != readInterfaceInfo.Descriptor.InterfaceID)
                        throw new Exception("read/write endpoints must be on the same interface.");

                    mEP1Reader = mUsbDevice.OpenEndpointReader(
                        (ReadEndpointID)mReadEndpointInfo.Descriptor.EndpointID,
                        mBenchMarkParameters.BufferSize,
                        (EndpointType)(mReadEndpointInfo.Descriptor.Attributes & 3));

                    mEP1Writer = mUsbDevice.OpenEndpointWriter(
                        (WriteEndpointID) mWriteEndpointInfo.Descriptor.EndpointID,
                        (EndpointType) (mWriteEndpointInfo.Descriptor.Attributes & 3));

                    mInterfaceInfo = writeInterfaceInfo;

                    mEP1Reader.ReadThreadPriority = mBenchMarkParameters.Priority;
                    mEP1Reader.DataReceived += OnDataReceived;

                    makeTestBytes(out loopTestBytes, mBenchMarkParameters.BufferSize);

                    // If this is a "whole" usb device (libusb-win32, linux libusb)
                    // it will have an IUsbDevice interface. If not (WinUSB) the
                    // variable will be null indicating this is an interface of a
                    // device.
                    IUsbDevice wholeUsbDevice = mUsbDevice 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(mInterfaceInfo.Descriptor.InterfaceID);
                    }
                    return true;
                }
            }
            catch(Exception ex)
            {
                SetStatus(ex.Message, true);
            }

            if (!ReferenceEquals(mUsbDevice,null))
            {
                try
                {
                    closeTestDevice(mUsbDevice);
                }
                finally
                {
                    mUsbDevice = null;
                    mEP1Reader = null;
                    mEP1Writer = null;
                }
            }
            return false;
        }