Modbus example: a virtual motor

The following code gives an example of Modbus I/O. It shows how a Beckhoff motor is implemented as a virtual motor.

#!/usr/bin/perl -w
#
# file name: 
#   /online_dir/vm4.pl
# 
# the X motor 
#
use Spectra; 

my ($method, $value_new) = @ARGV; 
my $status = 1; 
#
# the controller has a time-out after 10s, be sure to re-open the Modbus
#
Spectra::modbuClose( "hasa106bc01");

if( "$method" eq "set_position")
{
    my $ziel = $value_new; 
    Spectra::modbusWrite( "hasa106bc01", 0x4002, ($ziel & 0xffff, $ziel >> 16)); 
    Spectra::modbusWrite( "hasa106bc01", 0x4000, 1); 
    my $count = 0; 
    delete $Spectra::SYM{ INTERRUPT_SCAN}; 
    while( 1)
    {
	my @data = Spectra::modbusRead( "hasa106bc01", 0x4004, 2); 
	my $pos = $data[0] + ($data[1] << 16); 
	last if( Spectra::key() == 32); 
	last if( $Spectra::SYM{ INTERRUPT_SCAN}); 
	last if( $pos == $ziel);
	Util::refresh() if( !($count++ % 10)); 
    }
    Spectra::modbusWrite( "hasa106bc01", 0x4000, 2); 
    goto finish;
}

if( "$method" eq "get_position")
{
    my @data = Spectra::modbusRead( "hasa106bc01", 0x4004, 2); 
    my $pos = $data[0] + ($data[1] << 16); 
    $SYM{RETURN_VALUE} = $pos;
    goto finish;
}

if( "$method" eq "get_limit_min")
{
    $SYM{RETURN_VALUE} = 0;
    goto finish;
}

if( "$method" eq "get_limit_max")
{
    $SYM{RETURN_VALUE} = 1000000;
    goto finish;
}

finish:
 $status;