Results 1 to 8 of 8

Thread: Device::Gsm Perl mod probs

  1. #1
    Join Date
    Jan 2003
    Sydney, Australia

    Unhappy Device::Gsm Perl mod probs


    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:

    #! /usr/bin/perl
    use strict;
    use Device::Gsm;
    use CGI qw(:standard);
    use MIME::Lite;  #Script also sends emails
    my ($number, $content);
    if ($in{sendsms}) {
         $number = "0421444444";
         $content = "Hello.";
         print p("SMS was sent");
    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.



  2. #2
    Join Date
    Nov 2002
    NY, USA
    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

    if ($in{sendsms}) {
    $number = "0421444444";
    $content = "Hello.";
    print p("SMS was sent");
    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 04:21 AM.

  3. #3
    Join Date
    Jan 2003
    Sydney, Australia
    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!


  4. #4
    Join Date
    Nov 2002
    NY, USA
    $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.

    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.

  5. #5
    Join Date
    Jan 2003
    Sydney, Australia
    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
    NY, USA
    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();

  7. #7
    Join Date
    Jan 2003
    Sydney, Australia
    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???

  8. #8
    Join Date
    Nov 2002
    NY, USA
    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.

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11868 seconds
  • Memory Usage 2,917KB
  • Queries Executed 13 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (6)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates