use Tk;
my $mw = MainWindow -> new;
$mw -> title("Test Code With Serial Port and GUI");
my $btn_01=$mw->Button(-text =>"Done",
-command=>sub{exit})
->pack;
my $btn_02=$mw->Button(-text=>"Serial Port",
-command=>sub{$thr=threads->new(\&serial_port)})
->pack;
MainLoop;
####
use threads;
use threads::shared;
my $thr; # thread
my $Rval:shared;# return value inside thread
my $RTval; # return value intended to be "out in Tk code"
# the following 3 lines of code would be invoked by a button press
$thr=threads->new(\&sub_name);
$TRval = $thr->join;
print "$TRval"; # $TRval is in MainWindow Tk code
sub sub_name
{
$Rval = `a_command_line_function`; # final code will have serial port activity
}
####
use Device::SerialPort;
my $port=Device::SerialPort->new("/dev/ttyS0");
$port->baudrate(9600);
$port->parity("none");
$port->handshake("none");
$port->databits(8);
$port->stopbits(1);
$port->read_char_time(0);
$port->read_const_time(1);
my $readChars=0; # init
my $readBytes=""; # init
while(1) # yes, I know this thread is blocking
{
($readChars, $readBytes)=$port->read(1);
if ($readChars ne 0)
{
print("$readBytes"); # $Rval = $readBytes when attempting to share from thread to main window
$readBytes=""; # re-init
}
}