#!/usr/bin/perl -w use POSIX; system("setserial", "/dev/ttyS3", "spd_vhi"); $fd = POSIX::open('/dev/ttyS3', O_RDWR | O_NOCTTY | O_NONBLOCK) or die "$! in open"; $termios = POSIX::Termios->new; $termios->getattr($fd) or die "$! in getattr"; print $termios->getispeed, " ", $termios->getospeed, " ", POSIX::B9600, " ", POSIX::B38400, "\n"; $termios->setispeed(POSIX::B38400); $termios->setospeed(POSIX::B38400); $c_iflag = $termios->getiflag; $new_iflag = ($c_iflag & ~(BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF)) | IGNBRK; $c_lflag = $termios->getlflag; $new_lflag = $c_lflag & ~(ECHO | ECHOK | ICANON | ISIG | IEXTEN); $c_oflag = $termios->getoflag; $new_oflag = $c_oflag & ~(OPOST); $c_cflag = $termios->getcflag; $new_cflag = ($c_cflag & ~(CSIZE | PARENB )) | CS8; sub cbreak() { $termios->setiflag($new_iflag); $termios->setlflag($new_lflag); $termios->setoflag($new_oflag); $termios->setcflag($new_cflag); $termios->setcc(VTIME,1); $termios->setattr($fd, &POSIX::TCSANOW); } cbreak(); $old_count = 0; for (;;) { select undef, undef, undef, 0.5; POSIX::tcflush($fd, &POSIX::TCIOFLUSH); $buff = 'E'; print ">"; POSIX::write($fd, $buff, 1); while (!defined ($ret = POSIX::read($fd,$encoder_buff, 4))) { # print "<"; }; $encoder_count = unpack("l", $encoder_buff); $speed = $encoder_count - $old_count; print "$ret $encoder_count $speed\n"; $old_count = $encoder_count }; POSIX::close($fd);