views:

698

answers:

2

I'm trying to to program a serial communication using hardware handshake in linux using C/C++. The signals that implement the handshake are CTS (Clear to send) and RTS (Request to send). Currently my function for setting the CTS signal looks as follows:

int setCTS(int fd, int value) {
    int status;
    ioctl(fd, TIOCMGET, &status); // get the current port status
    if (value)
     status |= TIOCM_CTS; // rise the CTS bit
    else
     status &= ~TIOCM_CTS; // drop the CTS bit
    ioctl(fd, TIOCMSET, $status); // set the modified status
    return 0;
}

where fd is the file descriptor for the port and value is the value to be set for the signal. To code this function I based on http://www.easysw.com/~mike/serial/serial.html#5_1.

The problem is that gcc does not recognize any of the constants used in the example. Any suggestions?

-- Update --

I've found an answer. Looking to another example, sys/ioctl.h declares the constants.

A: 

Are you including the termios.h header?

Tyler McHenry
Yes, it is included.
freitass
Try including linux/termios.h. It seems that on my system termios.h includes asm/ioctls.h which defines TIOCMGET and TIOCMSET, but only linux/termios.h includes asm/termios.h which defines TIOCM_CTS.
Tyler McHenry
+1  A: 

This may not be applicable for your particular application, but I thought I'd post it here in case it helps you or someone else searching.

On most systems with termios, you can set the CRTSCTS flag in the ->c_cflags member of the termios structure that you pass to tcsetattr, and have the kernel or hardware do the RTS/CTS flow control for you.

(It's not POSIX, but it's on both BSD and SystemV derived systems so it's almost everywhere - including Linux).

caf
I second this. You really really want to let the line-discipline handle the throttling of CTS/RTS up and down for your application.
codeDr