use Win32::SerialPort 0.05; use strict; my $current_position=2350; my $mask_total=135; my $vlc="vlc.exe"; my $serial_port="COM1"; my $detected_ratio; my $serialobject; my $interval=$current_position/$mask_total; my $adjustment; print "Step interval = $interval\n"; unless ($serialobject = Win32::SerialPort->new ($serial_port)) { printf "could not open serial port\n"; exit 1; } $serialobject->baudrate(2400) || die "bad baudrate"; $serialobject->parity('none') || die "bad parity"; $serialobject->databits(8) || die "bad databits"; $serialobject->stopbits(1) || die "bad stopbits"; $serialobject->write_settings || undef $serialobject; my $pid = open(VLC, "$vlc 2>&1 |"); print "Resetting...\n"; $serialobject->write("r".chr(0)); get_serial_response(); while () { if ($_ =~/ratio/) { my @tmpratio=split (/ /); $detected_ratio=$tmpratio[5]; chomp $detected_ratio; print "Detected Ratio = $detected_ratio\n"; print "Current Postition: $current_position\n"; my $move; if ($detected_ratio > $current_position) { $adjustment= $detected_ratio-$current_position; print "Adjustment: Open $adjustment\n"; $move=int($adjustment/$interval); print "Open: ".$move." increments\n"; $serialobject->write("o".chr($move)); $current_position=$detected_ratio; get_serial_response(); } elsif ($detected_ratio < $current_position) { $adjustment= $current_position-$detected_ratio; print "Adjustment: Close $adjustment\n"; $move=int($adjustment/$interval); print "Closing: ".$move." increments\n"; $serialobject->write("c".chr($move)); $current_position=$detected_ratio; get_serial_response(); } else { print "No Ratio Change\n"; }; }; }; sub get_serial_response { my $current_time=time(); my $serin; while (!$serin ) { if ((time()-$current_time) < 10) { $serin=$serialobject->input(); } else { print "Position error - no response\n"; last; }; }; print "Positioning complete \n\n"; };