Sophia S.
Sophia S.

Reputation: 331

Opening a serial RS232 Port in C with Function CreateFile(..)

   HANDLE hPort = CreateFile(
    COM1,
    GENERIC_READ,
    0,
    NULL,
    OPEN_EXISTING,
    0,
    NULL
    );

Will this Function return a value unless zero when no device is connected to the COM port?

Upvotes: 2

Views: 711

Answers (1)

Ross Ridge
Ross Ridge

Reputation: 39631

It's not clear what exactly the question you're asking actually is, but the CreateFile call in your example will only fail for one of the two following reasons:

  • There is no COM1 device. This can be because there's no physical serial port in system, Windows failed to detect it, or Windows assigned it a different COM port number.
  • The COM1 device has already been opened. Windows only allows a serial device to be opened once. Any further attempts to open the port will fail.

When CreateFile fails it will return INVALID_HANDLE_VALUE. The call will not fail because there's is no cable or device attached to the serial port. Windows can only detect whether the serial port itself is present, it can't generally detect whether something is connected to the serial port.

Upvotes: 2

Related Questions