pashanoid has asked for the wisdom of the Perl Monks concerning the following question:
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; }; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: finding a serial port with Win32::SerialPort
by Marshall (Canon) on Aug 22, 2011 at 10:20 UTC | |
by pashanoid (Scribe) on Aug 22, 2011 at 10:24 UTC | |
by Marshall (Canon) on Aug 22, 2011 at 11:58 UTC | |
by pashanoid (Scribe) on Aug 22, 2011 at 15:02 UTC | |
by Marshall (Canon) on Aug 24, 2011 at 11:52 UTC | |
by Anonymous Monk on Aug 22, 2011 at 10:59 UTC | |
by pashanoid (Scribe) on Aug 22, 2011 at 14:59 UTC | |
|
Re: finding a serial port with Win32::SerialPort
by ajose (Acolyte) on Aug 25, 2011 at 18:58 UTC |