How can I read data from a serial port under Perl + Linux? I tried to
use Device::SerialPort but constructor isn't working for me, and when
i tried to run it as a root there was a getattr error.
Device::Serialport works nicely and is probably the easiest method to use
the serial port with Perl (and portable to Win32). I would go and find out
the problem with it instead of looking for other, probably more difficult
and less portable solutions.
Here on my box, the serial ports have the owner root:tty, so maybe your user
is not a member of the tty group and therefore cannot access /dev/ttyS* ?
Here is how I use Device::Serialport:
sub init_serial(@) {
# in: devicename, baudrate
# out: nothing
# opens serial device if possible
my ($dev, $baud) = @_;
my @items = split "/", $dev;
my $lockdevice = splice (@items,-1);
defined($lockdevice) || die 'failed extracting serial device\n';
$lockdevice = '/var/lock/LCK..' . $lockdevice;
# Change: Under Gentoo creating the lockfile suddenly takes 2 seconds
# (sleep defined in Device::Serialport)
# Under SuSE this sleep, implemented as nanosleep in Device::Serialport,
# did not happen like this.
# So I don't use device locking currently...
#$ser = Device::SerialPort->new ($dev, 0, $lockdevice)
# || die "Can\'t lock and open $dev: $!";
$ser = Device::SerialPort->new ($dev, 0, '')
or die "Can\'t open $dev: $!";
$ser->baudrate($baud) || die 'fail setting baudrate, try -b option';
$ser->parity("none") || die 'fail setting parity to none';
$ser->databits(8) || die 'fail setting databits to 8';
$ser->stopbits(1) || die 'fail setting stopbits to 1';
$ser->handshake("none") || die 'fail setting handshake to none';
$ser->datatype('raw') || die 'fail setting datatype raw';
$ser->write_settings || die 'could not write settings';
$ser->error_msg(1); # use built-in error messages
$ser->user_msg(1);
# important for nice behaviour, otherwise hogs cpu
$ser->read_const_time(100);
$ser->read_char_time(100); # dto.
};
So are there any other methods to connect to serial port, set
databits, parity and so on and transfer data?
open(), close(), select(), ioctl() etc.
and maybe with setserial in the shell.
Regards,
Ekki