close existing port using Ports.SerialPort component

R

ricolee99

Hi Everyone,

I'm using the System.IO.Ports.SerialPort component to attempt to kill
an existing component opened by another application.

I use the following code:

SerialPort serialPort = new SerialPort ("COM1");

if(serialPort.IsOpen)
{
serialPort.Close();
}


I cannot for the life of me close the opened connection. Each time I
create a dial up connection manually on COM 1, I run this code while
the connection is open. Each time, the serialPort.IsOpen is set to
false even though the dial up connection is open.

Can someone tell me how I can kill or close the COM 1 port if something
has it opened?

Thanks in advance

Eric
 
E

Eric

I cannot for the life of me close the opened connection. Each time I
create a dial up connection manually on COM 1, I run this code while
the connection is open. Each time, the serialPort.IsOpen is set to
false even though the dial up connection is open.

Have you tried closing it without testing it first? Maybe do this in a
try/catch and ignore an exception that might be thrown if it was really
closed.

I bet RAS has opened the port, and .NET doesn't know that it's open.
Are you sure you want to control the port yourself? If you merely want
an internet connection you should probably go through RAS.

See here:
<http://groups.google.com/group/micr...t&q=RAS+c#+dial&rnum=1&hl=en#9b30f797a59faa58>
 
R

ricolee99

Hi Eric,

Thanks for the response. You're right, I am using the win32 RAS, to
programmatically open the internet connection. I disconnect the
connection using RAS when file copying across the network is done. The
problem is on the client side, the internet connection is unreliable,
meaning anytime the connection can just drop. I simulate this by
opening the internet connection using RAS, and then manually cancelling
the connection under "Network Connections" window from admin tools.
The problem is that since the RAS connection did not disconnect
gracefully, the computer still thinks the port is open (I'm getting a
633 connection still in use error when I connect ). I'm trying to use
the Serial Port to close this open connection.

I've also tried closing it without checking if port is opened. It
tells me that it successfully closed the port, but yet the internet
connection is still there.
 
E

Eric

The problem is that since the RAS connection did not disconnect
gracefully, the computer still thinks the port is open

If you were on Win98 you could directly access the UART and force it
closed.

I'd seriously look at the RAS API (via pinvoke). I think there's a way
to force the port closed that way. As I recall, it was always a pain to
detect if the port was really open or closed, but I think you can force
it closed regardless of it's current status. But you may have to do
that through the RAS API, and not the WinNT SerialPort device control
mechanism. The problem with the WinNT model is that you can't close a
port you didn't open.

But maybe someone else can give you more specific assistance. Anyone?
 

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