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.)

In reply to Win32::SerialPort ; close / open problem by philipMac

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.