in reply to Re: RS232 and Tk with threads
in thread RS232 and Tk with threads
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.
Thanks in advance for your 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 ; } } }
Marjacktablet
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: RS232 and Tk with threads
by Marshall (Canon) on Jan 05, 2022 at 00:38 UTC | |
by afoken (Chancellor) on Jan 05, 2022 at 14:53 UTC | |
by Marshall (Canon) on Jan 05, 2022 at 23:15 UTC | |
by MarSkv267 (Acolyte) on Jan 07, 2022 at 13:54 UTC | |
by Marshall (Canon) on Jan 07, 2022 at 14:55 UTC |