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
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.