Reputation: 1560
I am trying to communicate with a embedded system using a communication port COM0 which is ttyS0 in linux. I tried another software on Windows and it seems to be able to communicate properly with the port. I tried using this code, but right at the very first line I get an error.
use strict;
use warnings;
use Device::SerialPort;
die "Cannot Open Serial Port\n" unless my $PortObj = new Device::SerialPort ("/dev/ttyS0");
Also is there another easier way to communicate with the serial port.
Upvotes: 1
Views: 1075
Reputation: 64929
It looks like you need code that looks like this:
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"
);
Note, I do not know if com1
is the right serial port for your code, but I think you need something like that. If you have many more platforms you need to deal with a hash may be a better option:
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});
Upvotes: 1