使用Device::SerialPorts在Linux中编程串行端口
我正在尝试使用通信端口COM0与嵌入式系统通信,该端口在linux中是ttyS0。我在Windows上尝试了另一个软件,它似乎能够与端口正确通信。我试着使用这段代码,但就在第一行我得到了一个错误使用Device::SerialPorts在Linux中编程串行端口,linux,perl,serial-port,Linux,Perl,Serial Port,我正在尝试使用通信端口COM0与嵌入式系统通信,该端口在linux中是ttyS0。我在Windows上尝试了另一个软件,它似乎能够与端口正确通信。我试着使用这段代码,但就在第一行我得到了一个错误 use strict; use warnings; use Device::SerialPort; die "Cannot Open Serial Port\n" unless my $PortObj = new Device::SerialPort ("/dev/ttyS0"); 还有另一种更简
use strict;
use warnings;
use Device::SerialPort;
die "Cannot Open Serial Port\n" unless my $PortObj = new Device::SerialPort ("/dev/ttyS0");
还有另一种更简单的方式与串行端口通信。看起来您需要这样的代码:
use strict;
use warnings;
use Device::SerialPort;
die "Cannot Open Serial Port\n"
unless my $PortObj = Device::SerialPort->new(
$^O eq "MSWin32" ? "com1" : "/dev/ttyS0"
);
注意,我不知道com1
是否是您代码的正确串行端口,但我认为您需要类似的东西。如果您有更多的平台,您需要处理哈希可能是一个更好的选择:
use strict;
use warnings;
use Device::SerialPort;
my %port_name = (
MSWin32 => "com1",
linux => "/dev/ttyS0",
);
die "I don't know what serial port to use on $^O\n"
unless exists $port_name{$^O};
die "Cannot Open Serial Port\n"
unless my $PortObj = Device::SerialPort->new($port_name{$^O});
看起来您需要这样的代码:
use strict;
use warnings;
use Device::SerialPort;
die "Cannot Open Serial Port\n"
unless my $PortObj = Device::SerialPort->new(
$^O eq "MSWin32" ? "com1" : "/dev/ttyS0"
);
注意,我不知道com1
是否是您代码的正确串行端口,但我认为您需要类似的东西。如果您有更多的平台,您需要处理哈希可能是一个更好的选择:
use strict;
use warnings;
use Device::SerialPort;
my %port_name = (
MSWin32 => "com1",
linux => "/dev/ttyS0",
);
die "I don't know what serial port to use on $^O\n"
unless exists $port_name{$^O};
die "Cannot Open Serial Port\n"
unless my $PortObj = Device::SerialPort->new($port_name{$^O});
哦,好吧,那就行了。主要的问题是另一个应用程序占用了串行端口,因此无法连接。哦,好的,这很有效。主要问题是另一个应用程序占用串行端口,因此无法连接。