www.webdeveloper.com
Results 1 to 8 of 8

Thread: Device::Gsm Perl mod probs

  1. #1
    Join Date
    Jan 2003
    Location
    Sydney, Australia
    Posts
    4

    Unhappy Device::Gsm Perl mod probs

    Hi,

    I'm afraid I am also a bit of a newbie...

    I am writing a perl script that recieves the contents of a form using POST, looks at the contents and if a checkbox is ticked it will send an SMS from a mobile phone on ttyS0. I am using Device::Gsm to handle the SMS. I can send an SMS just fine using the example script that came with the module, but when I put the appropriate code in my perl script it does not work...
    Relevant code follows:

    Code:
    #! /usr/bin/perl
    use strict;
    use Device::Gsm;
    use CGI qw(:standard);
    use MIME::Lite;  #Script also sends emails
    
    my ($number, $content);
    
    <snip!>
    if ($in{sendsms}) {
         $number = "0421444444";
         $content = "Hello.";
         &send_sms;
         print p("SMS was sent");
    <snip!>
    
    sub send_sms {
    
         my $gsm = Device::Gsm->new ( port => '/dev/ttyS0', pin => '0000');
         $gsm->connect ( baudrate => 57600); #sounds high but is rqd for my mobile to work
         my $ok = $gsm->send_sms(content=>$content, recipient=>$number, class=>'normal');
         return ;
    }
    The script includes a small conditional in the above subroutine that outputs a confirmation message if ($ok) but an error otherwise. When I run the example from the commend line it works ok, but when I run the copied script within my larger perl program it doesn't work and I get the error message.

    I can post the whole doc if requires - it's only about 300 lines.

    Cheers,

    Rohan

  2. #2
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    You probably don't need to post the entire script, but I am also not entirely clear about what normally would and would not be in your code so I may mention some things that won't apply.

    First and foremost, it's good to see you're using strict, but you should enable warnings and taint checks as well.

    #!/usr/bin/perl -wT

    <snip!>
    if ($in{sendsms}) {
    $number = "0421444444";
    $content = "Hello.";
    &send_sms;
    print p("SMS was sent");
    <snip!>
    I'm assuming that in your program the <snip!>s aren't there and the if the block has a closing brace.
    I don't see $in{sendsms} being set to anything. Is this done elsewhere? Otherwise it should be param('sendsms').
    A number should not have quotes around it (making it a string).
    Double quotes interpolate, single do not. Don't make perl do any more work than it has to.
    Try to keep your routines more self contained (portable). Pass $content and $number as arguments instead of setting them with a scope viewable to the routine.

    if (param('sendsms')) {
    &nbsp; send_sms(421444444, 'Hello') or die 'Could not send SMS'; # more on the second part of this when we get to the subroutine
    &nbsp; print '<p>SMS was send</p>'; # I always preferred writing the HTML out myself
    }


    sub send_sms {
    my $gsm = Device::Gsm->new ( port => '/dev/ttyS0', pin => '0000');
    $gsm->connect ( baudrate => 57600); #sounds high but is rqd for my mobile to work
    my $ok = $gsm->send_sms(content=>$content, recipient=>$number, class=>'normal');
    return ;
    }
    $gsm->connect() and $gsm->send_sms() both (probably anyway) have return values indicating success or failure. You should always use that when it's there, and use that information to decide whether your routine should return true for success or false for failure. If your routine returns false that's when the or die() gets executed, generating an error message and stopping the program.

    sub send_sms
    {
    &nbsp; my $number &nbsp;= shift @_;
    &nbsp; my $content = shift @_;
    &nbsp; return undef unless defined $number and defined $content;

    &nbsp; my $gsm = Device::Gsm->new(port => '/dev/ttyS0', pin => '0000');
    &nbsp; $gsm->connect(baudrate => 57600) or return 0;
    &nbsp; $gsm->send_sms(content => $content, recipient => $number, class => 'normal') or return 0;
    &nbsp; return 1; # everything went through ok, return true
    }


    Hmm, it seems most of this is stylistic issues. And anything that might actually cause an error is probably the result of snips. Is the script not compiling? Is there anything in your error log? If it compiles but doesn't send the message then implementing the return values indicating success or failure might help locate the problem.

    port => '/dev/ttyS0'
    Also just want to make sure that this is a Unix server you're running this on.
    Last edited by jeffmott; 01-03-2003 at 03:21 AM.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Jan 2003
    Location
    Sydney, Australia
    Posts
    4
    Thanks for your reply!

    I will make the changes you have suggested in the code.

    $in{sendsms} has a vlaue - it is set by a routine that captures the results of the POST operation on the web form.

    Great tip about the double quotes - I will update the rest of the script where neccessary!

    I am not sure what you mean about the script not compiling... I usually test the syntax of the script with

    %perl -wc script.pl

    However the script is called as the action result of a html form (using POST). I assumed this way of executing the script was different from running it at the command line with

    %perl script.pl

    Which was why I had one script working (the one running from CLI) and one not (the script called by the html form).

    The server is running Linux RedHat 7.3, Apache and Perl 5.6.1.

    I will make the suggested changes on Monday and try again!

    Thanks!

  4. #4
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    rjbourke
    $in{sendsms} has a vlaue - it is set by a routine that captures the results of the POST operation on the web form
    You've already imported CGI.pm. I hope you're using this to parse your incoming form data instead of wasting processor time loading the module.

    rjbourke
    I am not sure what you mean about the script not compiling
    If the script doesn't compile you'll get an error message and at the end Execution of script.pl aborted due to compilation errors. If it does compile then it will execute the instructions, but you're just not getting the results you were looking for.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  5. #5
    Join Date
    Jan 2003
    Location
    Sydney, Australia
    Posts
    4
    I'm afraid I am wasting processor time - I have a routine in my code that assigns the form elements to an associative array. I am guessing from your post that the ever helpful CGI.pm can do this for me??

  6. #6
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    It is, in fact, what it is most commonly used for.

    use CGI;
    my $cgi = CGI->new();

    # you can call on parameters individually
    my $value = $cgi->param('name');

    # or imort them all into a hash (associative array)
    my %params = $cgi->Vars();
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  7. #7
    Join Date
    Jan 2003
    Location
    Sydney, Australia
    Posts
    4
    Thanks jeffmott - your suggestions have made my code so much cleaner and quicker.

    I am still however having the same problem - I have removed all of the offending code and put it in it's own script. When I call it from the browser it runs ok but fails to send an SMS. When I run the same script from the command line with
    %perl test_sms.pl
    it runs ok and successfully sends the SMS.

    I can only assume it has something to do with the way Apache calls the script or something...

    I have attached the script in question...

    Any ideas???
    Attached Files Attached Files

  8. #8
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    Although I have no means of testing the script right now, I can offer up a theory.
    my $gsm = new Device::Gsm( port => '/dev/ttyS0');
    The CGI, Device::Gsm, and perhaps other modules will import their own new() function into your namespace. So when you call new in the procedural style it is unclear which new() is being executed from which module. Try changing the above line to define specifically that it should look for that particular new() in Device::Gsm

    my $gsm = Device::Gsm->new(port => '/dev/ttyS0');

    Let me know if it makes a difference. If it doesn't post your entire script and I'll download the required modules and see what I can do with it.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles