首页 文章

C# - 使用LIBUSB与WinUSB设备通信

提问于
浏览
3

我正在尝试使用LIBUSING和C#通过USB与诺基亚Lumia手机(RM-917)进行通信 . LIBUSB能够查看设备的信息(pid,vid等) . 但是,我无法成功写入任何 endpoints ,甚至无法将命令作为Windows设备恢复工具发送 .

根据WinUSB,写 endpoints 是EP07,但是这个 endpoints 只是超时 . 我已经尝试了所有其他 endpoints ,所有这些都失败了 .

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

    }
}

2 回答

  • -1

    找到了解决方案

    调试OEM软件,发现程序使用的是USB设备的不同路径 . 之后,我获得了访问被拒绝的错误,这是通过将项目移动到另一个驱动器来解决的 . 由于未知原因,当程序在c盘上运行时,CreateFile函数失败并拒绝访问

  • 1

    可能要激活写入,您需要先发送一些特定于类的控制请求 . 你提到windows设备恢复工具能够写 . 您可以在Windows PC中安装USB数据包嗅探软件,然后使用设备管理器将一些数据写入设备 . 数据包嗅探器工具将能够捕获发送到设备的所有数据包 . 通过这种方式,您可以查看启用写入操作所需的确切设备请求 .

    分析器软件 - http://www.usblyzer.com/

    请尝试这种方式来解决您的问题 .

    PS-我假设您没有像Lecroy顾问或Beagle那样的硬件USB数据包分析器 . 软件包嗅探器应该没问题,因为主机是PC .

相关问题