Greetings, Perl monks:

I'm developing an application which communicates with an Arduino via USB. It runs in a POE environment. I use the Device::SerialPort module to talk to the USB port (as a file).

At times I want to close the connection (when certain errors occur) and re-open it. So far, closing the connection is not working for me. Here's how I open it:

use Symbol qw(gensym); use Device::SerialPort; (etc.) my $port, $handle; $handle = gensym(); $port = tie(*$handle, "Device::SerialPort", "/dev/ttyUSB0"); $port->baudrate(57600); (etc.)

By passing $handle to a POE ReadWrite wheel, I can communicate perfectly with the Arduino. I use the following code to try to close the connection, prior to re-opening it:

$port->close; undef $port; untie *$handle;

and this results in the error message:

<fh> select error: Bad file descriptor (hits=-1)

I have to admit that I don't understand what the "gensym" and "tie" stuff is all about; I'm just following the examples in the documentation, so it's almost certainly due to my ignorance. Can anyone explain how to properly close this port?


In reply to Destroying tied serial port by Marais

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.