stephane has asked for the wisdom of the Perl Monks concerning the following question:
Currently I have a problem catching (or handling) SIGIOs in a Perl script. What I would like to achieve, is to have a small script reading and writing to a serial port whithout the need to poll it every now and then to see if there is indeed something to read.
The code here under opens a serial port, uses POSIX and Fcntl to configure various parameters (baud rate, etc.) and to put it in asynchronous mode.
A SIGIO handler is set a the beginning of the script, which right now just prints out a string when an IO signal has been caught. sigtrap is there just in case other type of signals are sent (apperently not the case anyway).
Once all that is done ;-) it will write something on the line and wait for the signal(s) - I verified that the device indeed answers ("OK" in this case).
The problem is: no SIGIOs are sent back..
use POSIX; use Fcntl; use sigtrap qw(die untrapped); sub SIGIO_Handler { my $signame = shift; print "Somebody sent me a SIG$signame\n"; } $SIG{IO} = \&SIGIO_Handler; $| = 1; open(LINK, "+</dev/ttyS0") || die "Can't connet to the device: $!"; my $termios = new POSIX::Termios; $termios->getattr( fileno(LINK) ); my $c_cflag = $termios->getcflag; my $c_lflag = $termios->getlflag; $c_cflag |= (CLOCAL | CREAD | CS8); $c_cflag &= ~(PARENB); $c_cflag &= ~(CSTOPB); $c_cflag |= (CSIZE); $c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); $c_oflag &= ~(OPOST); $termios->setcc(VMIN, 0); $termios->setcc(VTIME, 10); $termios->setcflag($c_cflag); $termios->setlflag($c_lflag); $termios->setoflag($c_oflag); $termios->setispeed(B2400); $termios->setospeed(B2400); $termios->setattr( fileno(LINK), TCSAFLUSH); # -- Register "myself" ($$) to catch SIGIOs. fcntl(LINK, &Fcntl::F_SETOWN, $$); fcntl(LINK, F_SETFL, O_ASYNC); print LINK "AT\r"; while (1) { # -- Just wait and ... # -- see if any SIGIO arrives };
More precisely, if the script just waits (as it does in the above code) no SIGIOs will be catched. Instead, if (at some point) I start reading what is on the port (meaning: modify the script to actualkly read something at some point).. then the SIGIO_Handler will be used.
So, I dont want the signals to tell me that I am reading on the port.. I want them to tell me that there is something to read in the first place! (before I actually read it "myself").
Can somebody provide any hints/advice?
How can properly catch SIGIOs?
Thanks!
P.S.: by the way, I don't want to use the Device::SerialPort since I have the feeling that it should be possible through POSIX and Fcntl and they are in the standard distribution, or even Win32::SerialPort for the allready given reasons, plus.. Im on Linux (kernel v2.2)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Handling I/O Signals
by count0 (Friar) on Mar 13, 2002 at 21:45 UTC | |
|
Re: Handling I/O Signals
by traveler (Parson) on Mar 13, 2002 at 22:04 UTC | |
by stephane (Monk) on Mar 14, 2002 at 18:09 UTC | |
by traveler (Parson) on Mar 14, 2002 at 18:47 UTC | |
by stephane (Monk) on Mar 15, 2002 at 09:49 UTC | |
by traveler (Parson) on Mar 15, 2002 at 15:38 UTC |