Dear Bretheren, is there a way to poll/find a working serial port in the system without crashing it? My Linux setup works just fine, however, once I'm on Win32 everything crashes!?

sub connect{ if ($os =~ /Win/i){ #this only works with COM1 my @ports_win = ('COM1'); foreach (@ports_win){ open_uart($_); last if ($PortObj); } } else { #This works great my @ports = ('/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ +ttyUSB3','/dev/ttyUSB4','/dev/ttyUSB5','/dev/ttyUSB6'); foreach (@ports){ open_uart($_); last if ($PortObj); } } } } sub open_uart { $port_to_open = shift; &attempt($port_to_open); if ($port_not_opening){ #try to connect! for (my $i=1; $i<=15; $i++){ &attempt($port_to_open); print "trying to connect $i\n"; last unless ($port_not_opening); } } return if ($port_not_opening); $PortObj->is_rs232; $PortObj->handshake("none"); $PortObj->user_msg(1); $PortObj->baudrate($conn_speed); $PortObj->parity("none"); $PortObj->databits(8); $PortObj->stopbits(1); $PortObj->read_const_time(500); # 500 milliseconds = 0.5 + seconds $PortObj->read_char_time(500); #was 5 # avg time betwe +en read char $PortObj->buffers(4096, 4096); # read, write $PortObj->parity_enable(1); $PortObj->write_settings || undef $PortObj; #print "Can't change Device_Control_Block: $^E\n" unless ($Por +tObj); } sub attempt { $port_to_open = shift; $port_not_opening =""; if ($os =~ /Win/i){ #win32 eval { $PortObj = new Win32::SerialPort ($port_to_ope +n) or $port_not_opening = $port_to_open; }; } else { #linux eval { $PortObj = new Device::SerialPort ("$port_to_o +pen") or $port_not_opening = $port_to_open; }; } }

In reply to finding a serial port with Win32::SerialPort by pashanoid

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.