C # - Communicating with WinUSB device with LIBUSB

I am trying to communicate with a Nokia Lumia (RM-917) phone, over USB, using LIBUSING and C #. LIBUSB can see device information (pid, vid, etc.). However, I am unable to successfully write ANY endpoint, even sending the exact command as a means of restoring Windows devices.

According to WinUSB, the recording endpoint is EP07, however, this endpoint is simply disabled. I've tried all the other endpoints and they all fail.

`
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"":""<JSONRPC>"",""id"":<ID>,""method"":""<METHOD>"",""params"":null}";
    value = value.Replace("<JSONRPC>", "2.0");
    value = value.Replace("<ID>", currentID.ToString());
    value = value.Replace("<METHOD>", _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";

    }
}

      

+3


source to share


2 answers


Found a solution



Debugged OEM software and found that the program was using a different path to the USB device. After that, I got access denied errors, which was resolved by moving the project to another disk. For unknown reasons, when the program runs on drive c, the CreateFile function exits with access denied

+1


source


Perhaps, in order to activate the recording, you first need to send a request to control a specific class. You mentioned that Windows Device Recovery Tool is capable of writing. You can install USB packet analysis software on your Windows PC and then use Device Manager to write some data to the device. Batch sniffer tool will be able to capture all packets sent to the device. This way you can see the exact device requests that are required to enable the write operation.

Analyzer Software - http://www.usblyzer.com/



Please try this method to fix your problem.

PS- I'm assuming you don't have a hardware USB packet sniffer like the Lecroy or Beagle advisor. The sniffer of the software package must be accurate since the host is the PC.

-1


source







All Articles