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

tobias_hofer has asked for the wisdom of the Perl Monks concerning the following question:

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!