Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-25 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found