Alex van Es
Alex van Es

Reputation: 1181

Newbie perl serial programming

I have a RFXCOM transceiver for 433 mhz signals. I managed to put together a program that can transmit signals without a problem (and for example turn on a lamp). However I also want to be able to receive signals from my remote control. A bit of googling gave me this working code;

use Device::SerialPort;
 my $PortObj=Device::SerialPort->new("/dev/ttyUSB1");

  $PortObj->user_msg(ON);
  $PortObj->databits(8);
  $PortObj->baudrate(38400);
  $PortObj->parity("none");
  $PortObj->stopbits(1);
  $PortObj->handshake("rts");


 my $STALL_DEFAULT=10; # how many seconds to wait for new input

 my $timeout=$STALL_DEFAULT;

 $PortObj->read_char_time(0);     # don't wait for each character
 $PortObj->read_const_time(1000); # 1 second per unfulfilled "read" call

 my $chars=0;
 my $buffer="";
 while ($timeout>0) {
        my ($count,$saw)=$PortObj->read(1); # will read _up to_ 255 chars
        if ($count > 0) {
                $chars+=$count;
                $buffer.=$saw;
print $saw; 
                # Check here to see if what we want is in the $buffer
                # say "last" if we find it
        }
        else {
                $timeout--;
        }
 }

 if ($timeout==0) {
        die "Waited $STALL_DEFAULT seconds and never saw what I wanted\n";
 }

One thing I can't figure out - this script gives me the output after about 10 seconds, but I want to see the received data instantly. Any idea what I need to change? I don't think it has to do with the timeout part since that just seems to measure the time since the last received signal. Any ideas?

Upvotes: 2

Views: 3113

Answers (1)

mob
mob

Reputation: 118615

Suffering from buffering? Set

$| = 1;

at the top of your script.

Upvotes: 2

Related Questions