1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void main()
{
long nErr, nPort;
AmsAddr Addr;
PAmsAddr pAddr = &Addr;
DWORD dwData;
// Kommunikationsport auf dem ADS Router öffnen
nPort = AdsPortOpen();
nErr = AdsGetLocalAddress(pAddr);
if (nErr) cerr << "Error: AdsGetLocalAddress: " << nErr << '\n';
// TwinCAT2 RTS1 Port = 801
pAddr->port = 801;
}
1
2
3
4
5
6
7
8
typedef struct {
AmsNetId netId;
USHORT port;
} AmsAddr, *PAmsAddr;
typedef struct {
UCHAR b[6];
} AmsNetId, *PAmsNetId;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
sub new {
my $class = shift;
my $Path2Dll = shift;
my $self = {
# Dll function Path Function IN OUT
_LocalAddr => _GetRefToDllFunction("$Path2Dll", 'AdsGetLocalAddress', 'P', 'N'),
};
bless $self, $class;
return $self
}
sub _GetRefToDllFunction {
my $refToDllFunction = Win32::API->new(@_);
if(not defined $refToDllFunction) {
die "ERROR: Trying to load $_[1] from $_[0].dll. Failure: $!\n";
}
return $refToDllFunction;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
_LocalAddr => _GetRefToDllFunction("$Path2Dll", 'AdsGetLocalAddress', 'S', 'N'),
...
Win32::API::Struct->typedef(
AmsNetId => qw(
UCHAR b[6];
)
);
Win32::API::Struct->typedef(
AmsAddr => qw(
AmsNetId netId;
USHORT port;
)
);
Win32::API->Import("$self->{_Path2Dll}" => 'LONG AdsGetLocalAddress(LPAmsAddr pt)');
my $pt = Win32::API::Struct->new('AmsAddr');
AdsGetLocalAddress($pt);
1
2
3
4
5
6
7
8
9
sub GetLocalAddr {
my $self = shift;
my @AmsNetId = (0,0,0,0,0,0);
my $Port = 0;
my $Param = pack ('C6 S', @AmsNetId,$Port);
my $Result = $self->{_LocalAddr}->Call($Param);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
_LocalAddr => _GetRefToDllFunction("$Path2Dll", 'AdsGetLocalAddress', 'P', 'N','_cdecl'),
sub GetLocalAddr {
my $self = shift;
my @AmsNetId = (0,0,0,0,0,0);
my $Port = 0;
my $Param = pack ('C6 S', @AmsNetId,$Port);
# ggf. noch pack NetId und Port
my $Result = $self->{_LocalAddr}->Call($Param);
print $Result;
(@AmsNetId,$Port) = unpack('C6 S',$Param);
print $Port;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sub GetLocalAddr1 {
my $self = shift;
Win32::API::Struct->typedef(
AmsNetId => qw(
UCHAR b[6];
)
);
Win32::API::Struct->typedef(
AmsAddr => qw(
AmsNetId netId;
USHORT port;
)
);
Win32::API->Import("$self->{_Path2Dll}" => 'LONG _cdecl AdsGetLocalAddress(LPAmsAddr pt)');
my %pt;
tie %pt, 'Win32::API::Struct'=> 'AmsAddr';
AdsGetLocalAddress(\%pt);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sub GetLocalAddr0 {
my $self = shift;
my $Param1 = pack ('C6S',0,0,0,0,0,0,0,0 );
# ggf. noch pack NetId und Port
my $Result = $self->{_LocalAddr}->Call($Param1);
print $Result;
my ($A,$B,$C,$D,$E,$F,$P) = unpack('C6S',$Param1);
print "A:$A\n";
print "B:$B\n";
print "C:$C\n";
print "D:$D\n";
print "E:$E\n";
print "F:$F\n";
print "P:$P\n";
}