Serial Port Monitor Control Signals Changes [Archive] - C Board

PDA

View Full Version : Serial Port Monitor Control Signals Changes


s_Fanous
04-18-2008, 12:57 PM
Hello

I'm coding a program that needs to monitor changes in serial port control signals (cts, rts, dtr, dcd), to be more specific I'm interested in cts changes.

Is there any blocking system call that can be called, and only return when a specified control line is set or unset.

The only other way I can think of doing it is an infinite loop



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

if (status & TIOCM_CTS)
cts_set = 1;

while (do_work)
{
ioctl(fd, TIOCMGET, &status);

if ((status & TIOCM_CTS) && (!cts_set))
{
// CTS was unset, now set
cts_set = 1;
// Do work
}
else if ((!(status & TIOCM_CTS)) && (cts_set))
{
// CTS was set, now unset
cts_set = 0;
// Do work
}

usleep(200000);
}


I know this is not a good way of doing it, and hope there is some sort of blocking system call that would just return when the control signal I'm interested in changes value.

Thanks in advance

matsp
04-18-2008, 01:13 PM
Normally, CTS is used to signal that it's "clear to send" (That's what CTS stands for), whcih means that if you write() to the port, it won't send the data until CTS is "on". There's really no need for your software to check CTS manually. Unless of course you are using CTS in a non-standard way.

--
Mats

s_Fanous
04-19-2008, 03:05 AM
Unless of course you are using CTS in a non-standard way.

Which is the case in my situation. There's no flow control.

Salem
04-19-2008, 04:10 AM
Lots of good info here - http://www.lvr.com/serport.htm
http://www.tldp.org/HOWTO/Serial-HOWTO.html

s_Fanous
04-19-2008, 04:50 AM
Lots of good info here - http://www.lvr.com/serport.htm
http://www.tldp.org/HOWTO/Serial-HOWTO.html

Hello Salem

Thanks for the links, but they don't contain what I'm looking for. Perhaps if I provide further information on why I need this it might help.

I'm communicating with a smart card reader connected to the serial port. When a smart card is inserted in the reader the CTS line is set. When the card is removed the CTS line is unset. I want to be able to identify when these events occur.

I want to do it efficiently, so if possible I don't want to waste CPU cycles. Moreover, in my sample code above, during my usleep, the card might be removed and inserted very quickly, when the thread wakes up it will still find the CTS line set, however it would have missed the fact that the card was removed and inserted.

Thanks again for your help

Salem
04-19-2008, 07:02 AM
> I want to do it efficiently, so if possible I don't want to waste CPU cycles.
Have you actually tried to measure how much CPU time this loop uses?

> the card might be removed and inserted very quickly
Yeah, I'd reduce the timeout to 100000 in the usleep call to make that all but impossible.
http://en.wikipedia.org/http://cboard.cprogramming.com/showthread.php?t=101976#post743750
Serial Port Monitor Control Signals Changes - C Boardwiki/Human%27s_periods

Short of hacking the driver to provide a "wait for cts change ioctl", the answer you have seems reasonable. Even then, you may end up doing your polling in the driver instead of polling in your program, and be no better off.
It really depends on the capabilities of the UART in your machine.

An example discussion is here
http://xinu.mscs.mu.edu/UART_Driver
If you read the data sheet, it does indicate that an interrupt CAN be generated on a CTS change.