I don't have a direct answer to your question, but I hope this may be of some help.
A nice document about serial programming:
http://www.easysw.com/~mike/serial/
Some code I used once to wait (sleeping) for one (or more) serial control lines to change state (from hi->low, or low->hi). IRCC this isn't mentioned in the document above, I gues because its pobably Linux-specific.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <linux/serial.h>
/* ... */
int fd;
unsigned int status, oldstate, newstate, changed, mask;
/* Assuming fd is an open file descriptor for (one of) the
* serial port(s) (/dev/ttyS[0-9]).
*/
/* To wait for *any* of the 4 control lines to change state,
* use this instead of the line below:
* mask = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS | TIOCM_RNG;
*/
mask = TIOCM_RNG;
/* Get current status of the control lines in mask */
if (ioctl(fd, TIOCMGET, &status) == -1) {
perror("ioctl(TIOCMGET)");
exit(1);
}
oldstate = status & mask; /* Save current status of control lines */
/* The ioctl() call below sleeps, and does not return until one
* of the serial control lines specified in mask changes state,
* or it was interrupted by some signal.
*/
rewait: /* Goto-label for when we get a signal. */
if (ioctl(fd, TIOCMIWAIT, mask) == -1) {
if (errno == EINTR) {
/* Signal received. Handle this case. Here we're
* just restarting the ioctl-wait call.
*/
goto rewait;
} else {
/* Error condition */
perror("ioctl(TIOCMIWAIT)");
exit(1);
}
}
/* Get new status of the control lines in mask */
if (ioctl(fd, TIOCMGET, &status) == -1) {
perror("ioctl(TIOCMGET)");
exit(1);
}
newstate = status & mask;
/* XOR old with new status to find the one(s) that changed */
changed = newstate ^ oldstate;
/* Do something with the result of al this */
if (changed & TIOCM_RNG) { /* RNG line has changed */
if (newstate & TIOCM_RNG) {
printf("RNG went from: 0 --> 1\n");
} else {
printf("RNG went from: 1 --> 0\n");
}
}