Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Communicate with WinUSB device with LIBUSB

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.

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.

`
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";

    }
}
like image 629
user1698144 Avatar asked Dec 14 '25 12:12

user1698144


1 Answers

Found the solution

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

like image 150
user1698144 Avatar answered Dec 16 '25 09:12

user1698144



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!