COM Port disappears

R

romain taillandier

Hi group,

I have a Mobile Compia M3+ device. I have some peripheral on CF type
I. I open the port, and discuss quite good with the peripheral, and
when i am finish, i close the port (fully normal until there)

Now if i try to open he port i have an exception :

The port COM1 does not exist.
At system.IO.Ports.SerialStream.WinIOError()
At system.IO.Ports.SerialStream.ctor()
At system.IO.Ports.SerialPort.Open()

So i never could re-open the port.
How it could happen that the port does not exist ? I was just use it a
few second before.
I understand when it has been bad closed, or unvailable, but here it
is simply disapears

But if i power down it (simple button on/off to put idle, no reboot),
i can re-open it, so it have been re-created by the OS i suppose...

Any idea ?
thank you all!
 
G

Guest

I'm betting that the error text is just misleading. Underneath it's likely
getting an error code 55 indicating that the port is still in use. Cycling
power "fixes" it because the OS probably closes and reopens the driver, or
at least the UART.

My bet is that the port needs to be Disposed, not just closed.
 
R

romain taillandier

New precisions :

If the port is open, and i try to reopen it i get the following
exception :
InvalidOperationException : The port is already open
At system.IO.Ports.SerialPort.Open()

then the port is broken, i can't read or write anymore.

if i do exactly the same a second time, i get the following :
System.IO.IOException : The port COM1 does not exist.
At system.IO.Ports.SerialStream.WinIOError()
At system.IO.Ports.SerialStream.ctor()
At system.IO.Ports.SerialPort.Open()
 
G

Guest

If the port is open, and i try to reopen it i get the following
exception :
InvalidOperationException : The port is already open
At system.IO.Ports.SerialPort.Open()

then the port is broken, i can't read or write anymore.

You've probably reassigned the global variable and it's now null or invalid.
You might want to check to see if it's open before trying to reopen - or use
a temporary variable to prevent clobbering the original.
if i do exactly the same a second time, i get the following :
System.IO.IOException : The port COM1 does not exist.
At system.IO.Ports.SerialStream.WinIOError()
At system.IO.Ports.SerialStream.ctor()
At system.IO.Ports.SerialPort.Open()

Again, did you Dispose after Closing?
 
R

romain taillandier

You've probably reassigned the global variable and it's now null or invalid.
You might want to check to see if it's open before trying to reopen - or use
a temporary variable to prevent clobbering the original.

Chris,
First, thank you for your help.

my code was like this :
Port.open()
Port.open()
Port.open()

For this second test, i effectively reopen the same port in the same
variable, that was the test. I just want you to see that if i reopen
an already opened port, i get the "Already opened" Exception, but if i
reopen it again (for the third time in the same variable), i get the
"does not exist" exception. (may be this say nothing). Why i get 2
different exception for the same error ?


But i have already a problem by closing the port. If i close it, i can
never re-open it until i turn the device off.
like this :
Port.open()
Port.Close() // or Port.Dispose()
Port.open() // Port does not exist exception

The close method of the system.IO.Port.SerialPort calls the
dispose(true) method (used reflector for knowing this). So closing or
disposing is the same for this class.
I work with a C# program, i have no global variable, and if i close
the form (a very simple form with a com port) the dispose method of
the form call the dispose method of the port. the port is a
system.IO.Port.SerialPort, standart CF2 port.
I also try to close the port in another thread (see this posts :
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1534447&SiteID=1),
but it does not work for me.
I have try also to add GC.Collect, and GC.WaitForPendingFinalizers,
with no result.

I have make a lot of test, I am now quite sure that the port is bad
closed.
So how can i better close it ?

ROM
 
R

romain taillandier

I have created a very simple form the code is following.
1 serial port
1 button to open the port
1 buton to close the port

nothing else for the moment

I do this :
- i shut down then up the device
- run the app
- press open button : "Ok"
- press close button : "Ok";
- press open button : "Port does not exist exception"

- i shut down then up the device
- press open button : "Ok"
- press close button : "Ok";
- press open button : "Port does not exist exception"

What can i do more than just closing the port by close or dispose (are
the same) ?


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnOuvrir_Click(object sender, EventArgs e)
{
try
{
this.serialPort1.Open();
MessageBox.Show("ok");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void btnFermer_Click(object sender, EventArgs e)
{
try
{
this.serialPort1.Close();
this.serialPort1.Dispose();
System.Threading.Thread.Sleep(1000);
MessageBox.Show("ok");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void InitializeComponent()
{
this.serialPort1 = new
System.IO.Ports.SerialPort(this.components);
//
// btnOuvrir
//
...
//
// serialPort1
//
this.serialPort1.BaudRate = 115200;
this.serialPort1.Parity = System.IO.Ports.Parity.Even;
this.serialPort1.StopBits = System.IO.Ports.StopBits.Two;
//
// btnFermer
//
...
//
// Form1
//
...

}
}
 
G

Guest

Without hardware, I'm out of ideas. Try another device of a different model
and see if the behavior remains - that will tell you if the problem is in
your code, the CF or the device's driver.
 
R

romain taillandier

thanks chris,

I use the system.IO.Ports.SerialPort since it exists, and other serial
class home maid (using partially opennetcf), from years, i have never
seen something like that on any device i have try.
I have test this code (the exactly same code) on all the device i have
here (7 devices from 5 differents model), all is fine.
I have also try the previous version of the same PDA which is in CE
4.2, and it is work !
I have also try with my home maid serial class, with same results.

I am quite sure the problem is from the device constructor. I don't
know if it is hard or soft.

Any other workaround appreciated
 

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