zentara has asked for the wisdom of the Perl Monks concerning the following question:

Hi, someone on the Gtk2-perl maillist got me thinking about how to run my scanner thru IPC. In the following code, my attempts to send a Control-d to the WRITE filehandle, piped to scanimage, never seems to cancel the scan. It accepts it as an Enter and continues the scan. I've uncommented the method shown by expect, using "\cd", but none of them work. Does anyone know how to force that control-d to go thru the WRITE pipe?

I know I could just kill the $pid, but the control-d works from the command-line operation of scanimage, and I would like to know why it dosn't go thru IPC.

#!/usr/bin/perl use warnings; use strict; use IPC::Open3; use Tk; my $mw=tkinit; my $start_but = $mw->Button(-text => 'Start Scan', -command => \&start_scan)->pack(); my $cancel_but = $mw->Button(-text => 'Cancel Scan', -command => \&stop_scan)->pack(); MainLoop; ################################### sub start_scan{ # do a 'scanimage -L ' for your list my @options = ( '-d umax:/dev/scanner', '-b', '--format=tiff', '--batch-count=3', '--batch-prompt', ); #interface to scanimage my $pid = open3(\*WRITE, 0 ,\*ERROR, "scanimage @options" ); $mw->fileevent('ERROR', 'readable' => \&collect_stderr); } ############################################################### sub collect_stderr { my $err = <ERROR>; print $err; } ############################################################### sub stop_scan{ # print WRITE chr(4),"\n"; # print WRITE "\x04\n"; print WRITE "\cd"; # print WRITE "\004\n"; }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re: sending a control signal to a scanner
by Fletch (Bishop) on Aug 01, 2006 at 20:41 UTC

    Already been shown what to do, but as to why:

    Control-D (\cD, 0x4, call it what you will) sent on a TTY causes end of file to be set on the descriptor (by default; technically of course you can use stty or the corresponding underlying termios calls to change the eof character to whatever you'd like). If the child process isn't running on a PTY then sending it a Ctrl-D is just like sending it any other character.

Re: sending a control signal to a scanner
by Hue-Bond (Priest) on Aug 01, 2006 at 20:29 UTC
    the control-d works from the command-line operation of scanimage

    IIUC, hitting Ctrl-D doesn't send a Ctrl-D character, but closes the file:

    $ cat > file <press ^D> $ cat file $ _

    This results in a file 0 bytes long. So, have you tried to close the filehandle instead of trying to send it a Ctrl-D?

    --
    David Serrano

      You are right!. "close WRITE"; did it. Of course I have to clean up the ERROR filehandle which then goes bezerk in the fileevent. Thanks.

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum