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

Hi,

On a FC3 box, log script launched with command
"perl - w -I $ScriptDir $ScriptName $Args < $Port > $Port",
and serial port initialize with "stty time 20 min 0 -icanon".
It worked fine at the beginning, but after a few of minutes, it couldn't read data througth serial port any more, just logged a few KB data. I can make sure that GPS outputs data to serial port every 30 seconds continuously.

Any ideas?

Here are the main parts of data logging script
sub LogData { $SessNum =0; $BuffLen = 1024; $TempFileName = ".temp.log"; $SiteName = ReadVar($ConfigFile,"SiteName","____"); $SessDuration = ReadVar($ConfigFile,"SessDuration",60); # Minutes $SessDuration *= 60*1000; # ms print STDERR "Open log file $TempFileName\n"; open(LOGFILE,">$TempFileName") or die "Can not open log file $Temp +FileName"; $SIG{INT} = \&catch_zap; # Install signall handler print "\$PASHQ,SNV\n\r"; # Query ephemeris undef $EndTime; LoggingData: while (not $Killed) { $ReadLen = 0; $timeout = 60; eval { local $SIG{ALRM} = sub { die "alarm\n" }; # NB \n require +d alarm $timeout; $ReadLen = read(STDIN,$Buff,$BuffLen); alarm 0; }; if ( $ReadLen > 0 ) { ($Time,$LastPbnPos) = CheckTime($Buff,$EndTime); $SetEndTime = 0; $SetEndTime = 1 if ( not defined($EndTime) and defined($Time) +); $SetEndTime = 2 if ( defined $LastPbnPos) ; $EndTime = $Time + ($SessDuration - $Time % $SessDuration ) if + ( $SetEndTime ); if ( $SetEndTime == 2 ) { # Write part of the buffer containing the PBN # with the given yime tag to the file and then close it $SplitPoint = $LastPbnPos + 58; $SplitPoint = $ReadLen if $SplitPoint > $ReadLen; $Head = substr($Buff,0,$SplitPoint); $Tail = substr($Buff,$SplitPoint); syswrite(LOGFILE,$Head,$SplitPoint); print STDERR "Close log file $TempFileName\n"; close(LOGFILE) or die "Can not close log file $FileName"; # Give the valid name to the temporary file $FileName = ComposeLogFileName($SiteName,$SessNum); print STDERR "Rename $TempFileName to $FileName\n"; rename ($TempFileName,$FileName) or die "Can not rename fil +es\n"; # Open the new file, write remainder of the buffer to it print STDERR "Open log file $TempFileName\n"; open(LOGFILE,">$TempFileName") or die "Can not open file $T +empFileName"; syswrite(LOGFILE,$Tail,$ReadLen - $SplitPoint); # Make sure we have ephemeris in the new file print "\$PASHQ,SNV\n\r"; ++$SessNum; } else { syswrite(LOGFILE,$Buff,$ReadLen); } } } if ( $Killed ) { print STDERR "Log processed killed, clean up...\n"; print STDERR "Close log file $TempFileName\n"; close(LOGFILE) or die "Can not close log file $FileName"; # Give the valid name to the temporary file $FileName = ComposeLogFileName("____",$SessNum); print STDERR "Rename $TempFileName to $FileName\n"; rename ($TempFileName,$FileName) or die "Can not rename files\n"; print STDERR "Delete ID file [$LogIdFile]\n"; unlink $LogIdFile; } } sub catch_zap { $Killed = 1; }

Edit: g0n - added readmore tags

Replies are listed 'Best First'.
Re: Problem with logging raw data through serial port
by jhourcle (Prior) on Jan 13, 2006 at 16:46 UTC

    I've never done serial communication in Perl, but back when I had to deal with console connections, everyone seemed to get stung sooner or later by flow control, which could explain the symptoms you're having -- particularly as you mentioned 'raw' data, and you don't look to be checking for XON / XOFF.

    If you're using hardware flow control, nevermind.

Re: Problem with logging raw data through serial port
by McDarren (Abbot) on Jan 13, 2006 at 07:59 UTC
      Thank you very much for comments.

      After reading the article, I changed read(STDIN,$Buff,$BuffLen) to sysread(STDIN,$Buff,$BuffLen), it logged data without problem.

      But I'm still a little bit of puzzling.

      According the article, there should be three buffers. I do the buffering myself with $Buff, the log program has its own buffer, /dev/ttyS0 is also buffered. There is no buffer for output, because I used syswrite operator.

      GPS outputs 382 Bytes to serial port every 30 seconds. If the system's default buffer is 8K, it will take 10.7 minutes to have a full buffer. But the real situation is that read(STDIN,$Buff,$BuffLen) returned zero byte after a few minutes. I tested it for several hours. It only logged 1.2K data at the beginning for a few minutes. At the rest time, read(STDIN,$Buff,$BuffLen) can't get any data through /dev/ttyS0. Where's the buffering?

      Thanks