Serial ports have basically three error conditions:

  1. Serial adapter (USB dongle) unplugged.
  2. Serial cable unplugged
  3. Remote device hanging/not reacting

(1) is something that needs to be handled in the driver and the Win32::SerialPort module. I don't use Windows (and my coworker uses hardware that has an integrated USB port), so i'm not sure exactly how to detect and handle this.

Depending on the circumstances, you may or may not be able to detect (2). With modern USB Dongles and devices not using most of the serial lines, there might be no way to detect (by hardware) if the cable is plugged in, so you'd handle (2) like (3), see below. If it is a a classic serial connection with all the signals, you might be able to inquire the status of "Data terminal ready" and "Data Carrier detect" (i think) lines, but i rather doubt that your setup still has support for these.

For (3) and most likely (2), the only thing you can do is exchange some sort of keep-alive messages. Many serial protocols have a way of doing that, but this really is device specific. Maybe the protocol has some sort of lifeticks you can request from the remote device. Or you can try to request some sort of device status if you have not received data from that device in a certain timeframe. Some protocols also have dummy/no-operation commands. If the device has a way to identify itself (often implemented on replies to ENQ or DC* bytes, see your favourite ASCII table), you can also use that to check if the device is still working.


In reply to Re: Win32::SerialPort - How do I detect if device is still connected? by cavac
in thread Win32::SerialPort - How do I detect if device is still connected? by petro4213

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.