"Diogo Nunes de Oliveira" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hi you all...
>
> Do you know how I can get the code that is passed by the serial port? I
> mean, I wanna create a script that "snifs" the serial port, gets the
> string, and write it to a file to then, convert it into plain english
> (portuguese, really, hehehe)... What should I do? Does Win32::SerialPort
> module work for such thing? Thx!
>
>
> -- 
> Diogo N Oliveira - Instrutor EAD - Senai CEDESG
>

Ive used the class below with sucess. I adapted it from the
Win32::SerialPort docs. You can use it like this:

  my $interface = Generic::PortInterface->new();
  while ( my $data = $interface->read ) {
    # ...
  }

package Generic::PortInterface;
use Win32::SerialPort;

our $WAIT = 45; # 210 == 3.5 min
our $PORT = 'COM1';

sub new {
  my $class = shift;
  my $self = bless( { @_ }, $class );
  $self->init;
  return $self;
}

sub init {
  my $self = shift;
  $self->{port} ||= $PORT;
  $self->{wait} ||= $WAIT;
  $self->setPortInterface;
}

sub setPortInterface {
  my $self = shift;
  my $port = Win32::SerialPort->new($self->{port}) or die("$self->{port}:
$!");
  $port->user_msg(1);    # misc. warnings
  $port->error_msg(1);      # hardware and data errors
  $port->baudrate(115200);
  $port->parity('none');
#  $ob->parity_enable(1); # for any parity except "none"
  $port->databits(8);
  $port->stopbits(1);
  $port->handshake('rts');
  $self->{port} = $port;
}

sub read {
  my $self = shift;
  my $port = $self->{port};
  my $timeout = Win32::GetTickCount() + (1000 * $self->{wait});
  $port->lookclear; # clear buffers
  my $gotit = "";

  for (;;) {
    return unless (defined ($gotit = $port->lookfor));
    if ($gotit ne "") {
      my ($found, $end) = $port->lastlook;
      return $gotit.$found;
    }
    return if ($port->reset_error);
    return if (Win32::GetTickCount() > $timeout);
  }
}

Todd W.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Reply via email to