Hello,

I am developing a perl module which shall communicate by rs232 with a micro controller. The communication is done by threads and thread::queues.
Messages I want to send are put into a queue. A thread is taking messages from the queue and sends them onto the RS232 by a handle to the RS232 Win32::SerialPort object.
The same approach I do for receving rs232 stuff.
Once the receive thread got something on the rs232, the thread puts it into the receive queue. In the main thread I will handle all the received messages and send new messages.

My problem occurs on shutdown. I am not able to end the read thread as it is blocked by a call to

my ($receivedBytes, $data) = $HandleToRS232->read();
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.

I have read the doc. in the Win32::SerialPort and have come to give a try with the API from Win32API::CommPort.
There is a API for receiving with timeout (I need this timeout so I can end the thread). This API is based on the ReadFile API.

However, I do not get this API working properly. I did the following:

my ($result) = Win32API::CommPort::ReadFile( $HandleToRS232,$data,$NumberOfBytesToRead, $ActualReadBytes,$template);

But the code is breaking and I do not receive an error message.. so I do not know why..
I am quite sure I missuse somehow this ReadFile API..

Any help is very welcome!

Best regards!


In reply to Threaded RS232 communication by tobias_hofer

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.