打开虚拟 COM 端口时收到 IOException
我正在编写一个需要从 NMEA GPS 设备接收输入的程序。 NMEA 标准要求使用 COM 端口之一进行通信。
以下是给我带来麻烦的代码摘录:
public int BaudRate { get; set; }
private SerialPort comm;
public string CommPort { get; set; }
protected override void Initialize() {
comm = new SerialPort();
comm.BaudRate = BaudRate;
comm.DataBits = 8;
comm.NewLine = "\r\n";
comm.Parity = Parity.None;
comm.PortName = ComPort;
comm.StopBits = StopBits.One;
comm.Open();
}
在我的单元测试方法中,我有以下代码:
NMEAGPS gps = new NMEAGPS();
gps.ComPort = "COM3";
gps.BaudRate = 4800;
gps.Start();
第一个代码片段中的 Intiialize 方法由 Start 方法调用。
调用 comm.Open() 时发生错误。以下是异常详细信息:
System.IO.IOException was caught
Message=The I/O operation has been aborted because of either a thread exit or an application request.
Source=System
StackTrace:
at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.InternalResources.WinIOError()
at System.IO.Ports.SerialStream.InitializeDCB(Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Boolean discardNull)
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 LPRCore.Devices.NMEAGPS.Initialize() in D:\ElsagTFS\EOC4\Client\LPRCore Plugin GPS\NMEAGPS.cs:line 385
at LPRCore.Module.InternalPrestart() in D:\ElsagTFS\EOC4\Client\LPRCore\Module.cs:line 413
这是我第一次在 .NET 中进行 COM 编程。我不明白我可能做错了什么。有人有什么想法吗?
托尼
I'm writing a program that needs to receive input from a NMEA GPS device. The NMEA standard requires communications using one of the COM ports.
Here's an excerpt of the code that's giving me trouble:
public int BaudRate { get; set; }
private SerialPort comm;
public string CommPort { get; set; }
protected override void Initialize() {
comm = new SerialPort();
comm.BaudRate = BaudRate;
comm.DataBits = 8;
comm.NewLine = "\r\n";
comm.Parity = Parity.None;
comm.PortName = ComPort;
comm.StopBits = StopBits.One;
comm.Open();
}
In my Unit Test method, I have the following code:
NMEAGPS gps = new NMEAGPS();
gps.ComPort = "COM3";
gps.BaudRate = 4800;
gps.Start();
The Intiialize method in my first code snippet is called by the Start method.
The error occurs on the call to comm.Open(). Here's the exception details:
System.IO.IOException was caught
Message=The I/O operation has been aborted because of either a thread exit or an application request.
Source=System
StackTrace:
at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.InternalResources.WinIOError()
at System.IO.Ports.SerialStream.InitializeDCB(Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Boolean discardNull)
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 LPRCore.Devices.NMEAGPS.Initialize() in D:\ElsagTFS\EOC4\Client\LPRCore Plugin GPS\NMEAGPS.cs:line 385
at LPRCore.Module.InternalPrestart() in D:\ElsagTFS\EOC4\Client\LPRCore\Module.cs:line 413
This is the first time that I've done any COM programming in .NET. I don't see what I could possibly be doing wrong. Does anybody have any ideas?
Tony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经找到了解决我的问题的方法。原来是我安装的GPS驱动软件的原因。这是 Vista 的老驱动程序。我下载了 Windows 7 的更新驱动程序(这是我在 PC 上运行的驱动程序)并安装了它。这解决了问题
谢谢大家。
I have found the solution to my problem. It turns out the cause was the driver software that I installed for the GPS. It was an older driver for Vista. I downloaded an updated driver for Windows 7 (which is what I'm running on my PC) and installed it. This fixed the problem
Thanks everyone.