Hi Jan, I’ll try that in just a tick. Meanwhile here is exactly what happened:
I used this code successfully:
#!/usr/bin/perl
use Tk;
use vars qw( $OS_win $ob $file );
BEGIN {
$OS_win = ($^O eq "MSWin32") ? 1 : 0;
if ($OS_win) {
eval "use Win32::SerialPort 0.11";
die "$@\n" if ($@);
}
else {
eval "use Device::SerialPort";
die "$@\n" if ($@);
}
} # End BEGIN
$file = 'tpj4.cfg';
if ($OS_win) {
$ob = Win32::SerialPort->start ($file);
}
else {
$ob = Device::SerialPort->start ($file);
}
die "Can't open serial port from $file: $^E\n" unless ($ob);
my $baud = $ob->baudrate;
print "baud from configuration: $baud\n";
# Centre servo & slider
servo(0,3000);
$MW = MainWindow->new;
$hello = $MW->Button ( -text => 'Reset',
-command => [\&servoReset,0,3000]);
$bar = $MW->Scale ( -orient => horizontal,
-width => 20,
-length => 200,
-from => 500,
-to => 5500,
-variable => \$variable,
-command => [ \&servo, $variable]
);
$hello->pack;
$bar->pack;
set $bar 3000;
MainLoop;
sub servo {
my $pos = $_[1];
my $position=sprintf ('%.0f', $pos/128);
my $pack = pack "CCCCCC", 128, 1, 4, 0, $position, 0;
print "Position: $position\n";
$ob->write($pack);
undef $pack;
return
}
sub servoReset {
my $pos = $_[1];
print "$pos\n";
set $bar $pos;
my $position=sprintf ('%.0f', $pos/128);
my $pack = pack "CCCCCC", 128, 1, 4, 0, $position, 0;
print "Position: $position\n";
$ob->write($pack);
undef $pack;
return
}
print "baud from configuration: $baud\n";
undef $ob;
==== End of Script ====
I then altered the sub routine ‘servo’:
my $pack = pack “CCCCCC”, 128, 1, 4, 0, $position, 0;
to
my $pack = pack “CCCCCC”, 128, $servonumber, 4, 0, $position, 0;
And passed a servo number so that I could use the same routine for more than one servo. The controller has not worked since
I’ll go try your suggestion.
Many thanks,
Darren