Search code examples
windowsserial-portwindev

Getting DTR and RTS pin of serial port in C on Windows platform


How to get DTR and RTS status of serial port on a windows platform? I want to read the current state (ON or OFF) of these two pins.

I can set pins with :

EscapeCommFunction(hSerial,SETRTS);

But I don't know how to read the pin status.

Since on Linux, it can be done with the following code, I assume it is technicaly feasable:

int status=0;
ioctl(fd, TIOCMGET, &status);
return status & TIOCM_RTS;

Solution

  • Using inc\api\ntddser.h API and winioctl.h, you can access DTR and RTS status. Call DeviceIoControl, set the second parameter to IOCTL_SERIAL_GET_DTRRTS:

    Call:

    DeviceIoControl(
        handle, // handle returned by CreateFile
        IOCTL_SERIAL_GET_DTRRTS,
        NULL,
        0,
        &Status, // pointer to a DWORD variable 1
        sizeof(Status),
        &unused, // pointer to a DWORD variable
        pOverlapped // optional pointer to overlapped buffer (may be NULL)
    );
    

    Documentation about DeviceIoControl here.