title: C# - Communicate with WinUSB device with LIBUSB
body: <p>I am trying to communicate with a Nokia Lumia phone(RM-917), over USB using LIBUSING and C#. LIBUSB is able to see the device's information(pid,vid,etc). However, I am not able to successfully write to ANY endpoint, even sending the exact command as the Windows Device Recovery Tool.</p>
  
  <p>According to WinUSB, the write endpoint is EP07, however, this endpoint just times out. I have tried every other endpoint, and all of these fail.</p>
  
  <pre><code>`
  public void initDevice()
  {
  
  
      if(this.lumiaDevice == null)
      {
          throw new Exception("LumiaPhoneManager does not have a selected device");
      }
  
      UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0x0421, 0x0661);
      MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
  
  
      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(1);
      }
  
      if (this.writer == null)
      {
          writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep07);
      }
  
  
  
  }
  
  
      public void readPCode()
  
  {
      currentID++;
      var _x = new jsonPkt();
      ErrorCode ec = ErrorCode.None;
      int bytesWritten;
      _x.id = this.currentID + 1;
      _x.method = "ReadProductCode";
  
      string value = @"{""jsonrpc"":""&lt;JSONRPC&gt;"",""id"":&lt;ID&gt;,""method"":""&lt;METHOD&gt;"",""params"":null}";
      value = value.Replace("&lt;JSONRPC&gt;", "2.0");
      value = value.Replace("&lt;ID&gt;", currentID.ToString());
      value = value.Replace("&lt;METHOD&gt;", _x.method.ToString());
  
  
  ec = writer.Write(Encoding.Default.GetBytes(value), 8000, out bytesWritten);
      currentID++;
  
      if (ec != ErrorCode.None) throw new Exception(UsbDevice.LastErrorString);
  
      byte[] readBuffer = new byte[1024];
      while (ec == ErrorCode.None)
      {
          int bytesRead;
  
          // If the device hasn't sent data in the last 100 milliseconds,
          // a timeout error (ec = IoTimedOut) will occur. 
          ec = reader.Read(readBuffer, 100, out bytesRead);
  
       //   if (bytesRead == 0) throw new Exception("No more bytes!");
  
          // Write that output to the console.
          this.rtb.Text += Encoding.Default.GetString(readBuffer, 0, bytesRead).ToString() + "\n";
  
      }
  }
  </code></pre>