Re: capturing output from running process crashes gtk2



On Tue, 1 Aug 2006 10:30:25 +0200 
"Ratcliffe, Jeffrey (Peters)" <Jeffrey Ratcliffe External eads com> wrote:

I am new to Perl and gtk2-perl, but not to awk, C, etc, or to GUI programming.

I am trying to use my scanner via a gtk2-perl script. As I can't find a Perl wrapper for sane, I am having 
to use scanimage.

scanimage writes info about which page is being scanned to stderr. I'd like to capture this, but my various 
attempts crash gtk2.

I've reduced the code to the test example below. Can anyone give me any pointers on how to achieve what I 
want (or give me a Perl wrapper for sane)?

Thanks
Jeff


I played around with it a bit, and wondered why you wanted to capture STDERR.
On my scanimage, I get messages to STDERR only if I use batch-mode, and it's 
prompt for new material requests. If I don't use batch mode, and use  backticks,
I can capture it directly.  Now if you capture it directly to memory, you can then
create your own prompts and error messages. The only drawback to this, is that
it will give an error if the scanner isn't reset completely (light returned to rest) between
loop runs. So it's really only good for a 1 shot scan.

#!/usr/bin/perl
use warnings;
use strict;

my $count = shift || 3;

# scanimage -L for a list of scanners
my @options = ( '-d umax:/dev/scanner',  '--format=tiff',   );

foreach my $i ( 1 .. $count ){
   print "Please place item $i to be scanned\n";
   print "Hit Enter when ready\n";
   <>;
   my $image =  `scanimage @options` ;
   open (FH,"> test$i.tiff");
   print FH  $image;
   close FH
}
__END__


If you use IPC::Open3, you can get some good results. The only glitch
is that getting a Control - D is tricky to intercept, so I changed it to Control-C.

Granted, there is no Gtk2 code in this, but it a way to run scanimage interactively.
You should be able to put the STDERR messages up in a notification box, and write it
to a file simultaneously.

#!/usr/bin/perl
use warnings;
use strict;
use IPC::Open3;
use IO::Select;

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" );

my $sel = new IO::Select();
$sel->add(\*ERROR);

while ( my @ready = $sel->can_read() ) {
     foreach my $fh (@ready) {
          my $buf = '';
          if ($fh eq \*ERROR) {
            sysread(ERROR,$buf,4096);
            if ( $buf =~ /(.*)Press Ctrl \+ D to terminate(.*)/ ){ 
                 #change message to Control C, since I can't get ^D to go
                 $buf =~ tr/D/C/;
                 print $buf;
               my $query = <STDIN>;
               print WRITE $query;
            }else{ print $buf}
          
           }
    }
}

waitpid($pid, 1);
__END__ 


-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]