First of all, Tk and threads need special handling, which may confound a beginner. You can find alot of sample code with a google for "Perl/Tk threads". However, I will tell you that even with a thread, you will still need to run a timer to update the shared variable in the main Tk thread. So I will skip the thread approach and show a linux example which just uses a timer to repeatedly read the serial port. This example is for a Linux system, but you should be able to adapt it to Win32.
The basic idea, is to set a textvariable $input which automatically gets updated from the
repeater.
use warnings;
use strict;
use Tk;
use Device::SerialPort;
#my $serial_port = "/dev/ttyS0";
my $serial_port = new Device::SerialPort ("/dev/ttyS0") || die "can't
open COM1\n";
#Setup Perl/Tk
my $mw = MainWindow->new();
$mw->title("Scale");
#Close button
my $button = $mw->Button(
-text => 'Close',
-command => sub {$mw->destroy} # exit is better here
)->pack(
-pady => 20,
-side => 'bottom'
);
my $title_label = $mw->Label(
-text => "Weigh-Tronix"
)->pack(
-side => 'left');
my $input = '---------------';
my $display_label = $mw->Label(
-textvariable => \$input,
-width=>15, #keep the width stable at 15
)->pack(
-side => 'left');
# every 10 millisecs update from port
$mw->repeat(10, sub{
# $input = $port->input;
#or maybe better
if(defined $port->input){ $input = $port->input }
});
MainLoop;
__END__
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.