|Perl Monk, Perl Meditation|
Re: Problem with Threaded Socket Serverby BrowserUk (Pope)
|on Aug 18, 2013 at 20:19 UTC||Need Help??|
If i detach the threads or join them, when they are done it crashes my serial connection to the arduino board.
You do not close the connection to your USB device before returning from your thread proc, so the symptoms above lead me to the conclusion that when Perl tries to garbage collect your thread procs -- ie. when you either join them; or immediately when you return if you detach them; it tries to DESTROY the local copy of the usb device handle $local_arduino. And that closes your connection.
You can confirm this speculation if you enable DEBUG on the device handle and you see a debug message of the form:
You might be able to work around Device::SerialPort's lack of thread-awareness by overriding its DESTROY method:
And preventing it from closing the connection unless it is the last instance of the handle; or perhaps, only if the DESTROY method is being called from the same thread as created the connection. How you would arrange to know either of these things is down to your ingenuity.
Alternatively, and more likely to work, would be to have a single thread only communicate with your USB device.
In this scenario, you start a thread which creates the connection to the USB device; and pass it a queue handle at start up. That thread the loops reading that Q waiting for command requests from other threads; enacts them; and then returns the results to the requesting threads (via a queue handle they pass along with the command request).
Your USB handling thread would look something like this:
To make requests, your client threads then do:
Season to taste with logging and error handling.
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.