public bool Open() { if (IsOpen) { return(false); } if (!_Device.Open()) { return(false); } IUsbDevice whole = _Device as IUsbDevice; if (!ReferenceEquals(whole, null)) { if (!whole.SetConfiguration(ConfigurationID) || !whole.ClaimInterface(InterfaceID)) { _Device.Close(); throw new PTPException("could not set USB device configuration and interface to " + ConfigurationID + " and " + InterfaceID + ", respectively"); } } Writer = _Device.OpenEndpointWriter(WriterEndpointID); Reader = _Device.OpenEndpointReader(ReaderEndpointID); return(true); }
public Brazo() { dev = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(VENDOR_ID, PRODUCT_ID)); if (dev == null) { throw new Exception("Brazo no encontrado."); } try { IUsbDevice wholeUsbDevice = dev 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); } // open device with configuration 1, interface 0 and no alt interface } catch (Exception e) { Console.WriteLine(e); throw; } }
/// <summary> /// Performs initialisation tasks required before enabling printing. /// </summary> /// <exception cref="DC.PrintingServices.PrinterNotFoundException">No printer can be found with the specified vendor id and product id.</exception> public void Initialise() { // Create a USB device finder for finding the printer. _deviceFinder = new UsbDeviceFinder(this.VendorID, this.ProductID); // Find and open a connection to the printer. _printer = UsbDevice.OpenUsbDevice(_deviceFinder); _printerUSBDevice = _printer as IUsbDevice; if (_printerUSBDevice != null) { // ANDREW LING [28 Feburary 2014]: Make the configuration and interface settings configurable. _printerUSBDevice.SetConfiguration(1); _printerUSBDevice.ClaimInterface(0); } else { // The printer cannot be located. // ANDREW LING [28 February 2014]: TODO: Log when the printer cannot be located. // ANDREW LING [28 February 2014]: TODO: Display an error message on screen when the printer cannot be located. throw new PrinterNotFoundException(this.VendorID, this.ProductID); } // ANDREW LING [28 February 2014]: TODO: Make the write end point ID configurable. _writer = _printerUSBDevice.OpenEndpointWriter(WriteEndpointID.Ep02); // Mark that the initialisation tasks have been completed. _initialised = true; }
// Connect to the arm public bool Connect() { // Vendor ID/Product ID here UsbDeviceFinder USBFinder = new UsbDeviceFinder(0x1267, 0x0); // Try to open the device RobotArm = UsbDevice.OpenUsbDevice(USBFinder); // Did we connect OK? if ((RobotArm == null)) { return(false); } // 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 = RobotArm 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); } // Connected and have interface to the arm return(true); }
public bool Connect() { bool rv = false; rv = false; Disconnect(); MyUSBDeviceFinder = new UsbDeviceFinder(DevVID, DevPID, DevRevision, DevSerialNumber, DevGUID); MyUSBDevice = UsbDevice.OpenUsbDevice(MyUSBDeviceFinder); if (MyUSBDevice != null) { WholeUSBDevice = MyUSBDevice as IUsbDevice; if (!ReferenceEquals(WholeUSBDevice, null)) { WholeUSBDevice.SetConfiguration(1); WholeUSBDevice.ClaimInterface(0); } MyUSBWriter = MyUSBDevice.OpenEndpointWriter(WriteEndpointID.Ep01); MyUSBReader = MyUSBDevice.OpenEndpointReader(ReadEndpointID.Ep01, 4096); MyUSBReader.DataReceived += MyUSBReader_DataReceived; MyUSBReader.DataReceivedEnabled = true; MyUSBSetupPacket.RequestType = (byte)(UsbCtrlFlags.RequestType_Vendor | UsbCtrlFlags.Recipient_Interface | UsbCtrlFlags.Direction_In); MyUSBSetupPacket.Request = 1; MyUSBSetupPacket.Value = 0; MyUSBSetupPacket.Length = 4; bkThread = new System.Threading.Thread(this.Th_DoWork); bkThExit = false; bkThread.Start(); rv = true; } return(rv); }
public override bool Open() { try { UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(((UsbConfigModel)Configuration).PID, ((UsbConfigModel)Configuration).VID); UsbRegistry myUsbRegistry = UsbDevice.AllWinUsbDevices.Find(MyUsbFinder); if (myUsbRegistry is null) { return(false); } // Open this usb device. _dev = UsbDevice.OpenUsbDevice(MyUsbFinder) as IUsbDevice; _dev.SetConfiguration(1); _dev.ClaimInterface(0); _writer = _dev.OpenEndpointWriter(WriteEndpointID.Ep01); _reader = _dev.OpenEndpointReader(ReadEndpointID.Ep01); } catch (Exception exp) { throw new Exception(exp.ToString()); } return(true); }
private static void InitDevice() { MyUsbFinder = new UsbDeviceFinder(0x045E, 0x02B0); MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); // If the device is open and ready if (MyUsbDevice == null) { throw new Exception("Device Not Found."); } // 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 = 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); } }
/// <summary> /// Initializes the device via LibUSB, and sets the refresh interval of the controller data /// </summary> /// <param name="Interval">The interval that the device is polled for information.</param> public void Init(int Interval) { //ErrorCode ec = ErrorCode.None; // Find and open the usb device. MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); if (MyUsbDevice == null) { throw new Exception("Device Not Found."); } IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { wholeUsbDevice.SetConfiguration(1); wholeUsbDevice.ClaimInterface(0); } reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02); writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01); ButtonMasks.InitializeMasks(); SetPollingInterval(Interval); pollTimer.Elapsed += new ElapsedEventHandler(pollTimer_Elapsed); pollTimer.Start(); TestLEDs(); RefreshLEDState(); }
private void USB_init() { 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); } reader1 = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); writer1 = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01); // Setup trigger USB_receive = new Thread(USBReceive); // open read endpoint 1. USB_receive.Start(); button1Enable(true); }
public bool OpenDevice() { MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); if (MyUsbDevice == null) { return(false); } #region 当设备是一个 "whole" USB 时,我们的设备一般不会是此情况 // 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 = 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); } #endregion reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02); reader.ReadBufferSize = nReadBufSize; writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01); return(true); }
/// <summary> /// Open connection to the device /// </summary> public void OpenDevice() { MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); if (MyUsbDevice == null) { throw new Exception("Device Not Found."); } // 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. 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); } // open read endpoint 1. reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); // open write endpoint 1. writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02); }
public void Open() { _device = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(_vendorId, _productId)); if (_device != null) { IUsbDevice whole = _device as IUsbDevice; if (!ReferenceEquals(whole, null)) { whole.SetConfiguration(1); whole.ClaimInterface(1); } //Set up the endpoints var hci = _device.OpenEndpointReader(ReadEndpointID.Ep01); _reader = _device.OpenEndpointReader(ReadEndpointID.Ep02); _writer = _device.OpenEndpointWriter(WriteEndpointID.Ep02); _isoReader = _device.OpenEndpointReader(ReadEndpointID.Ep03); _isoWriter = _device.OpenEndpointWriter(WriteEndpointID.Ep03); //Set up our read callback(s) hci.DataReceived += hci_DataReceived; hci.DataReceivedEnabled = true; _reader.DataReceived += reader_DataReceived; _reader.DataReceivedEnabled = true; _isoReader.DataReceived += _isoReader_DataReceived; _isoReader.DataReceivedEnabled = true; //Reset the device Reset(); } }
void Setup() { if (UsbDev != null) { return; } UsbDev = FindDevice(); if (!UsbDev.SetConfiguration(1)) { throw new M4ATXException("Failed to set device config"); } if (!UsbDev.ClaimInterface(0)) { throw new M4ATXException("Failed to claim interface #0"); } if (!UsbDev.SetAltInterface(0)) { throw new M4ATXException("Failed to set alternate interface to 0"); } Writer = UsbDev.OpenEndpointWriter(WriteEndpointID.Ep01); Reader = UsbDev.OpenEndpointReader(ReadEndpointID.Ep01); }
public static void InitializeUSBDevice() { // Use the USB device finder to find our Gamecube adapter _usbDevice = UsbDevice.OpenUsbDevice(_usbDeviceFinder); if (_usbDevice == null) { // If the gamecube adapter isn't plugged in OR is being used by another application, throw an exception. // Is there even a way to take control over the adapter from another device? throw new Exception(Strings.ERROR_ADAPTERNOTFOUND); } IUsbDevice wholeUsbDevice = _usbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { wholeUsbDevice.SetConfiguration(1); // Set the adapter to use config one wholeUsbDevice.ClaimInterface(0); // Claim interface zero } // Set reader to read from endpoint 1 (apparently GCN adapters use endpoint 0x81, but this works?) _controllerReader = _usbDevice.OpenEndpointReader(ReadEndpointID.Ep01); // Write to endpoint 2 _controllerWriter = _usbDevice.OpenEndpointWriter(WriteEndpointID.Ep02); // begin polling command - https://gbatemp.net/threads/wii-u-gamecube-adapter-reverse-engineering-cont.388169/ _controllerWriter.Write(Convert.ToByte(0x13), 5000, out int transferLength); }
public MainWindow() { InitializeComponent(); ErrorCode ec = ErrorCode.None; try { MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); if (MyUsbDevice == null) { MessageBox.Show("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); } UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); reader.DataReceived += (OnRxEndPointData); reader.DataReceivedEnabled = true; } catch (Exception) { throw; } }
/// <summary> /// Attempts to open a USB registry as a USB DFU device. /// </summary> /// <param name="registry">The input USB registry of a connected device</param> /// <param name="dfuDevice">The opened DFU device in case of success</param> /// <returns>True if the DFU device is successfully opened</returns> public static bool TryOpen(UsbRegistry registry, out Device dfuDevice) { dfuDevice = null; UsbDevice dev; byte cfIndex = 0; byte ifIndex = 0; if (!registry.Open(out dev)) { return(false); } var confInfo = dev.Configs[cfIndex]; // This is a "whole" USB device. Before it can be used, // the desired configuration and interface must be selected. IUsbDevice usbDevice = dev as IUsbDevice; if (usbDevice != null) { // Select config usbDevice.SetConfiguration(confInfo.Descriptor.ConfigID); } // find DFU interface for (ifIndex = 0; ifIndex < confInfo.InterfaceInfoList.Count; ifIndex++) { var iface = confInfo.InterfaceInfoList[ifIndex]; if (!IsDfuInterface(iface)) { continue; } if (usbDevice != null) { // Claim interface usbDevice.ClaimInterface(iface.Descriptor.InterfaceID); } break; } try { if (ifIndex == confInfo.InterfaceInfoList.Count) { throw new ArgumentException("The device doesn't have valid DFU interface"); } dfuDevice = new Device(dev, cfIndex, ifIndex); return(true); } catch (Exception) { var d = dev as IDisposable; d.Dispose(); return(false); } }
public static void Connect(DeviceNameList dList) { try { if ((uDevice = UsbDevice.OpenUsbDevice(uDevFinder)) == null) { throw new Exception("Device Not Found"); } IUsbDevice iuDev = uDevice as IUsbDevice; if (!ReferenceEquals(iuDev, null)) { iuDev.SetConfiguration(1); iuDev.ClaimInterface(0); } uDevice.Open(); } catch (Exception e) { Console.WriteLine("usbcom Connecting Proces : {0}", e); throw; } (new Thread(() => { while (uDevice.IsOpen) { byte[] buf = new byte[64]; int bytesRead = 0; using (var Reader = uDevice.OpenEndpointReader(ReadEndpointID.Ep01)) { ErrorCode ec = ErrorCode.None; ec = Reader.Read(buf, 2000, out bytesRead); if (ec == ErrorCode.IoTimedOut) { continue; } if (ec == ErrorCode.IoCancelled) { return; } if (ec != 0) { throw new Exception("usbcom ReadCtrl Error : ErrorCode==" + ec.ToString() + "\n" + UsbDevice.LastErrorString); } } if (bytesRead > 0) { (new Thread(() => DataGot?.Invoke(null, new DataGotEvArgs() { Data = buf }))).Start(); } } })).Start(); }
public override void OpenDevice() { log.Info("QDLUsb trying to find device"); UsbDevice.UsbErrorEvent += new EventHandler <UsbError>(UsbErrorEvent); UsbRegistry regDev = null; if (Environment.OSVersion.Platform == PlatformID.Win32NT) { regDev = UsbDevice.AllWinUsbDevices.Find((reg) => reg.Vid == VID && reg.Pid == PID); } else { regDev = UsbDevice.AllDevices.Find((reg) => reg.Vid == VID && reg.Pid == PID); } if (regDev == null) { log.Error("No QDLUSB Devices found"); throw new QDLDeviceNotFoundException("Unable to find device"); } if (!regDev.Open(out device) || device == null) { log.Error("No QDLUSB Devices found"); throw new QDLDeviceNotFoundException("Unable to open device"); } if (UsbDevice.IsLinux) { log.Debug("Running on linux, detaching kernel driver"); MonoUsbDevice monodev = device as MonoUsbDevice; if (!monodev.DetachKernelDriver()) { log.Error("Failed to detach kernel driver"); throw new Exception("Failed to detach kernel driver"); } } IUsbDevice wholeUsbDevice = device as IUsbDevice; if (wholeUsbDevice != null) { wholeUsbDevice.SetConfiguration(1); wholeUsbDevice.ClaimInterface(0); } reader = device.OpenEndpointReader(ReadEndpointID.Ep01); writer = device.OpenEndpointWriter(WriteEndpointID.Ep01, EndpointType.Bulk); if (reader == null || writer == null) { device.Close(); device = null; UsbDevice.Exit(); log.Error("Failed to open endpoints"); throw new Exception("Unable to open endpoints"); } log.Info("Found QDLUSB device"); }
public void usbConnect() // The name is self-explanatory... { // Find and open the usb device. mUsbDevice = UsbDevice.OpenUsbDevice(mini2440Finder); if (mUsbDevice == null) { mUsbDevice = UsbDevice.OpenUsbDevice(mini6410Finder); } // If we fail to find or connect to the device if (mUsbDevice == null) { l_usbFound.Text = "Error"; t_log.AppendText("\r\n" + "## Could not find or open device." + "\r\n" + "Please plug the device, I will detect it." + "\r\n"); b_download.Enabled = false; b_upload.Enabled = false; } // If the device is open and ready else { l_usbFound.Text = "Connected"; if (mUsbDevice.UsbRegistryInfo.Pid == pid2440) { t_log.AppendText("\r\n" + "## Mini2440 connected." + "\r\n"); } if (mUsbDevice.UsbRegistryInfo.Pid == pid6410) { t_log.AppendText("\r\n" + "## Mini6410 connected." + "\r\n"); } b_download.Enabled = true; b_upload.Enabled = true; mEpReader = mUsbDevice.OpenEndpointReader((ReadEndpointID)(byte.Parse("1") | 0x80)); // = 81 mEpWriter = mUsbDevice.OpenEndpointWriter((WriteEndpointID)byte.Parse("3")); if (mEpWriter.EndpointInfo == null) // Try opening endpoint 4 if endpoint 3 is unavailable (i.e. after usb reset when downloading firmware) { mEpWriter = mUsbDevice.OpenEndpointWriter((WriteEndpointID)byte.Parse("4")); } //mEpReader.Flush(); mEpWriter.Flush(); 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(0); } } }
private void btnPrint_Click(object sender, EventArgs e) { _printer = UsbDevice.OpenUsbDevice(_printerFinder); _printerUsbDevice = _printer as IUsbDevice; if (_printerUsbDevice != null) { // This is a "whole" USB device. Before it can be used, // the desired configuration and interface must be selected. // Select config #1 _printerUsbDevice.SetConfiguration(1); // Claim interface #0. _printerUsbDevice.ClaimInterface(0); } // open write endpoint 1. UsbEndpointWriter writer = _printer.OpenEndpointWriter(WriteEndpointID.Ep02); List <byte> writeBytesList = new List <byte>(); writeBytesList.AddRange(Encoding.ASCII.GetBytes(txtTextToPrint.Text)); writeBytesList.Add(0x0A); byte[] writeBuffer = writeBytesList.ToArray(); int bytesWritten; ErrorCode writeErrorCode = writer.Write(writeBuffer, 10000, out bytesWritten); // open read endpoint 1. //UsbEndpointReader reader = printer.OpenEndpointReader(ReadEndpointID.Ep01); //ErrorCode readErrorCode = ErrorCode.None; //byte[] readBuffer = new byte[1024]; //while (readErrorCode == ErrorCode.None) //{ // int bytesRead; // // If the device hasn't sent data in the last 100 milliseconds, // // a timeout error (ec = IoTimedOut) will occur. // readErrorCode = reader.Read(readBuffer, 10000, out bytesRead); // if (bytesRead == 0) // { // break; // } // // Write that output to the console. // MessageBox.Show(Encoding.ASCII.GetString(readBuffer, 0, bytesRead)); //} }
private void InitUSB(int VendorID, int ProductID) { dev = (IUsbDevice)UsbDevice.OpenUsbDevice(x => x.Vid == VendorID && x.Pid == ProductID); if (dev == null) { throw new NullReferenceException("Cannot find USB device for JZ4780. Check Vendor/Product IDs and that LibUSB-Win32 \".inf\" installed."); } dev.SetConfiguration(1); }
public bool Connect() { _usbRegistry.Open(out _usbDevice); if (_usbDevice == null) { return(false); } // 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 = _usbDevice 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. if (!wholeUsbDevice.ClaimInterface(0)) { wholeUsbDevice.ReleaseInterface(0); _usbDevice.Close(); return(false); } } if (_resolverInfo.V45Endpoints == true) { // Open EP1 for writing, used to send BCP commands/packets to the unit. _endpointWriter = _usbDevice.OpenEndpointWriter(WriteEndpointID.Ep01); // Open EP2 for reading, used to send BCP replies to the USB host. _endpointReader = _usbDevice.OpenEndpointReader(ReadEndpointID.Ep02); // Open EP5 for reading, used to send “real time capture data” to the host. _realTimeEndpointReader = _usbDevice.OpenEndpointReader(ReadEndpointID.Ep05); } else { // Open EP2 for writing, used to send BCP commands/packets to the unit. _endpointWriter = _usbDevice.OpenEndpointWriter(WriteEndpointID.Ep02); // Open EP6 for reading, used to send BCP replies to the USB host. _endpointReader = _usbDevice.OpenEndpointReader(ReadEndpointID.Ep06); // Open EP8 for reading, used to send “real time capture data” to the host. _realTimeEndpointReader = _usbDevice.OpenEndpointReader(ReadEndpointID.Ep08); } return(true); }
private void buttonRead_Click(object sender, EventArgs e) { dev = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(0x1915, 0x7B)); // bool success = reg.Open(out dev); IUsbDevice wholeUsbDevice = dev as IUsbDevice; wholeUsbDevice.SetConfiguration(1); wholeUsbDevice.ClaimInterface(0); rdr = wholeUsbDevice.OpenEndpointReader((ReadEndpointID)0x81); rdr.Flush(); Read(); }
public static void open() { ErrorCode ec = ErrorCode.None; try { MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); if (MyUsbDevice == null) { throw new Exception("Device Not Found."); } IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { wholeUsbDevice.SetConfiguration(1); wholeUsbDevice.ClaimInterface(0); } UsbEndpointWriter writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01); byte[] bytesToSend = { 0x1b, 0x70, 0x00, 0x19, 0xff }; int bytesWritten; ec = writer.Write(bytesToSend, 2000, out bytesWritten); if (ec != ErrorCode.None) { throw new Exception(UsbDevice.LastErrorString); } } catch (Exception ex) { Console.WriteLine(); Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message); } finally { if (MyUsbDevice != null) { if (MyUsbDevice.IsOpen) { IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // Release interface #0. wholeUsbDevice.ReleaseInterface(0); } MyUsbDevice.Close(); } MyUsbDevice = null; UsbDevice.Exit(); } } }
private static void SetConfiguration(IUsbDevice wholeUsbDevice) { 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); } }
// USB connection and adapter management public override void Open() { this.Close(); try { caLibUsbAdapter.write_lock.WaitOne(); usb_finder = new UsbDeviceFinder(this.vid, this.pid); Debug.Assert(this.usb_finder != null); // open device usb_device = UsbDevice.OpenUsbDevice(usb_finder); if (usb_device == null) { throw new Exception("No compatible adapters found"); } wholeUsbDevice = usb_device as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { wholeUsbDevice.SetConfiguration(1); wholeUsbDevice.ClaimInterface(1); } else { throw new Exception("Failed to claim interface"); } // open endpoints ep_reader = usb_device.OpenEndpointReader(this.read_ep_id); ep_writer = usb_device.OpenEndpointWriter(this.write_ep_id); if (ep_reader == null || ep_writer == null) { throw new Exception("Failed to open endpoints"); } // clear garbage from input this.ep_reader.ReadFlush(); // attach read event ep_reader.DataReceived += (read_usb); ep_reader.DataReceivedEnabled = true; } catch (Exception e) { this.Close(); throw e; } finally { caLibUsbAdapter.write_lock.ReleaseMutex(); } }
private void _Init(int vendorId, int productId, int interfaceNumber) { _readers = new Dictionary <int, UsbEndpointReader>(); _writers = new Dictionary <int, UsbEndpointWriter>(); _forwardee = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(vendorId, productId)) as IUsbDevice; if (_forwardee == null) { throw new InvalidOperationException("Device not found"); } _forwardee.ClaimInterface(interfaceNumber); _forwardee.SetConfiguration(1); }
public GarminUnit(UsbDevice Device) { Configuration = new Dictionary <string, ushort>(); IUsbDevice wholedevice = Device as IUsbDevice; if (!ReferenceEquals(wholedevice, null)) { wholedevice.SetConfiguration(1); wholedevice.ClaimInterface(0); } Reader = Device.OpenEndpointReader(ReadEndpointID.Ep01); Writer = Device.OpenEndpointWriter(WriteEndpointID.Ep02); }
internal void OpenDevice(UsbRegistry registry) { this._device = registry.Device; IUsbDevice wholeUsbDevice = _device as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // Select config #1 wholeUsbDevice.SetConfiguration(1); // Claim interface #0. wholeUsbDevice.ClaimInterface(0); } }
private void _Init(bool throwErrors) { Dispose(); _device = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(_VENDOR_ID, _PRODUCT_ID1)) as IUsbDevice; if (_device == null) { _device = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(_VENDOR_ID, _PRODUCT_ID2)) as IUsbDevice; } if (_device == null) { _device = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(_VENDOR_ID, _PRODUCT_ID3)) as IUsbDevice; } if (_device == null) { if (throwErrors) { throw new InvalidOperationException("Wireless receiver not found"); } } else { _device.ClaimInterface(1); _device.SetConfiguration(1); _responses = new Dictionary <int, List <byte[]> >(); var now = DateTime.Now; for (int i = 0; i < _NUMBER_OF_SLOTS; i++) { var slot = new ReceiverSlot(); slot.DataWriter = _device.OpenEndpointWriter(_GetWriteEndpoint((i * 2) + 1)); slot.DataReader = _device.OpenEndpointReader(_GetReadEndpoint((i * 2) + 1)); slot.DataReader.DataReceived += DataReader_DataReceived; slot.DataReader.DataReceivedEnabled = true; slot.HeadsetWriter = _device.OpenEndpointWriter(_GetWriteEndpoint((i * 2) + 2)); slot.HeadsetReader = _device.OpenEndpointReader(_GetReadEndpoint((i * 2) + 2)); slot.HeadsetReader.DataReceived += HeadsetReader_DataReceived; slot.HeadsetReader.DataReceivedEnabled = true; _slots.Add(i + 1, slot); _RefreshSlot(i + 1); } _refreshSlotsThread = new Thread(new ThreadStart(_RefreshSlots)); _refreshSlotsThread.IsBackground = true; _refreshSlotsThread.Start(); } }
public Form1() { InitializeComponent(); // Hook the device notifier event UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent; usb_command = new byte[64]; try { MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); if (MyUsbDevice == null) { //throw new Exception("Device Not Found."); connected = false; } else { Device_l.Text = "Device: Connected"; connected = true; Scan_b.Enabled = true; 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); } //MessageBox.Show(ReadEndpointID.Ep04.ToString()); reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01); mode = 4; backgroundWorker1.RunWorkerAsync(); } } catch (Exception ex) { MessageBox.Show((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message); } }
private void button2_Click(object sender, EventArgs e) { try { MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); if (MyUsbDevice == null) { throw new Exception("Device Not Found."); //connected = false; } else { Device_l.Text = "Device: Connected"; //connected = true; Scan_b.Enabled = true; 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); } reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep03); writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep04); } } catch (Exception ex) { MessageBox.Show((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message); } }
public void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e) { // A Device system-level event has occured //Console.SetCursorPosition(0, Console.CursorTop); //MessageBox.Show(e.Device.IdVendor.ToString()); if (e.EventType == EventType.DeviceArrival && e.Device.IdVendor == 0x16C0 && e.Device.IdProduct == 0x05DD) { try { MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); if (MyUsbDevice == null) { //throw new Exception("Device Not Found."); connected = false; } else { Device_l.Text = "Device: Connected"; connected = true; Scan_b.Enabled = true; 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); } //MessageBox.Show(ReadEndpointID.Ep04.ToString()); reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01); mode = 4; backgroundWorker1.RunWorkerAsync(); } } catch (Exception ex) { MessageBox.Show((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message); } } if (e.EventType == EventType.DeviceRemoveComplete && e.Device.IdVendor == 0x16C0 && e.Device.IdProduct == 0x05DD) { timer1.Enabled = false; connected = false; if (MyUsbDevice != null) { 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(); Device_l.Text = "Device: Not Connected"; Scan_b.Enabled = false; DumpRAM_b.Enabled = false; DumpROM_b.Enabled = false; WriteRAM_b.Enabled = false; Banks_l.Text = "Banks: "; MBC_l.Text = "MBC: "; RAM_l.Text = "RAM Size: "; Size_l.Text = "Size:"; Title_l.Text = "Title:"; } } // Console.WriteLine(e.ToString()); // Dump the event info to output. //Console.WriteLine(); //Console.Write("[Press any key to exit]"); }
private void _Init(int vendorId, int productId, int interfaceNumber) { _readers = new Dictionary<int, UsbEndpointReader>(); _writers = new Dictionary<int, UsbEndpointWriter>(); _forwardee = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(vendorId, productId)) as IUsbDevice; if (_forwardee == null) throw new InvalidOperationException("Device not found"); _forwardee.ClaimInterface(interfaceNumber); _forwardee.SetConfiguration(1); }
public void Start() { lock (_lock) { UsbRegDeviceList allLibUsbDevices = UsbDevice.AllLibUsbDevices; if (allLibUsbDevices.Count > 0) { UsbDevice dev; IsOpen = ((LibUsbRegistry)allLibUsbDevices.First()).Open(out dev); _device = dev as IUsbDevice; // Select config bool configuration = _device.SetConfiguration(1); // Claim interface bool claimInterface = _device.ClaimInterface(0); /*bool found = false; byte readerId = (byte) ReadEndpointID.Ep01; while (!found && (byte)readerId <= (byte)ReadEndpointID.Ep15) { _reader = _device.OpenEndpointReader((ReadEndpointID)readerId); byte[] buffer = new byte[1024]; int length; ErrorCode ret = _reader.Read(buffer, 100, out length); found = (ret != ErrorCode.Win32Error); readerId++; }*/ EndPointId = GetEndpoint(); _reader = _device.OpenEndpointReader(EndPointId); _thread.Start(); //_reader.DataReceivedEnabled = true; //_reader.ReadBufferSize = 8; //_reader.DataReceived += OnReaderDataReceived; } } }
private void InitUSB(int VendorID, int ProductID) { dev = (IUsbDevice)UsbDevice.OpenUsbDevice(x => x.Vid == VendorID && x.Pid == ProductID); if (dev == null) { throw new NullReferenceException("Cannot find USB device for JZ4780. Check Vendor/Product IDs and that LibUSB-Win32 \".inf\" installed."); } dev.SetConfiguration(1); }
public void Open() { if (_deviceReg.Open(out _device) == false) { throw new InvalidOperationException("Unable to open the dongle, was it removed?"); } _writer = _device.OpenEndpointWriter(WriteEndpointID.Ep01, EndpointType.Bulk); _reader = _device.OpenEndpointReader(ReadEndpointID.Ep01, 32, EndpointType.Bulk); _iDevice = _device as IUsbDevice; if (_iDevice != null) { _iDevice.SetConfiguration(1); _iDevice.ClaimInterface(0); } Setup(); }
// USB connection and adapter management public override void Open() { this.Close(); try { caLibUsbAdapter.write_lock.WaitOne(); usb_finder = new UsbDeviceFinder(this.vid, this.pid); Debug.Assert(this.usb_finder != null); // open device usb_device = UsbDevice.OpenUsbDevice(usb_finder); if (usb_device == null) { throw new Exception("No compatible adapters found"); } wholeUsbDevice = usb_device as IUsbDevice; if (!ReferenceEquals (wholeUsbDevice, null)) { wholeUsbDevice.SetConfiguration(1); wholeUsbDevice.ClaimInterface(1); } else { throw new Exception("Failed to claim interface"); } // open endpoints ep_reader = usb_device.OpenEndpointReader(this.read_ep_id); ep_writer = usb_device.OpenEndpointWriter(this.write_ep_id); if(ep_reader == null || ep_writer == null) { throw new Exception("Failed to open endpoints"); } // clear garbage from input this.ep_reader.ReadFlush(); // attach read event ep_reader.DataReceived += (read_usb); ep_reader.DataReceivedEnabled = true; } catch (Exception e) { this.Close(); throw e; } finally { caLibUsbAdapter.write_lock.ReleaseMutex(); } }