Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Reading from a serial port

Reply
Thread Tools

Reading from a serial port

 
 
michal.golunski@gmail.com
Guest
Posts: n/a
 
      03-10-2007
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.

So are there any other methods to connect to serial port, set
databits, parity and so on and transfer data?

 
Reply With Quote
 
 
 
 
Ekki Plicht (DF4OR)
Guest
Posts: n/a
 
      03-10-2007
wrote:

> 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( || 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
emulate a serial port in windows (create a virtual 'com' port) Pom Python 2 01-31-2007 07:49 PM
Serial Port programming - Reading DSR from port msalerno Perl Misc 3 07-14-2005 12:58 PM
Can I connect router Serial interface directly to a PC serial port? Faustino Dina Cisco 2 08-18-2004 02:30 AM
Re: Serial port and PS/2 port schematics OR Assistive Tech. suggestion naive.verizon@locality.net Computer Support 1 07-10-2003 11:46 AM
Re: Serial port and PS/2 port schematics °Mike° Computer Support 1 07-09-2003 10:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57