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

I'm using modem dial to dial a number, however I want to cancel the dial when pressing a the hangup button.

I have written the following code, but it does not hangup when I click on hangup, it just continues dialing.

Help Please
use Tk; use Device::Modem; my $mw = new MainWindow; my $title = $mw -> Label(-text=>"Enter Start Number:") -> pack(); my $inputnumber = $mw -> Entry(-textvariable => \$number) -> pack(); my $but = $mw -> Button(-text => "Start Dialing",-width=>15, -command +=>\&push_button)-> pack(); my $but1 = $mw -> Button(-text => "Hangup F1",-width=>15, -command =>\ +&push_button1)-> pack(); my $txt = $mw -> Scrolled('Text',-scrollbars=>"oe") -> pack; $mw->bind( '<Key-F1>' => \&push_button1 ); my $modem = Device::Modem->new( port => 'COM1' ); if( $modem->connect( baudrate => 19200 ) ) { $txt -> insert('end',"Modem is Connected on COM1\n"); } else { $txt -> insert('end',"Can't Connect to Modem\n"); } MainLoop; sub push_button { $txt -> insert ('end',"Dialing $number\n"); $modem->dial($number); } sub push_button1 { $txt -> insert('end',"Hanging Up\n"); $modem->hangup(); }

Replies are listed 'Best First'.
Re: perl tk and Device::Modem
by zentara (Cardinal) on Apr 25, 2006 at 20:06 UTC
    With the exception of not using warnings and strict, it SEEMS like you are doing it right. Sadly, I have a DSL modem now, and will have to test this later on a different machine. BUT, there are some things you can try.

    If you look at the dial method in Modem.pm, it incorporates a timeout, and it probably is "blocking the gui" while it dials and waits for the timeout to connect. So during that time Tk is blocked. A possible solution, is not to use the dial method, and manually send out each digit at a time, and in-between let Tk do a 1-loop, to see if you hit the cancel button. Something like:

    use Tk::Event qw(DONT_WAIT); sub push_button { $txt -> insert ('end',"Dialing $number\n"); #$modem->dial($number); my (@n) = $number =~ /\d/g; foreach my $d (@n){ $modem->atsend( 'ATDT'. $d . Device::Modem::CR ); DoOneEvent(DONT_WAIT); } }

    I'm not really a human, but I play one on earth. flash japh
      Thanks for the reply.
      Really, I would like the option to hangup after dialing (when ringing/answer phone). It seems that the gui is locked until the modem has finished dialing and an answer is returned.
        You might want to experiment with the timeout setting, like setting it to 1 (it defaults to 30 seconds ). There is a way to do what you want, you may have to resort to manually accessing the modem thru directly opening it and writing AT commands to it. If you read the Modem.pm file, it shows exactly what it sends. Also try $modem->reset() instead of $modem->$hangup().

        People are starting to forget how to use the old phone-line modems. :-)

        Instead of using Device::Modem, you can use Device::SerialPort directly. There are some example in the eg directory of Device::SerialPort, and you should be able to do what you want. As a matter of fact the demo7.plx is a Tk script to dial out on a modem

        If my memory servers me correctly, I recall that you can send modem dialing commands with shell commands, like

        echo 'ATH0' > /dev/ttyS0 #go offhook echo 'ATDT123456789' > /dev/ttyS0 # dial
        There is a hangup command too, but I can't recall it offhand. So you could rewrite your script to use a more manual method of dialing.

        I'm not really a human, but I play one on earth. flash japh
Re: perl tk and Device::Modem
by rodion (Chaplain) on Apr 25, 2006 at 21:42 UTC
    Try doing
    $txt->attention; sleep(2); $txt->hangup;
    and see if that works. There is a call to attention() built into hangup(), but I looked at the code and I don't see an effective delay after attention() sends '+++' to the modem. There is just a comment about waiting "200 milliseconds". (If this code change gets it working, pare down the 2 secondsin sleep(). It's that big just for a test.)