Implementing Serial Communications

This procedure outlines how to implement serial communications in an application. With the exception of step 3, the procedure is identical for all three approaches.

    To use serial communications
  1. Determine which COM port you need to open.

    Port numbers are stored in the \HKEY_LOCAL_MACHINE\Drivers registry key. Active drivers are listed in the \HKEY_LOCAL_MACHINE\Active registry key.

  2. Call CreateFile with lpFileName set to the COM-port name, for example,"COM1:."

    The colon (:) is part of the port name and must be included. Set the lpSecurityAttributes parameter to NULL and the dwFlagsAndAttributes parameter to zero.

  3. For raw IR transmission, place the port in IR mode by calling EscapeCommFunction with hFile set to the handle returned by CreateFile and dwFunc set to SETIR.
  4. Call SetCommTimeouts to set the communication timeouts.
  5. Call the ReadFile and WriteFile functions to transmit and receive serial data.

    When one thread is waiting for a ReadFile function to return, ReadFile calls issued by other threads are blocked until the initial ReadFile call returns. The same is true for the WriteFile function.

  6. Call CloseHandle to close the serial port.