1
我有用於433 mhz信號的RFXCOM收發器。我設法組裝了一個可以毫無問題地傳輸信號的程序(例如打開燈泡)。不過,我也希望能夠接收來自遙控器的信號。有點谷歌搜索給了我這個工作代碼;Newbie perl串行編程
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";
}
有一件事我不明白 - 這個腳本在10秒後給我輸出,但我想立即看到收到的數據。任何想法我需要改變?我不認爲這與超時部分有關,因爲這似乎是測量自上次收到信號以來的時間。有任何想法嗎?
寫得好多了STDOUT-> autoflush(1);以免使用那些難以閱讀的標點變量。 – LeoNerd 2012-07-08 14:16:39