Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

Call the CreateFilefunction to open a serial port.

Because hardware vendors and device driver developers can give any name to a port, an application should list the available ports and enable users to specify the port to open.

If a port does not exist, CreateFilereturns ERROR_FILE_NOT_FOUND, and users should be notified the port is not available.

To open a serial port
  1. Insert a colon after the communication port pointed to with the first parameter, lpzPortName.

    For example, specify COM1: as the communication port.

  2. Specify zero in the dwShareModeparameter unless you are using a driver that supports multiple opens.

    Only one device can perform read/write or any operation that changes the behavior of the port. Other applications can perform operations such as port monitoring or controlling line status.

  3. Specify OPEN_EXISTING in the dwCreationDispositionparameter.

    This flag is required.

  4. Specify zero in the dwFlagsAndAttributesparameter.

    Windows Embedded CE supports only nonoverlapped I/O.

The following code example shows how to open a serial communications port.

Copy Code
// Open the serial port.
  hPort = CreateFile (lpszPortName, // Pointer to the name of the
port
					GENERIC_READ | GENERIC_WRITE,
									// Access (read-write) mode
					0, 		// Share mode
					NULL, 	 // Pointer to the security
attribute
					OPEN_EXISTING,// How to open the serial port
					0, 		// Port attributes
					NULL); 	// Handle to port with
attribute
									// to copy

Before writing to or reading from a port, configure the port.

When an application opens a port, it uses the default configuration settings, which might not be suitable for the device at the other end of the connection.

See Also