SerialPort.Close Method ()

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Closes the port connection, sets the IsOpen property to false, and disposes of the internal Stream object.

Namespace:   System.IO.Ports
Assembly:  System (in System.dll)

member Close : unit -> unit

Exception Condition
IOException

The port is in an invalid state.

- or -

An attempt to set the state of the underlying port failed. For example, the parameters passed from this SerialPort object were invalid.

Calling this method closes the SerialPort object and clears both the receive and transmit buffers. This method calls the Component.Dispose() method, which invokes the protected SerialPort.Dispose(Boolean) method with the disposing parameter set to true.

The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly.

The following code example demonstrates the use of the SerialPort class to allow two users to chat from two separate computers connected by a null modem cable. In this example, the users are prompted for the port settings and a username before chatting. This code example is part of a larger code example provided for the SerialPort class.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Return to top
Show: