I am trying to write a Perl program to collect data from a serial port, parse it and post the fragments to various text boxes in a Perl/Tk window. I did quite a bit of searching and thought I had a good starting point with the PerlMonks node 416132. But, there were two problems. First, the code link for node 416132 is broken. Second, the code in the text of 416132 seems to be a little advanced for this senior-newbee / junior-intermediate programmer. Therefore, I started off on my own.

I have many small test files, a big and bloated file with no clear direction to it and since I don't yet have a clear direction I don't have a single clean file to post for comments. Thus, I have pulled and rewritten fragments to show my approach.

Pulling from my big file of test code I have the following to represent my approach with Tk:
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;
In the SAME FILE the following represents a streamlined version of my thread code (note $Rval is shared):
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 po +rt activity }
I was quite suprised at how long it took to find sample serial port code that looked as easy as I thought it should be. Finally I found some and the snipped below shows my approach (note use of shared $Rval):
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 sha +re from thread to main window $readBytes=""; # re-init } }
Once I got all of these pieces working alone (I did) I thought that I could put them together and have a working model. Expanding the model for my "real world" data stream and my "real world" display requirements and I would be in good shape.

I'm guessing that I am probably facing two (or more) problems that I did not expect. First, I seem to need some sort of a timer or change detector to cause the Tk main loop to do a callback to code that will post the passed data to the window (about once a second). Second, the always-running serial port code is blocking the serial port thread (as expected) but it also seems to be (I'm not sure) blocking the MainLoop (not expected). In addition, the data might not be getting passed (shared) and there could be more problems that I don't yet know about.

I have browsed through several books and dozens of internet postings. I have seen (at a top level) "Anatomy of the MainLoop", shared thread data, forks, callbacks, bindings, events, pipes, sockets, semaphores, blocking, nonblocking, before and after. My head is starting to spin. There is probably a good chance that one or more of these topics may solve my problem(s). But, I have spent two days on this already and I need to try go get code working this weekend. There is no way for me to dive into a study of all of these topics and still hit my goals. There probably are clean and "easy looking" ways to make a serial port thread "play nice" with a Tk main window. Any suggestions you have would be greatly appreciated.

Thank you,
Bruce

P.S. At this point I would like to learn how to finish my code. But, if it is available somewhere I would love to also have a copy of the code from PerlMonks node 416132 "Marrying Tk, Threads and SerialPort, a COM Port Monitor".


In reply to Posting thread data in a Perl/Tk window by Bruce32903

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.