调试时无法访问串口 [英] Cannot access serial port during debugging

查看:149
本文介绍了调试时无法访问串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个 SO 问题,所以请纠正我没有提供足够或正确信息的地方.

This is my first SO question so please correct me where I am not supplying enough or correct information.

我通过 FTDI 串口转 USB 集线器上的 RS232 端口将两个 RFID 标签阅读器连接到我的程序.该程序在 VS2010 之外完美运行,但我无法调试代码.该程序在调试期间启动良好,但是一旦我将标签置于读取范围内,我就会收到错误消息.这只会在调试期间发生.

I'm interfacing two RFID tag readers via RS232 ports on a FTDI serial to USB hub to my program. The program runs perfectly outside VS2010, but I cannot debug the code. The program starts fine during debugging, but as soon as I bring a tag within reading proximity I get an error. This only happens during debugging.

错误:

Error Opening Serial Port COM9

System.UnauthorizedAccessException: Access to the port 'COM9' is denied.

   at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)

   at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)

   at System.IO.Ports.SerialPort.Open()

   at Tag_Reader.COMs.openPort(String sPortName) in C:\XXX\Tag_Reader\COMs.cs:line 36

COMs.cs:第 36 行:

COMs.cs:line 36:

 _port.PortName = sPortName;
 _port.BaudRate = 9600;
 _port.DataBits = 8;
 _port.Parity = Parity.None;
 _port.StopBits = StopBits.One;
 _port.ReadTimeout = 500;
 _port.WriteTimeout = 500;
 _port.Open();

我将不胜感激!如上所述,如果我在 VS 之外运行 .exe,程序运行时不会出现故障.

I would appreciate any help! As mentioned above the programs runs without a glitch if I run the .exe outside VS.

此错误仅在调试期间发生,并且发生在从标签阅读器流式传输数据时(当标签处于阅读器的读取范围内时).这表明端口设置正确.

This error only occurs during debug and only when there is data being streamed from the tag reader (when a tag is brought within the reader's reading proximity). This indicates that the port setup is correct.

推荐答案

例外是非常特定的,它只意味着一件事.端口已经打开,不能再打开.这可能是另一个过程,也可能是您自己的过程.

The exception is very specific, it only ever means one thing. The port is already opened, you cannot open it again. It might be another process, it could be your own.

您可能犯的使其特定于调试会话的标准错误是使用另一个程序查看端口发送的数据.通常要做的事情是将此类程序显示的数据与显示的数据进行比较以查找不匹配的数据.这行不通,你在启动自己的程序之前先启动这样的程序,它会打开端口并且你的程序总是会失败.您需要一种不同类型的实用程序来执行此操作,它是一个过滤驱动程序,它在串行端口驱动程序之前注入自身并监视驱动程序请求.SysInternals 的 PortMon 就是一个例子.

A standard mistake you could make that makes it specific to a debug session is to use another program to look at the data sent by the port. A common thing to do, you'd want to compare the data displayed by such a program with the data you display to look for a mismatch. This cannot work, you start such a program before you start your own, it will open the port and your program will always fail. You need a different kind of utility to do this, a filter driver that injects itself ahead of the serial port driver and spies on the driver requests. SysInternals' PortMon is an example.

任何仅当有数据从标签阅读器流式传输时

Any only when there is data being streamed from the tag reader

这个注释很麻烦,您当然必须始终在标签阅读器开始流式传输数据之前打开端口.这暗示了一种非常不同的错误,应该可以从 完整 堆栈跟踪中看到.DataReceived 事件处理程序中的代码导致再次调用 Open() 方法的某些情况.由于 DataReceived 事件处理程序中的 Begin/Invoke() 调用,可能隐藏得很好.

This comment is very troublesome, you must of course always open the port well before the tag reader starts streaming data. This hints at a very different kind of bug, ought to be visible from the full stack trace. Some scenario where the code in the DataReceived event handler causes the Open() method to be called again. Possibly well hidden due to a Begin/Invoke() call inside your DataReceived event handler.

最后但并非最不重要的一点,请确保您从不重复调用 Close() 和 Open().Close() 调用实际上并没有关闭串行端口,它只是开始 来关闭它.内部工作线程需要退出,这需要时间.只在程序终止时调用 Close().

Last but not least, do make sure that you never repeatedly call Close() and Open(). The Close() call doesn't actually close the serial port, it only starts to close it. An internal worker thread needs to exit, that takes time. Only ever call Close() at program termination.

这篇关于调试时无法访问串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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