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

Please help me, I'm completely stuck.

Just trying to access smart device behind /dev/ttyS1, step by step, using How to read the RS-232 line and Device::SerialPort.

Look here:
use strict; use warnings; #use Device::SerialPort; open L, ">>logfile" or die "cant log"; print L "\nstarting up.\n====\n\n";
When I uncomment 'use Device::SerialPort' line, the file 'logfile' is created in current directory but it contains nothing. It is empty, zero-sized. What does it mean?

I am using (v5.8.9 built for i386-linux-thread-multi)

UPDATE. Forgot to mention that when I start playing around, it all works as expected, and only after some amount of time (I even can't say what exactly affects) I got this piece of reproducible mmm... what do you call it? a THING.
May be there is a simplier way to read/write over /dev/ttyS1 ?

Replies are listed 'Best First'.
Re: strange behaviour with Device::SerialPort
by Anonymous Monk on Nov 25, 2010 at 17:27 UTC
    You're nowhere creating an object my $port = Device::SerialPort->new( "/dev/ttyS1" );, so I'm not sure what you expect you code to do, or how opening the logfile is meant to be related to Device::SerialPort, or how this could ever have "worked as expected"...
      No-no-no. Of course I created this object. And suddenly I realized I have no logfile. So I started to cut my script to minimal reproducible size. And so I came to this minimal script you see here in the post.
      My actual question was not about how to read from a serial port. I just wondering why its not writing to file when I use mentioned perl module.
        Ah, sorry, that wasn't clear to me from your original post.

        Maybe perl doesn't get around to flushing the file contents of L, because it prematurely quits for some reason related to Device::SerialPort (in some cleanup routine of the module?). Just a guess, though.

        I would try to explicitly close the file. Or use strace to figure out what is or isn't happening under the hood.

Re: strange behaviour with Device::SerialPort
by Anonymous Monk on Nov 26, 2010 at 09:43 UTC
    1. use the three-argument form of open
    2. use lexical filehandles, not globals (you don't know what other code is playing with global globs named L)
    3. display the system's error message (from $!)

    open(my $log_handle, '>>', $filename) or die "can't open log file \"$filename\" for appending: $!";

    As in:

    #! /usr/bin/perl use strict; use warnings; my $filename = "logfile.log"; my $string = scalar localtime; open(my $log_handle, '>>', $filename) or die "can't open log file \"$filename\" for appending: $!"; print $log_handle $string; close $log_handle; # or let $log_handle fall out of scope