/// <summary> /// Open the connection /// </summary> /// <returns>whether the connection was opened successfully</returns> public async Task<bool> OpenAsync() { dev = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(0x10C4, 0xEAC9)); if (dev == null) { dev = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(0x10C4, 0xEACA)); if (dev == null) return false; } 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); } writer = dev.OpenEndpointWriter(WriteEndpointID.Ep01, EndpointType.Interrupt); reader = dev.OpenEndpointReader(ReadEndpointID.Ep01, 64, EndpointType.Interrupt); return true; }
/// <summary> /// Class constructor for the traq|paq /// </summary> public TraqpaqDevice() { // find the device traqpaqDeviceFinder = new UsbDeviceFinder(Constants.VID, Constants.PID); // open the device this.MyUSBDevice = UsbDevice.OpenUsbDevice(traqpaqDeviceFinder); // If the device is open and ready if (MyUSBDevice == null) throw new TraqPaqNotConnectedException("Device not found"); this.reader = MyUSBDevice.OpenEndpointReader(ReadEndpointID.Ep01); this.writer = MyUSBDevice.OpenEndpointWriter(WriteEndpointID.Ep02); // create the battery object this.battery = new Battery(this); // get a list of saved tracks getAllTracks(); // get the record table data tableReader = new RecordTableReader(this); tableReader.readRecordTable(); this.recordTableList = tableReader.recordTable; }
public void CloseDevice() { if(_usbDevice == null || !_usbDevice.IsOpen) return; if(_epReader != null) { _epReader.Dispose(); _epReader = null; } if(_epWriter != null) { _epWriter.Abort(); _epWriter.Dispose(); _epWriter = null; } // 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. var wholeUsbDevice = _usbDevice as IUsbDevice; if(!ReferenceEquals(wholeUsbDevice, null)) wholeUsbDevice.ReleaseInterface(0); // Release interface #0. _usbDevice.Close(); _usbDevice = null; }
public override void Close() { try { write_lock.WaitOne (); if (ep_reader != null) { // detach read event ep_reader.DataReceivedEnabled = false; ep_reader.DataReceived -= (read_usb); } ep_reader = null; ep_writer = null; if (IsOpen ()) { // close devices usb_device.Close (); wholeUsbDevice.ReleaseInterface (1); wholeUsbDevice.Close (); } // release devices usb_device = null; wholeUsbDevice = null; UsbDevice.Exit(); } catch (Exception) { // Ignore everything } finally { write_lock.ReleaseMutex(); } }
private Thread m_writeThread = null; // Thread d'envoi #endregion Fields #region Constructors public Sender(USBManager manager) { if (manager != null) { m_manager = manager; m_writer = m_manager.getDevice().OpenEndpointWriter(writeEndpoint); m_toSend = new Stack<string>(); // Initialisation de la pile m_writeThread = new Thread(this.doSend); // Création du thread m_writeThread.Start(); // Lancement du thread } }
public PTPDevice(UsbDevice dev) { _Device = dev; PTPSupported = false; _Name = dev.Info.ProductString; // TODO: try get better name Reader = null; Writer = null; ConfigurationID = 1; InterfaceID = 0; ReaderEndpointID = ReadEndpointID.Ep01; WriterEndpointID = WriteEndpointID.Ep02; }
public PTPDevice(UsbDevice dev) { this.Device = dev; this.PTPSupported = false; this._Name = dev.Info.ProductString; // TODO: try get better name this.Reader = null; this.Writer = null; this.ConfigurationID = 1; this.InterfaceID = 0; this.ReaderEndpointID = ReadEndpointID.Ep01; this.WriterEndpointID = WriteEndpointID.Ep02; }
public USBFadecandy(int pixelCount = 0, string serialNumber = "") : base(pixelCount) { if(pixelCount > 512) throw new ArgumentOutOfRangeException("pixelCount"); var packets = pixelCount/21; // Can fit 21 pixels into each packet activeBuffer = new byte[64 * packets]; // Each packet needs to be 64 bytes queuedBuffer = new byte[64 * packets]; // Setup control bytes for all packets. Never wiped out, only needs to be done once. for (int i = 0; i < packets; i++) { byte packet_index = (byte)(i & 0x1F); // Bits 0-4, packet index byte final = (byte)(i == packets - 1 ? 1 : 0); // Bit 5, final bit (denotes last packet) //byte type = 0; // Bits 6-7, type code (0 for video data) activeBuffer[i*64] = (byte)(packet_index & (final >> 5)); // First byte in each packet is a bitfield } Array.Copy(activeBuffer, queuedBuffer, activeBuffer.Length); dirty = false; UsbDeviceFinder usbFinder = string.IsNullOrEmpty(serialNumber) ? new UsbDeviceFinder(0x1d50, 0x607a) : new UsbDeviceFinder(0x1d50, 0x607a, serialNumber); ErrorCode ec = ErrorCode.None; // Find and open the usb device. fcdevice = UsbDevice.OpenUsbDevice(usbFinder); // If the device is open and ready if (fcdevice == 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 = fcdevice 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 write endpoint 1. writer = fcdevice.OpenEndpointWriter(WriteEndpointID.Ep01); }
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); }
public SmartScopeUsbInterfaceLibUsb(UsbDevice usbDevice) { if (usbDevice is IUsbDevice) { bool succes1 = (usbDevice as IUsbDevice).SetConfiguration(1); if (!succes1) throw new ScopeIOException("Failed to set usb device configuration"); bool succes2 = (usbDevice as IUsbDevice).ClaimInterface(0); if (!succes2) throw new ScopeIOException("Failed to claim usb interface"); } device = usbDevice; serial = usbDevice.Info.SerialString; dataEndpoint = usbDevice.OpenEndpointReader(ReadEndpointID.Ep01); commandWriteEndpoint = usbDevice.OpenEndpointWriter(WriteEndpointID.Ep02); commandReadEndpoint = usbDevice.OpenEndpointReader(ReadEndpointID.Ep03); Common.Logger.Debug("Created new ScopeUsbInterface from device with serial " + serial); }
public void closeDevice() { if (mUsbDevice != null) { if (mUsbDevice.IsOpen) { if (mEpReader != null) { mEpReader.DataReceivedEnabled = false; mEpReader.DataReceived -= mEp_DataReceived; mEpReader.Dispose(); mEpReader = null; } if (mEpWriter != null) { mEpWriter.Abort(); mEpWriter.Dispose(); mEpWriter = null; } // 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)) { // Release interface #0. wholeUsbDevice.ReleaseInterface(0); } mUsbDevice.Close(); mUsbDevice = null; } } }
public PolhemusStream() { try { // Find and open the usb device. PolhemusUsbDevice = UsbDevice.OpenUsbDevice(UsbFinder); // If the device is open and ready if (PolhemusUsbDevice == null) throw new Exception("Polhemus 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 = PolhemusUsbDevice 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); } //Polhemus uses EndPoint 2 for both read and write PolhemusReader = PolhemusUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02); PolhemusWriter = PolhemusUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02); } catch (Exception e) { throw new Exception("Error in PolhemusStream constructor: " + e.Message); } }
private static bool Get_connect_PlanetCNC() { //vid 2121 pid 2130 в десятичной системе будет как 8481 и 8496 соответственно UsbDeviceFinder myUsbFinder = new UsbDeviceFinder(8481, 8496); // Попытаемся установить связь _myUsbDevice = UsbDevice.OpenUsbDevice(myUsbFinder); if (_myUsbDevice == null) { string StringError = "Не найден подключенный контроллер."; _connected = false; AddMessage(StringError); //запустим событие о разрыве связи if (WasDisconnected != null) WasDisconnected(null, new DeviceEventArgsMessage(StringError)); return false; } 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); } // open read endpoint 1. _usbReader = _myUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); // open write endpoint 1. _usbWriter = _myUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01); return true; }
public iPhoneWrapper(UsbDevice USBDevice, bool bAFC2) { if (USBDevice == null) throw new Exception("Failed to create iPhone wrapper"); StartTime = DateTime.UtcNow; iPhoneUSBDevice = USBDevice; MUXConnections = new List<USBMUXConnection>(); IUsbDevice LibUsbDevice = iPhoneUSBDevice as IUsbDevice; if (!ReferenceEquals(LibUsbDevice, null)) { // This is a "whole" USB device. Before it can be used, // the desired configuration and interface must be selected. // Select config #3 LibUsbDevice.SetConfiguration(3); // Claim interface #1. LibUsbDevice.ClaimInterface(1); } // Open read endpoint 5. iPhoneEndpointReader = iPhoneUSBDevice.OpenEndpointReader(ReadEndpointID.Ep05); // Open write endpoint 4. iPhoneEndpointWriter = iPhoneUSBDevice.OpenEndpointWriter(WriteEndpointID.Ep04); // Say hello to the device and set up lockdown. if (!Initialize()) throw new Exception("Couldn't initialize iPhone"); // Try to start AFC2 if requested. If it doesn't work (not jailbroken), start AFC. int nAFCPort = 0; if (bAFC2) { try { nAFCPort = Lockdown.StartService("com.apple.afc2"); } catch { nAFCPort = Lockdown.StartService("com.apple.afc"); } if (nAFCPort != 0) { AFC = new AFCConnection(this, 5, (ushort)nAFCPort); MUXConnections.Add(AFC); } } else { nAFCPort = Lockdown.StartService("com.apple.afc"); AFC = new AFCConnection(this, 5, (ushort)nAFCPort); MUXConnections.Add(AFC); } int nNPPort = Lockdown.StartService("com.apple.mobile.notification_proxy"); NP = new NotificationProxyConnection(this, 6, (ushort)nNPPort); MUXConnections.Add(NP); Global.Log("Shutting down lockdownd (don't need it anymore).\n"); try { Lockdown.CloseConnection(); } catch { } MUXConnections.Remove(Lockdown); }
public void Shutdown(bool bUnexpected) { Global.Log("Shutting down iPhoneWrapper\n"); // If device was unexpectedly removed (unplugged), don't try to send // goodbye packets. if (!bUnexpected) { foreach (USBMUXConnection C in MUXConnections) { try { C.CloseConnection(); } catch { } } } try { if (iPhoneUSBDevice != null) { if (iPhoneUSBDevice.UsbRegistryInfo.IsAlive) { IUsbDevice LibUsbDevice = iPhoneUSBDevice as IUsbDevice; if (!ReferenceEquals(LibUsbDevice, null)) { // Release interface #1. LibUsbDevice.ReleaseInterface(1); } iPhoneUSBDevice.Close(); } iPhoneUSBDevice = null; iPhoneEndpointReader = null; iPhoneEndpointWriter = null; } } catch { } }
private bool openDevice(int index) { bool bRtn = false; closeDevice(); chkRead.CheckedChanged -= chkRead_CheckedChanged; chkRead.Checked = false; cmdRead.Enabled = true; chkRead.CheckedChanged += chkRead_CheckedChanged; if (mRegDevices[index].Open(out mUsbDevice)) { bRtn = true; // 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(0); } if (bRtn) { if (String.IsNullOrEmpty(comboBoxEndpoint.Text)) comboBoxEndpoint.SelectedIndex = 0; byte epNum = byte.Parse(comboBoxEndpoint.Text); mEpReader = mUsbDevice.OpenEndpointReader((ReadEndpointID)(epNum | 0x80)); mEpWriter = mUsbDevice.OpenEndpointWriter((WriteEndpointID)epNum); mEpReader.DataReceived += mEp_DataReceived; mEpReader.Flush(); panTransfer.Enabled = true; } } if (bRtn) { tsStatus.Text = "Device Opened."; } else { tsStatus.Text = "Device Failed to Opened!"; if (!ReferenceEquals(mUsbDevice, null)) { if (mUsbDevice.IsOpen) mUsbDevice.Close(); mUsbDevice = null; } } return bRtn; }
public LibUsb_AsyncUsbStream (string port) { var splited = port.Split ('-'); var finder = new UsbDeviceFinder (int.Parse (splited [0]), int.Parse (splited [1])); device = UsbDevice.OpenUsbDevice (finder); if (device == null) { throw new Exception ("Failed to find device:" + port); } if (!device.IsOpen) { throw new Exception ("Device is not open:" + port); } var usbDevice = device as IUsbDevice; var interfaceInfo = device.Configs [0].InterfaceInfoList [0]; if (usbDevice != null) { usbDevice.SetConfiguration (device.Configs [0].Descriptor.ConfigID); usbDevice.ClaimInterface (interfaceInfo.Descriptor.InterfaceID); deviceInterfaceId = interfaceInfo.Descriptor.InterfaceID; } foreach (var ep in interfaceInfo.EndpointInfoList) { if ((ep.Descriptor.EndpointID & 0x80) > 0) { reader = device.OpenEndpointReader ((ReadEndpointID)ep.Descriptor.EndpointID); reader.DataReceived += HandleDataReceived; reader.DataReceivedEnabled = true; } else { writer = device.OpenEndpointWriter ((WriteEndpointID)ep.Descriptor.EndpointID); } } }
/// <summary> /// Opens an endpoint for writing /// </summary> /// <param name="writeEndpointID">Endpoint number for read operations.</param> /// <param name="endpointType">The type of endpoint to open.</param> /// <returns>A <see cref="UsbEndpointWriter"/> class ready for writing. If the specified endpoint is already been opened, the original <see cref="UsbEndpointWriter"/> class is returned.</returns> public virtual UsbEndpointWriter OpenEndpointWriter(WriteEndpointID writeEndpointID, EndpointType endpointType) { foreach (UsbEndpointBase activeEndpoint in ActiveEndpoints) if (activeEndpoint.EpNum == (byte) writeEndpointID) return (UsbEndpointWriter) activeEndpoint; UsbEndpointWriter epNew = new UsbEndpointWriter(this, writeEndpointID, endpointType); return (UsbEndpointWriter) mActiveEndpoints.Add(epNew); }
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 static bool Connect() { ErrorCode ec = ErrorCode.None; try { // Find and open the usb device. MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); // If the device is open and ready if (MyUsbDevice == null) throw new Exception("Device Not Found."); // If this is angle "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 angle // device. IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice; if (!ReferenceEquals(wholeUsbDevice, null)) { // This is angle "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(1); } // open write endpoint 1. writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02); } catch (Exception ex) { Console.WriteLine(); Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message); return false; } Connected = true; return true; }
private void OpenDevice() { if(_usbDevice != null && _usbDevice.IsOpen) return; // Find and open the usb device. _usbDevice = UsbDevice.OpenUsbDevice(_usbFinder); // If the device is open and ready Debug.Assert(_usbDevice != null, string.Format("No device with PID: {0:X4} & VID: {1:X4} Found!", _pid, _vid)); // 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. var 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. wholeUsbDevice.ClaimInterface(0); } // open read endpoint 1. _epReader = _usbDevice.OpenEndpointReader((ReadEndpointID) EpIn); _epReader.Flush(); // open write endpoint 1. _epWriter = _usbDevice.OpenEndpointWriter((WriteEndpointID) EpOut); LibMain.OnConnectedChanged(true); }
public bool Open() { bool success = true; // try { // Find and open the usb device. myUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); // // If the device is open and ready if (myUsbDevice == null) throw new Exception("X10 CM15Pro device not connected."); // // 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); } // // open read endpoint 1. reader = myUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); // open write endpoint 2. writer = myUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02); // this.WriteData(new byte[] { 0x8B }); // status request } catch { success = false; //throw new Exception("Error opening X10 CM15Pro device."); } return success; }
public bool Reset() { bool ret = true; Close(); //Start message thread _messageThread = new Thread(new ThreadStart(_SendMessages)); _messageThread.IsBackground = true; _messageThread.Start(); //Find device _device = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(_VENDOR_ID, _PRODUCT_ID)) as IUsbDevice; if (_device != null) { //Set up the incoming and outgoing endpoints _reader = _device.OpenEndpointReader(LibUsbDotNet.Main.ReadEndpointID.Ep03); _reader.DataReceived += _reader_DataReceived; _reader.DataReceivedEnabled = true; lock (_writerLock) { _writer = _device.OpenEndpointWriter(LibUsbDotNet.Main.WriteEndpointID.Ep02); } } else ret = false; return ret; }
public void Open() { if (_portType == PortType.COM) { _serialPort.Open(); _isOpen = true; } if (_portType == PortType.USB) { // Find and open the usb device. _usbDevice = UsbDevice.OpenUsbDevice(_usbFinder); // If the device is open and ready if (_usbDevice == 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 = _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. wholeUsbDevice.ClaimInterface(0); } // open write endpoint 1. _usbWriter = _usbDevice.OpenEndpointWriter(_endpointId); _isOpen = true; } if (_portType == PortType.EtherNet) { _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socket.Connect(_ipEndPoint); _socket.SendTimeout = 1000; _isOpen = true; } }
private bool openDevice(int index) { bool bRtn = false; closeDevice(); if (mRegDevices[index].Open(out mUsbDevice)) { bRtn = true; // 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(0); } if (bRtn) { byte epNum = endpoint; mEpReader = mUsbDevice.OpenEndpointReader((ReadEndpointID)(epNum | 0x80), 64); mEpWriter = mUsbDevice.OpenEndpointWriter((WriteEndpointID)epNum); mEpReader.DataReceived += mEp_DataReceived; mEpReader.ReadBufferSize = 64; mEpReader.ReadThreadPriority = ThreadPriority.AboveNormal; mEpReader.Flush(); } } if (bRtn) { tsStatus.Text = "Device Opened."; } else { tsStatus.Text = "Device Failed to Opened!"; if (!ReferenceEquals(mUsbDevice, null)) { if (mUsbDevice.IsOpen) mUsbDevice.Close(); mUsbDevice = null; } } return bRtn; }
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 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]"); }
/// <summary> /// Opens an endpoint for writing /// </summary> /// <param name="writeEndpointID">Endpoint number for read operations.</param> /// <param name="endpointType">The type of endpoint to open.</param> /// <returns>A <see cref="UsbEndpointWriter"/> class ready for writing. If the specified endpoint is already been opened, the original <see cref="UsbEndpointWriter"/> class is returned.</returns> public virtual UsbEndpointWriter OpenEndpointWriter(WriteEndpointID writeEndpointID, EndpointType endpointType) { foreach (UsbEndpointBase activeEndpoint in ActiveEndpoints) if (activeEndpoint.EpNum == (byte) writeEndpointID) return (UsbEndpointWriter) activeEndpoint; byte altIntefaceID = mClaimedInterfaces.Count == 0 ? UsbAltInterfaceSettings[0] : UsbAltInterfaceSettings[mClaimedInterfaces[mClaimedInterfaces.Count - 1]]; UsbEndpointWriter epNew = new UsbEndpointWriter(this, altIntefaceID, writeEndpointID, endpointType); return (UsbEndpointWriter) mActiveEndpoints.Add(epNew); }
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); } }
// 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(); } }