Reading from a serial port

Discussion in 'Perl Misc' started by michal.golunski@gmail.com, Mar 10, 2007.

  1. Guest

    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?
     
    , Mar 10, 2007
    #1
    1. Advertising

  2. 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
     
    Ekki Plicht (DF4OR), Mar 10, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Willie

    serial port reading

    Willie, Sep 7, 2005, in forum: Java
    Replies:
    4
    Views:
    13,549
    Willie
    Sep 8, 2005
  2. lec
    Replies:
    1
    Views:
    403
    Grant Edwards
    Jul 17, 2003
  3. Pom
    Replies:
    2
    Views:
    1,680
    Bas-i
    Jan 31, 2007
  4. Vivek Menon
    Replies:
    8
    Views:
    3,190
    bogdan
    Dec 6, 2006
  5. msalerno
    Replies:
    3
    Views:
    414
    Ilmari Karonen
    Jul 14, 2005
Loading...

Share This Page