ControlTransfer指令未从LibUsbDotNet中的设置打包程序发送值参数 [英] ControlTransfer instruction not sending value parameter from setup packetr in LibUsbDotNet

查看:55
本文介绍了ControlTransfer指令未从LibUsbDotNet中的设置打包程序发送值参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个USB设备,需要能够通过.net应用程序与之通信.该设备不是标准的HID设备,为了对其进行初始化,我从USB协议分析器/嗅探器获得了一些数据包的踪迹,用于在另一种类型的计算机上初始化它时使用的数据包.我需要从.net应用程序中复制此数据包序列以初始化设备.

I have a USB device that I need to be able to talk to from a .net application. The device is not a standard HID device and in order to initilise it I've been given a trace of packets from a USB Protocol Analyser / Sniffer for the packets used when intialising it on another type of machine. I need to replicate this packet sequence from my .net application to initialise the device.

一切正常,直到我收到特定的控制传输包/类类型请求为止.

Everything is working well until I get to a particular control transfer packet / class type request.

我得到的跟踪结果表明我应该发出:

The trace I've been given states I should issue:

Control Transfer Class Type Request 
21 0A 00 00 00 00 00 00 
Result stall (intentional)

Control Transfer Class Tyoe Request
A1 01 01 03 00 00 40 00 
Result will initiate a 64 byte transfer of data from the device to the host.

这是我用来执行此操作的代码:

This is the code I'm using to do this:

                // Transcation 6
                UsbSetupPacket setup = new UsbSetupPacket(0x21, 0x0A, 0, 0, 0);
                bool result = MyUsbDevice.ControlTransfer(ref setup, buffer, 0, out transferred);
                Console.WriteLine("Result = {0}", result);

                // Transcation 7
                setup = new UsbSetupPacket(0xA1, 0x01, 0x0301, 0x0000, 0x0040);
                result = MyUsbDevice.ControlTransfer(ref setup, buffer, 64, out transferred);

                Console.WriteLine("Result = {0}, {1}", result, transferred);

这是我从BusHound接收到的跟踪,正在嗅探该设备的USB数据流量:

And this is the trace I'm receiving from BusHound which is sniffing the USB data traffic for this device:

Device  Phase  Data                      Description       Cmd.Phase.Ofs(rep)
------  -----  ------------------------  ----------------  ------------------
  46.0  CTL    21 0a 00 00  00 00 00 00  SET IDLE                20.1.0        
  46.0  USTS   c0000004                  stall pid               20.2.0        
  46.0  CTL    a1 01 01 03  00 00 00 00  GET REPORT              21.1.0        
  46.1  USTS   c0000004                  stall pid               22.1.0        

正如您所看到的,即使我进行了设置,安装程序包中的0x0040值参数也未将其识别出来.我对USB和.net/LibUsbDotNet相对较新,我不太确定自己做错了什么.我想知道是否有人可以建议我尝试一下?

As you can see the 0x0040 value parameter in the setup packet is not making it out even though I'm setting it. I'm relatively new to USB and to .net / LibUsbDotNet and I'm not quite sure what I'm doing wrong. I wonder if anyone can suggest anything for me to try?

请注意,我正在使用Visual Studio 2008在Windows 7 64位计算机上进行开发.

Note, I'm developing on a Windows 7 64bit machine using Visual Studio 2008.

谢谢,丰富

推荐答案

好的,经过大量调查,我发现了问题的根源,实际上是我对LibUSBDotNet的工作原理缺乏了解,穷人对此没有帮助.否则就很棒的库的文档.

OK, after much investigation I found the source of the problem and it was really my lack of understanding of how LibUSBDotNet works, which isn't helped by the poor documentation for the otherwise excellent library.

问题在于0x0040不应在设置数据包中手动指定-此值似乎无关紧要.相反,只需在ControlTransfer方法中指定要传输的字节,并确保缓冲区是预先分配的适当大字节数组即可,例如:

The problem is that the 0x0040 should not be manually specified in the setup packet - this value appears to be irrelevant. Instead simply specify the bytes to transfer in the ControlTransfer method and also ensure that buffer is a pre-allocated suitably large byte array eg:

byte[] buffer = new byte[256];
setup = new UsbSetupPacket(0xA1, 0x01, 0x0301, 0x0000, 0x0000); 
result = MyUsbDevice.ControlTransfer(ref setup, buffer, 0x0040, out transferred); 

这将生成发送到USB设备的正确控制传输数据包

This will generate the correct control transfer packet sent to the USB device

Control Transfer Class Tyoe Request   
A1 01 01 03 00 00 40 00   

LibUsbDotNet似乎对各种参数进行了一些验证,并且在缓冲区数组不够大的情况下,它只是发送其他内容(在我的情况下为0x0000)而不是抛出适当的异常.

It appears the LibUsbDotNet does some validation on the various parameters and in the case where the buffer array isn't large enought it simply just sends something else instead (in my case 0x0000) rather than throwing an appropriate exception.

这篇关于ControlTransfer指令未从LibUsbDotNet中的设置打包程序发送值参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆