none
IOException when SerialPort.Open()

    Question

  • Hi

    I am trying to open serialport on COM1 so that I can print some messages to it for debugging purposes.

    When I invoke this function below, it does list COM1 and says Port is Closed.

    But raises IOException at port.Open().

    private void Func() {

       SerialPort port = new SerialPort("COM1",115200,Parity.None);//this works

       string[] ports = SerialPort.GetPortNames();

       foreach (string str in ports)   {

                 Console.Write( "The List of Ports are: " + " \n\n " + str + ":");

                 if (port.IsOpen == false)  {

                          Console.WriteLine( "This Port is Closed \n");

                          port.Open();  //IOException here

                 }

                 else if (port.IsOpen == true)

                        Console.WriteLine( "The Port is Open \n");

      }

    }

    Can someone help me solve this problem please.

    Thanks

    Thursday, April 06, 2006 8:43 PM

Answers

  • Hi,

    When SerialPort.IsOpen is false it only means the port has not been opened by the particular SerialPort instance; it does not mean the port has not been opened by other serial port applications or that the port in fact is in a usable state.

    If SerialPort.Open() throws an IOException, it usually means that the port is not in a usable state. You may want to check with the manufacturer the list of ports that you may use.

    Cheers,

    Anthony Wong [MSFT]

    Friday, April 14, 2006 10:44 PM

All replies

  • Hi,

    When SerialPort.IsOpen is false it only means the port has not been opened by the particular SerialPort instance; it does not mean the port has not been opened by other serial port applications or that the port in fact is in a usable state.

    If SerialPort.Open() throws an IOException, it usually means that the port is not in a usable state. You may want to check with the manufacturer the list of ports that you may use.

    Cheers,

    Anthony Wong [MSFT]

    Friday, April 14, 2006 10:44 PM
  • Hi!

    I have a problem with SerialPort.Open().

    I'm using a wm6 pda with built-in GPS receiver. It's hardware port is com1, the virtual gps port is set to com5. I'm using a GPS Viewer from holux to connect to the gps and display nmea sentences and it works fine. I can open both, com1 and com5 port and get data. The problem is when i try to access the port from .net 2.0 sp2 (gps viewer closed!), in both cases I keep on getting an IOException on SerialPort.Open(). I've no problems accessing a paired BT gps on com7 using the same code.

    Anyone have ANY idea what's wrong?

    Maybe i need to supply some additional non-default parameters to the SerialPort constructor? I've tried explicitly setting Parity.None and databits = 8, StopBits.One but with no luck.
    Sunday, November 04, 2007 11:30 PM
  • I am having the same problem if I find an answer I will post it.

    p.s. If you just want to get the location/speed/heading from the device (I'm looking for a way to get NMEA sentences)

    goto: http://www.microsoft.com/downloads/details.aspx?FamilyID=06111a3a-a651-4745-88ef-3d48091a390b&DisplayLang=en

    and download the appropriate WM6 SDK...

    If you look on the start menu under "Windows Mobile 6 SDK"
    there is a folder called "Samples" open it.
    Click on the "Samples Directory."
    Choose the coding language and open the folder called "GPS"
    Goto the debug folder of "GPSSamples" and copy the DLL: "microsoft.windowsmobile.samples.location.dll" to your program's debug folder and add it as a reference.
    Saturday, June 20, 2009 2:24 PM