I am not sure that your code needs to explicitly have any threads at all! Windows is doing the "heavy lifting" interrupt driven low level device service code for you. 9600 baud is about 1,000 chars/ second +- start/stop/data bit settings. The serial port itself may only have a 16 byte buffer. Somebody is doing that and it isn't Perl code.

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".

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.


In reply to Re: RS232 and Tk with threads by Marshall
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.