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 between 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 ($PortObj); } sub attempt { $port_to_open = shift; $port_not_opening =""; if ($os =~ /Win/i){ #win32 eval { $PortObj = new Win32::SerialPort ($port_to_open) or $port_not_opening = $port_to_open; }; } else { #linux eval { $PortObj = new Device::SerialPort ("$port_to_open") or $port_not_opening = $port_to_open; }; } }