http://www.perlmonks.org?node_id=1016023


in reply to Threaded RS232 communication

First off, you are probably limiting your audience unnecessarily by mentioning "threads" in the title.

Your basic problem, that of needing a non-blocking read, has nothing to do with threading.

Secondly, do you really need to terminate your read thread? That is, if you detached it, it would just silently melt into the ether when your main thread decides to exit the process.

But if you do really need to, I think that you've already discovered the 'right' solution, namely read_bg(). All you nee to do is use the API correctly and alter your coding strategy a little. (NOTE: None of the following is tested.)

So I changed to "background read" my ($receivedBytes, $data) = $HandleToRS232->read_bg(); Doing this I got nothing in $data so I did a call to my($done, $count_in, $string_in) = $HandleToRS232->read_done(1); Then I receive the message in the $string_in variable, but the call to read_done is also blocking.

Based on my reading the source -- and I may well have misread it --


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Threaded RS232 communication
by tobias_hofer (Friar) on Jan 30, 2013 at 13:48 UTC

    I went back to $HandleRS232->read();
    Because it is simpler for me and simply meets the requirements.
    I have also detached the threads so they can go into the ether. :-D
    It working perfect! Super!

    Thanks a lot!