Hi All,

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)


In reply to Handling I/O Signals by stephane

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.