2

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;
Fifi
  • 3,360
  • 2
  • 27
  • 53
  • DeviceIoControl() [this ioctl code](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntddser/ni-ntddser-ioctl_serial_get_dtrrts). It is fairly risky, a driver might not implement it. – Hans Passant Dec 21 '19 at 21:02

2 Answers2

1

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.

Mr Robot
  • 1,037
  • 9
  • 17
0

Unless you are actively changing the signal line, is the value set in DCB used?
Other than that, you control the signal line yourself, so you should remember it each time you change it.

As long as you have the serial port open, you have all control and nothing else will change.
Isn't there anybody who uses handshake or toggle mode now?

SetDefaultCommConfigW function

BOOL SetDefaultCommConfigW(
  LPCWSTR      lpszName,
  LPCOMMCONFIG lpCC,
  DWORD        dwSize
);

SetCommConfig function

BOOL SetCommConfig(
  HANDLE       hCommDev,
  LPCOMMCONFIG lpCC,
  DWORD        dwSize
);

GetCommConfig function

BOOL GetCommConfig(
  HANDLE       hCommDev,
  LPCOMMCONFIG lpCC,
  LPDWORD      lpdwSize
);

COMMCONFIG structure

typedef struct _COMMCONFIG {
    ...
  DCB   dcb;
    ...
} COMMCONFIG, *LPCOMMCONFIG;

DCB structure

typedef struct _DCB {
  DWORD DCBlength;
    ...
  DWORD fDtrControl : 2;
    ...
  DWORD fRtsControl : 2;
    ...
} DCB, *LPDCB;

DTR_CONTROL_DISABLE   0x00
DTR_CONTROL_ENABLE    0x01
DTR_CONTROL_HANDSHAKE 0x02

RTS_CONTROL_DISABLE   0x00
RTS_CONTROL_ENABLE    0x01
RTS_CONTROL_HANDSHAKE 0x02
RTS_CONTROL_TOGGLE    0x03

If you still want to do so, use DeviceIoControl() commented by @Hans Passant.
However, there is no guarantee that it is properly supported, since most people will not use it.

Device Input and Output Control (IOCTL)
DeviceIoControl function

The following is a sample DeviceIoControl call for a DISK drive, but you can call it by changing each of these parameters to those related to IOCTL_SERIAL_GET_DTRRTS for the serial port.
Calling DeviceIoControl

Serial Device Control Requests
IOCTL_SERIAL_GET_DTRRTS IOCTL

kunif
  • 4,060
  • 2
  • 10
  • 30
  • I used the serial COM without handshake. I already remember the state of the pin each time I change it but it is not fully satisfying. Your answer is not clear and do not clarify my question. Is it possible Yes or No? If yes, please provide an example code. – Fifi Dec 23 '19 at 15:09
  • The question doesn't give details on what the "not fully satisfying" situation is. Then no one will be able to answer. And my answer is that unless you use another means, for example, an RS232C line monitor, protocol analyzer, or oscilloscope, the exact state of the standard COM port API is not known. All you can do is remember what you have specified and make decisions based on it. – kunif Dec 23 '19 at 15:29
  • "All you can do is remember what you have specified and make decisions based on it." => Source ? By the way, it is possible under Linux. – Fifi Dec 23 '19 at 17:00
  • Because it is not described in Microsoft's API documentation. [Communications Functions](https://learn.microsoft.com/en-us/windows/win32/devio/communications-functions) For DTR/RTS, there is only API to get DCB configuration. If you are a device driver creator, you will be the implementer of the IOCTL commented by @Hans Passant, not the user. And if you are an application programmer, DTR/RTS is an output pin, and if it is in a normal state, it will work as you specified, so if you remember what you specified, you do not need to get that state. – kunif Dec 23 '19 at 22:47