You can ask Tk to run a specified routine periodically: $widget->repeat(milliseconds, callback);, like perhaps: $widget->repeat(500, \&update_serial_info); This will cause Tk to call update_serial_info() every 1/2 second - the Tk code continues executing immediately after this "request".

Following your advice I wrote a simple module which uses POSIX and sends time stamps to the GUI window:

my @localtime=localtime; my $hour=strftime ("%d.%m.%Y  %H:%M:%S", @localtime); our $receive_T->insert('end',$hour."\n");

I define a text widget in another module where it calls the above module and writes time stamps into the text widget:

$receive_T = $receive_Tk->ROText(-relief=>'groove',-width=>72,-height= +>20,-font=>'Courier 8 bold')->pack(); $receive_T->repeat(5000,\&Receive2);

In the update_serial_info routine, you should use non-blocking calls to  the serial port S/W. If the Tk display is not ready to be updated, then  don't do anything. If the data is "ready" perhaps you just put the data  into some $text_variable that the GUI knows about. If the info received  so far warrants a display update like a text window pane, then do that  being aware that updating the GUI is very "expensive" compute wise. You  probably don't want to do that for every single individual character  received! This Serial Port code may run just fine without being in a  separate thread if you call it every 200ms or so (maybe less, maybe  more).

A key is to call the non-blocking serial port methods. If there is  nothing to be read, then just don't do anything. That will happen most  of the time, so make that path fast.

I took one of rare examples for serial communication  I could find and which actually worked without GUI.

Since there are no more laptops with RS232 port I use a USB adapter and one of my modules uses mode from DOS which detects COM port and sets parameters, then opens the COM port.

I admit I'm not capable to implement the non-blocking calls  in the below lines - maybe it's because of the fact I'm not a programmer or the Win32::SerialPort documetation is too short - so I'm asking for some additional help.

#!/usr/bin/perl use strict; use Pod::Usage; use Getopt::Std; our $RECEIVED; our $PORT; my $x; our $receive_T; our $ob; my $timeout; sub Receive{             while(1){         $RECEIVED = ReadPort($PORT, 5);         if($RECEIVED ne ""){             print "\t$x\tRX>\t$RECEIVED\n";             $receive_T->insert('end',"\t$x\tRX>".$RECEIVED."\n");             $x++;         }     }  } sub ReadPort{         # Store timeout time in millisecond tic    $timeout = $ob->get_tic +k_count + (1000 * $timeout);     $ob->lookclear;  # Clear lookfor buffers         while(1) {             # polls until we have a line of data         # return unless ( defined ($gotit = $ob->streamline) );         if ($RECEIVED ne "") {             my ($found, $end, $obattern, $instead) = $ob->lastlook;                         return "$RECEIVED";         }         # or an error         return if ($ob->reset_error);         if ($ob->get_tick_count > $timeout) {             my ($match, $after, $obattern, $instead) = $ob->lastlook;             return ;         }     } }

Thanks in advance for your help.

Marjacktablet


In reply to Re^2: RS232 and Tk with threads by Anonymous Monk
in thread RS232 and Tk with threads by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.