Multithreading perl线程退出时串行端口处理程序错误

Multithreading perl线程退出时串行端口处理程序错误,multithreading,perl,asynchronous,serial-port,Multithreading,Perl,Asynchronous,Serial Port,我使用perl(5.12 ActiveState)中的线程来允许在Windows上的两个不同COM端口上进行并行和异步写入。我的代码是这样的: #!/usr/bin/perl use warnings; use strict; use Win32::SerialPort; use threads; my $ComPortObj = new Win32::SerialPort ("COM10") or die ("This is the bitter end..."); [... omit p

我使用perl(5.12 ActiveState)中的线程来允许在Windows上的两个不同COM端口上进行并行和异步写入。我的代码是这样的:

#!/usr/bin/perl

use warnings;
use strict;
use Win32::SerialPort;
use threads;

my $ComPortObj = new Win32::SerialPort ("COM10") or die ("This is the bitter end...");
[... omit port settings ...]

my $ComPortObj2 = new Win32::SerialPort ("COM6") or die ("This is the bitter end...");
[... omit port settings ...]    

my $s_read = "";

my $HangupThr = async 
{
    # printf("THREAD - Wait 3 seconds\n");
    # sleep(3);
    print("THREAD - write on COM10: AT\n");
    $ComPortObj->write("AT\r") || die ("Unable to send command\n");
    printf("THREAD - Wait 1 second\n");
    sleep(1);
    $s_read = $ComPortObj2->input;
    # $s_read =~ s/\n/N/g;
    # $s_read =~ s/\r/R/g;
    print("THREAD - read from COM6: $s_read\n");
    return 1;

};
$HangupThr->detach();

# printf("MAIN - Wait 4 seconds\n");
# sleep(4);
print("MAIN - write on COM6: AT\n");
$ComPortObj2->write("AT\r") || die ("Unable to send command\n");
printf("MAIN - Wait 1 second\n");
sleep(1);
$s_read = $ComPortObj->input;
# $s_read =~ s/\n/N/g;
# $s_read =~ s/\r/R/g;
print("MAIN - read from COM10: $s_read\n");

$ComPortObj->close();
$ComPortObj2->close();
当程序退出时,我得到的是一个错误。完整输出:

MAIN - write on COM6: AT
THREAD - write on COM10: AT
MAIN - Wait 1 second
THREAD - Wait 1 second
MAIN - read from COM10: AT
OK

THREAD - read from COM6: AT
OK

Error in PurgeComm at C:\userdata\Perl scripts\src\handler_error.pl line 0 thread 1
The operation completed successfully.
Error in GetCommTimeouts at C:\userdata\Perl scripts\src\handler_error.pl line 0 thread 1
Error Closing handle 184 for \\.\COM6
The handle is invalid.
Error closing Read Event handle 188 for \\.\COM6
The handle is invalid.
Error closing Write Event handle 192 for \\.\COM6
The handle is invalid.
Error in PurgeComm at C:\userdata\Perl scripts\src\handler_error.pl line 0 thread 1
The handle is invalid.
Error in GetCommTimeouts at C:\userdata\Perl scripts\src\handler_error.pl line 0 thread 1
Error Closing handle 144 for \\.\COM10
The handle is invalid.
Error closing Read Event handle 148 for \\.\COM10
The handle is invalid.
Error closing Write Event handle 180 for \\.\COM10
The handle is invalid.
这与串口处理程序purge有关,我不知道perl如何在线程中复制。我尝试了各种接近线程的尝试,主要。。。没有成功。此外,我必须在主程序和线程中使用相同的端口。有没有防止这些错误的建议


非常感谢

您正在处理串行端口,在windows中,任何时候只有一个进程可以控制串行端口(某些终端交换机提供多个登录,但这不是您的情况),当一个进程连接到COM时,它会自动断开其他进程的连接。您可以尝试从windows计算机两次登录到同一COM端口,另一个端口应断开连接,这将导致无效句柄,您将其视为错误

其他你可以尝试的事情

  • 在线程内部创建Com对象,使用它并销毁对象,然后在其他线程上访问它

  • 我做了一些实验,避免错误的唯一方法是在启动线程之前关闭主程序中的所有com对象。事实上,该错误与线程启动时生成的复制com对象有关。请查看下面对@pilcrow的评论以了解更多详细信息。是的,一旦我没有重复,我就必须正确地打开和关闭线程内的com对象。