打开虚拟COM端口时收到IOException

本文关键字:IOException 虚拟 COM | 更新日期: 2023-09-27 18:24:58

我正在编写一个程序,该程序需要从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();

我的第一个代码片段中的Intialize方法是由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编程。我不认为我可能做错了什么。有人有什么想法吗?

Tony

打开虚拟COM端口时收到IOException

我已经找到了问题的解决方案。事实证明,原因是我为GPS安装的驱动程序软件。这是Vista的旧驱动程序。我下载了一个更新的Windows7驱动程序(这是我在电脑上运行的)并安装了它。这修复了的问题

谢谢大家。