Garayed.com  

Go Back   Garayed.com > Perl
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-10-2007, 12:01 PM
michal.golunski@gmail.com
 
Posts: n/a
Default Reading from a serial port

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?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-10-2007, 02:49 PM
Ekki Plicht (DF4OR)
 
Posts: n/a
Default Re: Reading from a serial port

michal.golunski@gmail.com 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(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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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



All times are GMT. The time now is 02:55 AM.




LinkBacks Enabled by vBSEO 3.0.0 © 2007, Crawlability, Inc.