我在Perl线程中使用串口,我可以在线程中读写。但是,如果我想关闭串行端口,以便另一个应用程序可以使用该端口,并在以后再次打开它,我将无法再进行读写。如何做到这一点?
my $dev;
my $port = "/dev/ttyACM0";
my $run :shared = 0;
my $thr;
my $read_thr;
sub port_init
{
$dev = Device::SerialPort->new($port, 1) || die "Cannot open $port: $!\n";
$dev->baudrate(115200);
...
$run = 1;
}
sub read_port # read async
{
my $str;
while ($run == 1)
{
$str = $dev->lookfor;
if ($str ne "")
{
print "recv: $str\n";
}
sleep 0.5;
}
}
sub write_port
{
my $msg = shift;
if ($run == 1)
{
$dev->write($msg."\r");
}
}
sub close_port
{
$run = 0;
$dev->close;
}
# Main
port_init();
$read_thr = threads->new(\&read_port);
$read_thr->detach();
if ("event1 occurs") # send cmd to port
{
$thr = threads->new(\&write_port, "ATI"); # works, response received
$thr->detach();
}
if ("event2 occurs") # another application is requesting the port
{
$thr = threads->new(\&close_port);
$thr->detach();
# wait till application has finished
port_init();
$read_thr = threads->new(\&read_port);
$read_thr->detach();
# send cmd to port, doesn't work
$thr = threads->new(\&write_port, "ATI");
$thr->detach();
}关闭端口后,我不能再使用它。第二次启动后,read_port在Device::SerialPort::input中抛出错误#9。我需要线程,因为解析器必须始终是可访问的。
发布于 2016-07-19 17:08:45
好吧,如果你不发布你的实际代码,就很难说了。
然而,我认为问题将是因为您在线程启动之前创建了$dev -并使用port_init()实例化它-但随后每个线程在它们启动时都继承了这个对象的克隆。
这会让你处于一种非常混乱的状态--我强烈建议你从一开始就不要进入这种状态。因为每个线程仍在运行-它是活动的,但已分离-所以将‘持有’它自己的端口文件句柄实例。
这非常混乱,几乎可以肯定这就是为什么它不能正常工作的原因。
我建议你不要一开始就遇到这种情况--不要像那样共享一个端口,只需要一个线程来处理整个事情。如果您需要比共享变量更复杂的东西来进行进程间通信,请使用Thread::Queue。
https://stackoverflow.com/questions/38452256
复制相似问题