简体   繁体   English

当在C#中使用WriteFile与HID设备通信时,我的程序停止

[英]When using WriteFile in C# for communication with HID device my program halts

At the end of a data transfer session with my HID device my software has a ~20% chance to halt on the function WriteFile. 在与我的HID设备进行数据传输会话结束时,我的软件有大约20%的机会停止写入函数WriteFile。

This is written in C#, and I can't figure out why this is happening. 这是用C#编写的,我无法弄清楚为什么会这样。 A 20% error rate per packet is simply unacceptable and I am having a hard time getting to the bottom of it, I was wondering if it might be something with my declarations or data types? 每个数据包20%的错误率是不可接受的,我很难找到它的底部,我想知道它是否与我的声明或数据类型有关?

This is the import 这是导入

[DllImport("kernel32.dll")] 
    static public extern int WriteFile(int hFile, ref byte lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, int lpOverlapped);

And this is the call to the function that is halting 这就是对暂停功能的调用

Result = USBSharp.WriteFile(hidHandle, ref outputReportBuffer[0], outputReportBuffer.Length, ref NumberOfBytesWritten, 0);

The handle is confirmed to be valid and the rest is fairly self explanatory... 确认手柄是有效的,其余的是相当自我解释的......

The function simply never returns. 该功能根本不会返回。 I've looked this issue up online a few different locations and mostly nobody's fixes apply to me. 我在网上看了几个不同的地方这个问题,大多数人都没有修复我。 I would just thread it and re-call it if it fails but doing that 20% of the time on hundreds of packets is simply... awful. 如果它失败了,我只会对它进行线程化并重新调用它,但是在数百个数据包上执行20%的时间只是......非常糟糕。

I am using windows 7, C#, .NET 4.0, and the HID device is not halting it is still active and running - not only that, but the entire data transfer happens properly and this call happens at the very end to complete the transaction and then halts (even though I already have all the data). 我正在使用Windows 7,C#,.NET 4.0,并且HID设备没有停止它仍然处于活动状态并且正在运行 - 不仅如此,而且整个数据传输正常发生并且此调用在最后发生以完成事务并且然后停止(即使我已经拥有所有数据)。 Unfortunately I can't just ignore this final part of the transaction because this data needs to be 100% maintained or else bad, bad, BAD things will happen to the users. 不幸的是,我不能忽略交易的最后部分,因为这些数据需要100%维护,否则糟糕,糟糕,用户会遇到不好的事情。

If problem persists then you might consider implementing a timeout mechanism: 如果问题仍然存在,那么您可以考虑实施超时机制:

var stream= new FileStream(hidHandle,FileAccess.Write,false);
var waitEvent= new ManualResetEventSlim();

void Write(byte[] report, int timeout){
    waitEvent.Reset();
    stream.BeginWrite(report,0,report.Lenght,(ar)=>{stream.EndWrite(ar);waitEvent.Set();},null);
    waitEvent.Wait(timeout);
}

Sounds like a problem in the USB driver library. 听起来像USB驱动程序库中的问题。 That's the only unique variable in your circumstances. 这是您情况下唯一的唯一变量。 (eg lots of people are using WriteFile with USB drives without any problem). (例如,许多人正在使用带有USB驱动器的WriteFile而没有任何问题)。

I suggest you contact Florian Leitner for support, or find alternate means of connecting to USB devices. 我建议您联系Florian Leitner寻求支持,或者找到连接USB设备的替代方法。 Forian details that it uses the USBSharp class and Scott Hanselman's article so maybe those would be good alternates to start with. Forian详细说明它使用了USBSharp类和Scott Hanselman的文章,所以也许那些将是很好的替代品开始。

It's hard to guess what is going wrong here but, here is something important to check/try: 很难猜出这里出了什么问题但是,这里有一些重要的检查/尝试:

check if you're calling CreateFile, WriteFile from inside an unsafe context . 检查你是否在不安全的上下文中调用CreateFile,WriteFile read more about unsafe code 阅读更多关于不安全代码的信息
And to make this unsafe context use unsafe keyword like this: 为了使这个不安全的上下文使用不安全的关键字,如下所示:

unsafe static void writeMyFile(...){
...
}

also check the option (Allow unsafe code) in the project properties like this: 还要检查项目属性中的选项(允许不安全的代码),如下所示: 允许不安全的代码

i hope this will help. 我希望这将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM