串行端口和 BSOD
我编写了一些 C# 代码,通过在端口上发出命令并侦听回复来检查设备是否存在于任何串行端口上。 当我设置端口速度、打开端口、获取串行流并开始处理时,它在 100% 的时间内工作。 然而,我们的一些设备以不同的速度工作,我正在尝试以不同的速度探测设备以自动协商连接并检测设备的存在。
当我在单个线程中完成所有这些操作时,没有任何问题。 但十速时3s超时是每个串口30s,可能有好几个。 因此需要同时探测所有端口。
有时这有效。 有时 Vista 蓝屏。 当我使用线程同时探测所有端口时,它几乎总是蓝屏。 当我强制所有内容在一个线程中运行时,它永远不会发生。
USB 串行 Prolific PL-2303 适配器与 x64 驱动程序一起使用。
@Vinko - 感谢您提供有关阅读小型转储的提示。
据我所知,问题的关键在于,通过从不同的线程启动新的异步 I/O 操作,可以为重叠 I/O 赋予全新的含义,从而导致驱动程序内部的竞争条件。 由于驱动程序在内核模式下执行,BLAM!
Epilogue
除了启动之外,不要在回调处理程序之外使用 BeginXxx,并且在调用 EndXxx 之前不要调用 BeginXxx,因为您将在内核模式下运行的驱动程序代码中引发竞争条件。
后记
我发现这也适用于套接字流。
I've written some C# code that checks whether a device is present on any SerialPort by issuing a command on the port and listening for a reply. When I just set the port speed, open the port, get the serial stream and start processing, it works 100% of the time. However, some of our devices work at different speeds and I am trying to probe for a device at various speeds to autonegotiate a connection as well as detect device presence.
When I do all this in a single thread there are no problems. However, 3s timeout at ten speeds is 30s per serial port, and there may be several. Hence the desire to probe all ports concurrently.
Sometimes this works. Sometimes Vista bluescreens. When I use threads to probe all the ports simultaneously it nearly always bluescreens. When I force everything to run in one thread it never happens.
A USB-serial Prolific PL-2303 adaptor is in use with x64 drivers.
@Vinko - thanks for the tip on reading minidumps.
As near as I can tell, the crux of the problem is that by starting a new asynchronous I/O operation from a different thread it is possible to give a whole new meaning to overlapped I/O, inducing a race condition inside the driver. Since the driver executes in kernel mode, BLAM!
Epilogue
Except for kicking off, don't use BeginXxx outside of the callback handler and don't call BeginXxx until you've called EndXxx, because you'll induce a race condition in driver code that runs in kernel mode.
Postscript
I have found that this also applies to socket streams.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
曾经为此类设备之一编写过 Windows 驱动程序,我的建议是不要在 WinDbg 上浪费时间来试图证明您已经知道的事情 - 即您正在使用的驱动程序有问题。
如果您可以从 PL2302 找到更新的驱动程序,请尝试一下,但我的建议是,如果您必须使用 USB-> 串行适配器,那么基于 FTDI 的适配器是最好的。 (他们也不是我为其编写驱动程序的人......)
Having written Windows drivers for one of these sort of device once, my advice would be not to waste your time with WinDbg trying to prove what you already know - i.e. that the driver you're using is buggy.
If you can find a more up-to-date driver from the PL2302, then try that, but my recommendation is that if you have to use USB->Serial adaptors, the FTDI-based ones are the best. (They're not the one I wrote the drivers for, either...)
您还可以在系统属性\高级\启动和恢复\设置下禁用“自动重新启动”。 一旦禁用该功能,您就可以看到 BSOD 并查找错误消息,例如 IRQL_LESS_OR_EQUAL,通过搜索该错误名称,通常可以缩小问题的根源。
顺便说一句,现在带串口的笔记本不多了,所以你一定用的是USB转串口转换器吧? 如果是这种情况,则驱动程序可能是一个开始问题,因为大多数制造商将串行端口驱动程序编写为虚拟驱动程序。
You can also disable "Automatic Restart" under System Properties\Advanced\Start and Recovery\Settings. Once you disable that, you can see the BSOD and look for the error message, e.g. IRQL_LESS_OR_EQUAL, by searching for that error name, you can usually narrow down to the source of the problem.
Btw, not many notebook comes with serial ports nowadays, so you must be using USB-Serial converter? If that's the case, the driver might have been an issue to start with, since most manufacturer wrote the serial port driver as virtual driver.
BSOD 通常意味着有缺陷的驱动程序。
您使用哪种硬件端口? 我在使用 SiLabs CP21xx USB 转串口转换器驱动程序时遇到过 BSOD。
BSOD usually means buggy drivers.
What kind of HW ports do you use? I've had BSODs with SiLabs CP21xx USB to Serial converters drivers.
有在 x64 vista 和 win7 下稳定的 FTDI 驱动程序。 我支持那个说只使用 FTDI 芯片组的人。
我附近的商店(加拿大多伦多)的大多数廉价串行转 USB 加密狗似乎都是 FTDI 芯片。 盒子上从来没有,所以我买了一个,如果好的话,我就去买一整盒。
瓦
There are FTDI drivers that are stable under x64 vista and win7. I second the person who said to use FTDI chipsets only.
Most of the cheap serial to usb dongles at the shops near me (Toronto, Canada) seem to be FTDI chips. It's never on the box, so I buy one, and if it's good, I go buy a box full of them.
W