views:

725

answers:

1

I would like to use the Win32::SerialPort module under Windows XP to read a text string from a COM port.

There is a scale attached to the serial port which continuously sends the current measured weight. Since I'm also using Win32::GUI I need a way to read that non-blocking. What's the best way to do this? Should I use Lookfor or streamline? I'm a little confused by the documentation.


Original question text: Ich möchte mit dem Modul Win32::SerialPort unter Windows Xp von einem COM-Port ein bestimmten Textstring nur einlesen. An dem SerialPort ist eine Waage angeschlossen, die permanent das aktuell gemessene Gewicht ausgibt. Da ich auch Win32::GUI verwende darf/sollte das einlesen nicht blockierend sein. Wie stelle ich das am geschicktesten an? Was sollte ich verwenden Lookfor oder streamline? Ich blicke bei dem Manual nicht so richtig durch.

+6  A: 

Since the device is continuously sending information through the serial port, you probably want to set up a timer and take a look at what's coming down the connection without blocking the main thread.

First, I am going to point you to Win32::GUI::Tutorial::Part4 where timers are discussed.

Run the following example using perl.exe, not wperl.exe because the output goes to the console:

#!/usr/bin/perl

package My::GUI;

use strict; use warnings;

use Win32::GUI();

sub new { bless {} => shift }

sub initialize { # very quick and dirty example
    my $self = shift;

    $self->{window} = Win32::GUI::Window->new(
        -name => 'Main',
        -title => 'Test',
        -onTerminate => sub { -1 },
        -onTimer => sub { $self->onTimer(@_) },
    );

    $self->{timer} = $self->{window}->AddTimer(Timer => 0);
    return $self;
}

sub run {
    my $self = shift;

    my $window = $self->{window};
    $window->Show;
    $window->SetRedraw(1);
    $self->{timer}->Interval(1000);
    Win32::GUI::Dialog();
}

# poll serial port here, don't block
sub onTimer { warn time - $^T, "\n"; return; }

package main;

use strict; use warnings;

My::GUI->new->initialize->run;

Output:

C:\Temp> gui
1
2
3
4
5
6
Terminating on signal SIGINT(2)

Now, regarding the choice between Win32::SerialPort versus Win32::CommPort and which methods depends on the specs of the scale on the other end of the connection.

Sinan Ünür
+1 for all the effort. Great job!
0xA3