Perl 스레드에서 직렬 포트를 사용하고 있습니다. 스레드에서 읽고 쓸 수 있습니다. 하지만 다른 응용 프로그램에서 포트를 사용하고 나중에 다시 열 수 있도록 직렬 포트를 닫고 싶다면 더 이상 읽고 쓸 수 없습니다. 그렇게하는 방법?Linux Perl 스레드 SerialPort 닫고 다시 엽니 다.
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를 두 번째로 시작한 후 throw합니다. 파서가 항상 액세스 할 수 있어야하므로 스레드가 필요합니다.