Serial Port with C# serial port class problem

B

BrianK

Hello,
My CE 6.0 device has 3 serial ports and the serial port class can enumurate
them all via this method just fine.
string[] AvailablePorts = SerialPort.GetPortNames();
Also creating a serial port object is no problem for any of the 3 ports as
follows,
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits, System.IO.Ports.StopBits.One);
I can also do this,
hSp.BaudRate = 9600;
However, when I try to open any of the 3 ports(I know com1 is the debug
port) I get an exception as follows,
A first chance exception of type 'System.IO.IOException' occurred in
System.dll
I cannot open the port, what is wrong here?
As far as I can tell no other applications are running that would have
claimed ports 2 and 3.
Thanks,
Brian
 
B

BrianK

The only code missing in my question is the actual open statment which is as
follows. Exception occurs here.
hSp.Open();

Chris Tacke said:
Show us some code.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


BrianK said:
Hello,
My CE 6.0 device has 3 serial ports and the serial port class can
enumurate
them all via this method just fine.
string[] AvailablePorts = SerialPort.GetPortNames();
Also creating a serial port object is no problem for any of the 3 ports as
follows,
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits, System.IO.Ports.StopBits.One);
I can also do this,
hSp.BaudRate = 9600;
However, when I try to open any of the 3 ports(I know com1 is the debug
port) I get an exception as follows,
A first chance exception of type 'System.IO.IOException' occurred in
System.dll
I cannot open the port, what is wrong here?
As far as I can tell no other applications are running that would have
claimed ports 2 and 3.
Thanks,
Brian
 
C

Chris Tacke, eMVP

I disagree. You did a mashup of fragments, but I see no code that would
coherently demonstrate the problem. For example what actual port name are
you passing in? What names get passed back from the GetPortNames call? And
what is the exact exception message?

There are really only 2 options for the failure:

1. The port name you're using is invalid
2. The port is in use


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

BrianK said:
The only code missing in my question is the actual open statment which is
as
follows. Exception occurs here.
hSp.Open();

Chris Tacke said:
Show us some code.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


BrianK said:
Hello,
My CE 6.0 device has 3 serial ports and the serial port class can
enumurate
them all via this method just fine.
string[] AvailablePorts = SerialPort.GetPortNames();
Also creating a serial port object is no problem for any of the 3 ports
as
follows,
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits, System.IO.Ports.StopBits.One);
I can also do this,
hSp.BaudRate = 9600;
However, when I try to open any of the 3 ports(I know com1 is the debug
port) I get an exception as follows,
A first chance exception of type 'System.IO.IOException' occurred in
System.dll
I cannot open the port, what is wrong here?
As far as I can tell no other applications are running that would have
claimed ports 2 and 3.
Thanks,
Brian
 
B

BrianK

Here is the little test class I used.
This is the actual exception message that shows up in VS2005 on the 3 open
attempts.
"A first chance exception of type 'System.IO.IOException' occurred in
System.dll"


using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;

namespace KeyBrdTerminalDemo
{
public class Class_Commport
{

static string[] AvailablePorts = SerialPort.GetPortNames();
int PortCount = AvailablePorts.Length;
private string[] portsOpen = new string[32];

public Class_Commport()
{

}


public void GetCommPortInfo()
{
int i0=0;
string ComName;
int dataBits = 8, baudRate = 9600;

foreach (string tStr in AvailablePorts)
{
ComName = tStr;
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits, System.IO.Ports.StopBits.One);

try
{
hSp.PortName = ComName;
hSp.BaudRate = 9600;
}
catch {
MessageBox.Show(ComName, "Error A");
}

try
{
hSp.Open(); // exception occurs here
portsOpen[i0] = ComName + " Available";
hSp.Close();
}
catch (Exception e)
{
string Ex = e.Message;
portsOpen[i0] = ComName + " Busy";
}
i0++;
}
}

}
}


Chris Tacke said:
I disagree. You did a mashup of fragments, but I see no code that would
coherently demonstrate the problem. For example what actual port name are
you passing in? What names get passed back from the GetPortNames call? And
what is the exact exception message?

There are really only 2 options for the failure:

1. The port name you're using is invalid
2. The port is in use


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

BrianK said:
The only code missing in my question is the actual open statment which is
as
follows. Exception occurs here.
hSp.Open();

Chris Tacke said:
Show us some code.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


Hello,
My CE 6.0 device has 3 serial ports and the serial port class can
enumurate
them all via this method just fine.
string[] AvailablePorts = SerialPort.GetPortNames();
Also creating a serial port object is no problem for any of the 3 ports
as
follows,
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits, System.IO.Ports.StopBits.One);
I can also do this,
hSp.BaudRate = 9600;
However, when I try to open any of the 3 ports(I know com1 is the debug
port) I get an exception as follows,
A first chance exception of type 'System.IO.IOException' occurred in
System.dll
I cannot open the port, what is wrong here?
As far as I can tell no other applications are running that would have
claimed ports 2 and 3.
Thanks,
Brian
 
C

Chris Tacke, eMVP

So what is an *exact* string value for the port name that is getting passed
in that is failing?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com




BrianK said:
Here is the little test class I used.
This is the actual exception message that shows up in VS2005 on the 3 open
attempts.
"A first chance exception of type 'System.IO.IOException' occurred in
System.dll"


using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;

namespace KeyBrdTerminalDemo
{
public class Class_Commport
{

static string[] AvailablePorts = SerialPort.GetPortNames();
int PortCount = AvailablePorts.Length;
private string[] portsOpen = new string[32];

public Class_Commport()
{

}


public void GetCommPortInfo()
{
int i0=0;
string ComName;
int dataBits = 8, baudRate = 9600;

foreach (string tStr in AvailablePorts)
{
ComName = tStr;
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits, System.IO.Ports.StopBits.One);

try
{
hSp.PortName = ComName;
hSp.BaudRate = 9600;
}
catch {
MessageBox.Show(ComName, "Error A");
}

try
{
hSp.Open(); // exception occurs here
portsOpen[i0] = ComName + " Available";
hSp.Close();
}
catch (Exception e)
{
string Ex = e.Message;
portsOpen[i0] = ComName + " Busy";
}
i0++;
}
}

}
}


Chris Tacke said:
I disagree. You did a mashup of fragments, but I see no code that would
coherently demonstrate the problem. For example what actual port name
are
you passing in? What names get passed back from the GetPortNames call?
And
what is the exact exception message?

There are really only 2 options for the failure:

1. The port name you're using is invalid
2. The port is in use


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

BrianK said:
The only code missing in my question is the actual open statment which
is
as
follows. Exception occurs here.
hSp.Open();

:

Show us some code.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


Hello,
My CE 6.0 device has 3 serial ports and the serial port class can
enumurate
them all via this method just fine.
string[] AvailablePorts = SerialPort.GetPortNames();
Also creating a serial port object is no problem for any of the 3
ports
as
follows,
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits,
System.IO.Ports.StopBits.One);
I can also do this,
hSp.BaudRate = 9600;
However, when I try to open any of the 3 ports(I know com1 is the
debug
port) I get an exception as follows,
A first chance exception of type 'System.IO.IOException' occurred in
System.dll
I cannot open the port, what is wrong here?
As far as I can tell no other applications are running that would
have
claimed ports 2 and 3.
Thanks,
Brian
 
B

BrianK

It would be COM1, COM2 or COM3.

Chris Tacke said:
So what is an *exact* string value for the port name that is getting passed
in that is failing?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com




BrianK said:
Here is the little test class I used.
This is the actual exception message that shows up in VS2005 on the 3 open
attempts.
"A first chance exception of type 'System.IO.IOException' occurred in
System.dll"


using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;

namespace KeyBrdTerminalDemo
{
public class Class_Commport
{

static string[] AvailablePorts = SerialPort.GetPortNames();
int PortCount = AvailablePorts.Length;
private string[] portsOpen = new string[32];

public Class_Commport()
{

}


public void GetCommPortInfo()
{
int i0=0;
string ComName;
int dataBits = 8, baudRate = 9600;

foreach (string tStr in AvailablePorts)
{
ComName = tStr;
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits, System.IO.Ports.StopBits.One);

try
{
hSp.PortName = ComName;
hSp.BaudRate = 9600;
}
catch {
MessageBox.Show(ComName, "Error A");
}

try
{
hSp.Open(); // exception occurs here
portsOpen[i0] = ComName + " Available";
hSp.Close();
}
catch (Exception e)
{
string Ex = e.Message;
portsOpen[i0] = ComName + " Busy";
}
i0++;
}
}

}
}


Chris Tacke said:
I disagree. You did a mashup of fragments, but I see no code that would
coherently demonstrate the problem. For example what actual port name
are
you passing in? What names get passed back from the GetPortNames call?
And
what is the exact exception message?

There are really only 2 options for the failure:

1. The port name you're using is invalid
2. The port is in use


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

The only code missing in my question is the actual open statment which
is
as
follows. Exception occurs here.
hSp.Open();

:

Show us some code.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


Hello,
My CE 6.0 device has 3 serial ports and the serial port class can
enumurate
them all via this method just fine.
string[] AvailablePorts = SerialPort.GetPortNames();
Also creating a serial port object is no problem for any of the 3
ports
as
follows,
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits,
System.IO.Ports.StopBits.One);
I can also do this,
hSp.BaudRate = 9600;
However, when I try to open any of the 3 ports(I know com1 is the
debug
port) I get an exception as follows,
A first chance exception of type 'System.IO.IOException' occurred in
System.dll
I cannot open the port, what is wrong here?
As far as I can tell no other applications are running that would
have
claimed ports 2 and 3.
Thanks,
Brian
 
C

Chris Tacke, eMVP

Have you tried "COM1:", "COM2:" or "COM3:"?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


BrianK said:
It would be COM1, COM2 or COM3.

Chris Tacke said:
So what is an *exact* string value for the port name that is getting
passed
in that is failing?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com




BrianK said:
Here is the little test class I used.
This is the actual exception message that shows up in VS2005 on the 3
open
attempts.
"A first chance exception of type 'System.IO.IOException' occurred in
System.dll"


using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;

namespace KeyBrdTerminalDemo
{
public class Class_Commport
{

static string[] AvailablePorts = SerialPort.GetPortNames();
int PortCount = AvailablePorts.Length;
private string[] portsOpen = new string[32];

public Class_Commport()
{

}


public void GetCommPortInfo()
{
int i0=0;
string ComName;
int dataBits = 8, baudRate = 9600;

foreach (string tStr in AvailablePorts)
{
ComName = tStr;
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits, System.IO.Ports.StopBits.One);

try
{
hSp.PortName = ComName;
hSp.BaudRate = 9600;
}
catch {
MessageBox.Show(ComName, "Error A");
}

try
{
hSp.Open(); // exception occurs here
portsOpen[i0] = ComName + " Available";
hSp.Close();
}
catch (Exception e)
{
string Ex = e.Message;
portsOpen[i0] = ComName + " Busy";
}
i0++;
}
}

}
}


:

I disagree. You did a mashup of fragments, but I see no code that
would
coherently demonstrate the problem. For example what actual port name
are
you passing in? What names get passed back from the GetPortNames
call?
And
what is the exact exception message?

There are really only 2 options for the failure:

1. The port name you're using is invalid
2. The port is in use


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

The only code missing in my question is the actual open statment
which
is
as
follows. Exception occurs here.
hSp.Open();

:

Show us some code.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


Hello,
My CE 6.0 device has 3 serial ports and the serial port class can
enumurate
them all via this method just fine.
string[] AvailablePorts = SerialPort.GetPortNames();
Also creating a serial port object is no problem for any of the 3
ports
as
follows,
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits,
System.IO.Ports.StopBits.One);
I can also do this,
hSp.BaudRate = 9600;
However, when I try to open any of the 3 ports(I know com1 is the
debug
port) I get an exception as follows,
A first chance exception of type 'System.IO.IOException' occurred
in
System.dll
I cannot open the port, what is wrong here?
As far as I can tell no other applications are running that would
have
claimed ports 2 and 3.
Thanks,
Brian
 
B

BrianK

Yes, I had tried appending the colon, no go also.
I would think if this statement which returned OK, '"SerialPort hSp = new
SerialPort(ComName, baudRate,System.IO.Ports.Parity.None, dataBits,
System.IO.Ports.StopBits.One);" which includes the com port name then indeed
the comport name is OK?


Chris Tacke said:
Have you tried "COM1:", "COM2:" or "COM3:"?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


BrianK said:
It would be COM1, COM2 or COM3.

Chris Tacke said:
So what is an *exact* string value for the port name that is getting
passed
in that is failing?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com




Here is the little test class I used.
This is the actual exception message that shows up in VS2005 on the 3
open
attempts.
"A first chance exception of type 'System.IO.IOException' occurred in
System.dll"


using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;

namespace KeyBrdTerminalDemo
{
public class Class_Commport
{

static string[] AvailablePorts = SerialPort.GetPortNames();
int PortCount = AvailablePorts.Length;
private string[] portsOpen = new string[32];

public Class_Commport()
{

}


public void GetCommPortInfo()
{
int i0=0;
string ComName;
int dataBits = 8, baudRate = 9600;

foreach (string tStr in AvailablePorts)
{
ComName = tStr;
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits, System.IO.Ports.StopBits.One);

try
{
hSp.PortName = ComName;
hSp.BaudRate = 9600;
}
catch {
MessageBox.Show(ComName, "Error A");
}

try
{
hSp.Open(); // exception occurs here
portsOpen[i0] = ComName + " Available";
hSp.Close();
}
catch (Exception e)
{
string Ex = e.Message;
portsOpen[i0] = ComName + " Busy";
}
i0++;
}
}

}
}


:

I disagree. You did a mashup of fragments, but I see no code that
would
coherently demonstrate the problem. For example what actual port name
are
you passing in? What names get passed back from the GetPortNames
call?
And
what is the exact exception message?

There are really only 2 options for the failure:

1. The port name you're using is invalid
2. The port is in use


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

The only code missing in my question is the actual open statment
which
is
as
follows. Exception occurs here.
hSp.Open();

:

Show us some code.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


Hello,
My CE 6.0 device has 3 serial ports and the serial port class can
enumurate
them all via this method just fine.
string[] AvailablePorts = SerialPort.GetPortNames();
Also creating a serial port object is no problem for any of the 3
ports
as
follows,
SerialPort hSp = new SerialPort(ComName, baudRate,
System.IO.Ports.Parity.None, dataBits,
System.IO.Ports.StopBits.One);
I can also do this,
hSp.BaudRate = 9600;
However, when I try to open any of the 3 ports(I know com1 is the
debug
port) I get an exception as follows,
A first chance exception of type 'System.IO.IOException' occurred
in
System.dll
I cannot open the port, what is wrong here?
As far as I can tell no other applications are running that would
have
claimed ports 2 and 3.
Thanks,
Brian
 
C

Chris Tacke, eMVP

Yes, I had tried appending the colon, no go also.
I would think if this statement which returned OK, '"SerialPort hSp =
new
SerialPort(ComName, baudRate,System.IO.Ports.Parity.None, dataBits,
System.IO.Ports.StopBits.One);" which includes the com port name then
indeed
the comport name is OK?

I wouldn't make that assumption. I'm not certain how they're doing the work
under the hood, but the name isn't going to be validated by the system until
CreateFile is called, which I presume is only done when Open is called.

Are the drivers for these ports actually loaded and running? Again I don't
know how the port enumeration was implemented, but if they simply walked the
HKLM\Drivers\BuiltIn tree they certainly might return names of drivers that
aren't loaded and running (checking HKLM\Drivers\Active would be the way to
know).

Otherwise I'm not seeing anything obvious. Have you tried you code on
another device? You're certain nothing else is running that might be using
the ports? Have you tried to open the port in native code and see if you
get the same behavior?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
B

BrianK

Chris Tacke said:
I wouldn't make that assumption. I'm not certain how they're doing the work
under the hood, but the name isn't going to be validated by the system until
CreateFile is called, which I presume is only done when Open is called.

Are the drivers for these ports actually loaded and running? Again I don't
know how the port enumeration was implemented, but if they simply walked the
HKLM\Drivers\BuiltIn tree they certainly might return names of drivers that
aren't loaded and running (checking HKLM\Drivers\Active would be the way to
know).

Otherwise I'm not seeing anything obvious. Have you tried you code on
another device? You're certain nothing else is running that might be using
the ports? Have you tried to open the port in native code and see if you
get the same behavior?

I used this serial port class on desk top machines with great success, but
this is my first CE machine. By native code you would be referring to a
straight C++ project?
Or perhaps importing the serial functions into C# as contained in coredll.dll?
System.Runtime.InteropServices.DllImport("coredll.dll")....
Do you have any examples of using C# this way?
 
C

Chris Tacke, eMVP

I used this serial port class on desk top machines with great success, but
this is my first CE machine. By native code you would be referring to a
straight C++ project?

Yes, a simple C app that calls CreateFile with the same string to see if it
works or fails.
Or perhaps importing the serial functions into C# as contained in
coredll.dll?

That's also an option.
System.Runtime.InteropServices.DllImport("coredll.dll")....
Do you have any examples of using C# this way?

Yes, I do:
http://msdn.microsoft.com/en-us/library/aa446551.aspx

With the latest code (interface compatible with the CF libs) here:
http://opennetcf.com/FreeSoftware/OpenNETCFIOSerial/tabid/252/Default.aspx


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
D

Dick Grier

What hardware are you using? You say that COM1 is the debug port (and if
so, it may already be open, thus resulting in an exception). Also, if you
have ActiveSync running, you must disable its use of COM1 (for the same
reason).

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top