Hey guys,
First a quick bit of context:
I am venturing into WindowsLand (Edit : Windows XP ) and I need to write something that will periodically (1 second) look at a serial port, and if there is one plugged in will start polling it and stashing the data received.
Initially I wrote something that uses Win32::SerialPort, creates a $PortObj, polls every second, and does exactly what I want it to.
The added complexity comes when I want to have the plug in plug out capabilities.
So, using Win32::Daemon I wrote something that looks every second at the port, and can detect whenever anything is plugged in or plugged out. Whenever something is plugged in, the Daemon creates a PortObj and works away.
However, when the device is unplugged, the Daemon realises, and runs
$PortObj->close || warn "Oh Noes! No close!";
undef $PortObj;
I receive no warnings. The $PortObj exists still, and seems to close properly (even though the physical connection has been pulled).
Then, when my Daemon see's a connection again, I try to create a new $PortObj I get "Access to Port COMX is denied" and general borkage.
If I try to open and close and then reopen $PortObj without unplugging the device there is no problem.
I do note that in the documentation, under the Bugs section, there is a note (and I quote from http://search.cpan.org/~bbirth/Win32-SerialPort-0.19/lib/Win32/SerialPort.pm#BUGS)
"On Win32, a port must close before it can be reopened again by the same process. If a physical port can be accessed using more than one name (see above), all names are treated as one. The perl script can also be run multiple times within a single batch file or shell script. The Makefile.PL spawns subshells with backticks to run the test suite on Perl 5.003 - ugly, but it works."
To me there appears to be something hairy going on with the dealloc of resources between Perl and Windows that I am screwing up because of my device being pulled. It seems like the close is not working properly despite the lack of warnings being kicked out.
But, basically I just need this to work. Elegance need not be an issue at the moment. I considered doing something like the tail of the doc says, and stop the Daemon when I see the connect go away, but then how do I start it again to look for new connects? (Unless I write another Daemon that runs the portd ???)
I dunno. I have a suspicion that you lot may be smarter than me, and one of you is going to be able to whip out a single LOC that will force the freeing of the COM port and allow perl to grab it again. Any ideas?
(Also, seeing as this is my fp here, I should take a second to thank all of you for your excellent site, on which I have been lurking all over for years.)