我有一个用于 433 mhz 信号的 RFXCOM 收发器。我设法编写了一个可以毫无问题地传输信号的程序(例如打开灯)。但是,我也希望能够接收来自遥控器的信号。一些谷歌搜索给了我这个工作代码;
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 秒后给了我输出,但我想立即看到接收到的数据。知道我需要改变什么吗?我认为这与超时部分无关,因为这似乎只是测量自上次接收信号以来的时间。有任何想法吗?