How to read the CTS and DSR of RS232 in Unix using C language?

Hello to all Gurus out there,
Could you show me a source code in Unix platform using C language. I want to read the status or voltage level of the DSR and CTS.

Thanks a lot,

Swing5

Please click on the rules link at the bottom of the page. I have deleted your other thread.

You're not going to be able to do what you want. Use the command "man termios" to see what is available from the terminal driver. Use our search function to searc this forum to see some examples of termios in use.

In you have the hardware, you can use modem control signals, but the driver does it on your behalf. For an incoming line, a open() call raises DTR which signals the modem that it may auto-answer a call. When it does, it will raise DSR which causes the open to complete. If DSR drops, a hup signal is generated. A very clever program might be able to use all of this to "read" the state of the DSR signal. But open() is an expensive system call and the bandwidth would be low.

Some unix systems (maybe most? or all?) you can use CTS for hardware flow control of output, and RTS for hardware flow control of input. There is no hope for RTS, but a clever program might be able to determine the state of CTS by seeing if a non-blocking write() succeeds. But DSR would need to be high.

You might be able to have a simple switch send a break character. Anyone who uses a Sun with a terminal console knows how easy that is. You can arrange for a break character to send an INT signal to the program.

Something like the following will probably do the trick:

bool getCTS(int fd)
{
    int s;
    /* Read terminal status line: Clear To Send */
    ioctl(fd, TIOCMGET, &s);
    return (s & TIOCM_CTS) != 0;
}
bool getDSR(int fd)
{
    int s;
    /* Read terminal status line: Data Set Ready */
    ioctl(fd, TIOCMGET, &s);
    return (s & TIOCM_DSR) != 0;
}