/
bordomacchina.pl
343 lines (268 loc) · 8.52 KB
/
bordomacchina.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/perl
#----------------------------------------------------------------
#Perl test application for the Elexol Ethernet IO24 device.
#Developed using Perl v5.6.1 built for i386-linux.
#
#Summary:
# High Level Methods:
# FindDeviceOnNetwork()
# ConfigurePortDirection($IP_Address, $Port, "A", 0)
# ReadPortValue($IP_Address, $Port, "a")
# WritePortValue($IP_Address, $Port, "a", 5)
#
# Low Level Methods:
# OpenUDP($IP_Address, $Port)
# ReadUDP($sock)
#
#This code may be used freely in any application provided that
#the following web site is referenced in that application:
#
#
#Comments may be sent to:
#
#Author: Geoff Collett
# Geoff.Collett@AcaciaLT.com.au
# Acacia Lateral Technologies Pty Ltd.
# http://www.acacialt.com.au/
#
#Version 1.0 13 April 2005
#----------------------------------------------------------------
#
#);
# Use the socket library
use IO::Socket;
use DateTime;
# Automatically flush output
$| = 1;
#---------------------------------------------------------------
# Initialisation
#---------------------------------------------------------------
$Port = 2424;
$Direzione = $ARGV[0];
$Tempo = $ARGV[1];
#print "DIR: " .$Direzione."\n";
#print "Tempo: " .$Tempo."\n";
#WILLY
$IP_Address="10.10.10.10";
#print qq(
# L'ip della card e': [$IP_Address]);
# Determine the broadcast address on this network
# Otherwise use ifconfig to see what this is
$Broadcast_Address = inet_ntoa(INADDR_BROADCAST);
# Catch signals
$SIG{HUP} = \&CloseUp; #-1
$SIG{INT} = \&CloseUp; #-2
$SIG{QUIT} = \&CloseUp; #-3
$SIG{TRAP} = \&CloseUp; #-5
$SIG{KILL} = \&CloseUp; #-9
$SIG{TERM} = \&CloseUp; #-15
$SIG{STOP} = \&CloseUp; #-17
# Search for all EIO24 devices on the network
#($IP_Address, $Device_Name, $MAC_Address, $Version) = &FindDeviceOnNetwork();
#
#print qq(
#Device IP Address is [$IP_Address]
#Device Name is [$Device_Name]
#Device MAC Address is [$MAC_Address]
#Device Version is [$Version]
#
#);
#---------------------------------------------------------------
# Read the values on the ports
#---------------------------------------------------------------
#---------------------------------------------------------------
# Make port A an input (0=output, 255=input on all bits) port
#---------------------------------------------------------------
&ConfigurePortDirection($IP_Address, $Port, "A", 0);
$i=0;
#while ($i<1) {
#
&WritePortValue($IP_Address, $Port, "A", $Direzione);
# `sleep $Tempo`;
#&WritePortValue($IP_Address, $Port, "A", 0);
#$i++;
# stop
#&WritePortValue($IP_Address, $Port, "A", 96);
# avanti
#&WritePortValue($IP_Address, $Port, "A", 112);
# indietro
#&WritePortValue($IP_Address, $Port, "A", 240);
# avanti dx
#&WritePortValue($IP_Address, $Port, "A", 80);
# avanti sx
#&WritePortValue($IP_Address, $Port, "A", 48);
# indietro sx
#&WritePortValue($IP_Address, $Port, "A", 176);
# indietro dx
#&WritePortValue($IP_Address, $Port, "A", 208);
# ---- fine programma robot test ------
#converto il numero in binario per poter accedere ai singoli valori dei contatti
#da esadecimale a decimale
#$dec = hex($val);
# da decimale a binario
#$bin = unpack("B*", pack("N", $dec));
# rimuovo gli zeri iniziali
#$bin =~ s/^0+(?=\d)//;
#$time = DateTime->now();
# stampo il valore degli otto contatti (0|1)
#print qq($val\n);
#};
exit;
#---------------------------------------------------------------
#---------------------------------------------------------------
#-------------- Supporting Subroutines Follow ------------------
#---------------------------------------------------------------
#---------------------------------------------------------------
#---------------------------------------------------------------
# Open a UDP connection to the device
#---------------------------------------------------------------
sub OpenUDP
{
my ($IP_Address, $Port) = @_;
#print "Connecting to EIO24 on $IP_Address:$Port\n";
$sock = new IO::Socket::INET->new
(
PeerAddr => $IP_Address,
PeerPort => $Port,
Proto => 'udp',
Reuse => 1,
Timeout => 5,
Type => SOCK_DGRAM
) or die "Unable to connect to EIO24: $!";
$sock->autoflush(1);
#print "UDP Port opened to $IP_Address:$Port\n\n";
return($sock);
}
#---------------------------------------------------------------
# Read any reply from the UDP port
#---------------------------------------------------------------
sub ReadUDP
{
my ($sock) = @_;
# Accept a maximum reply of 20 bytes
my $MAXLEN=20;
# Clear the receive buffer
my $response = "";
# Attempt to receive the message from the UDP socket
$sock->recv($response, $MAXLEN);
# Return the response to the caller
return($response);
}
#---------------------------------------------------------------
# Catch all signals and shutdown gracefully.
#---------------------------------------------------------------
sub CloseUp
{
my $signal = shift;
print "Server received signal [$signal]. Shutting down.\n";
&WritePortValue($IP_Address, $Port, "A", 0);
exit;
}
#---------------------------------------------------------------
# Send broadcast message to find the device on the network
#---------------------------------------------------------------
sub FindDeviceOnNetwork
{
#print "Sending broadcast to $Broadcast_Address:$Port to locate the EIO24 device\n";
# Prepare to broadcast on the local network
use Socket;
socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die;
setsockopt(SOCKET, SOL_SOCKET, SO_BROADCAST, 1);
$ip = inet_aton($Broadcast_Address);
# Send message IO24 as broadcast
$msg="IO24";
send(SOCKET, $msg, 0, sockaddr_in($Port, $ip)) == length($msg) or die;
# Read the response from the device
#print "Waiting for response from card\n";
$val = '';
($Device_Address = recv(SOCKET, $val, 200, 0)) || die "recv: $!";
# Determine the IP address, mac and version number
($port, $hisiaddr) = sockaddr_in($Device_Address);
$Device_Name = gethostbyaddr($hisiaddr, AF_INET);
$Device_IP = inet_ntoa($hisiaddr);
# Extract the mac address and device version number
(@mac_bytes) = unpack("x4 H2 H2 H2 H2 H2 H2 H2", $val);
($version) = unpack("x4 x6 H2", $val);
$mac = join(":", @mac_bytes);
#print "Received response from $Device_Name [$Device_IP:$port]\n";
#print "MAC [$mac] $Version [$version]\n";
# Return this information
return($Device_IP, $Device_Name, $mac, $version);
}
#---------------------------------------------------------------
# Read the value of a port (a,b,c) and return the value
#---------------------------------------------------------------
sub ReadPortValue
{
my ($IP_Address, $Port, $CardPort) = @_;
# Check that a valid port was supplied
if($CardPort !~ /[abc]/)
{
print "Error: you must supply a valid port of a, b or c\n";
return -1;
}
# Open a connection to the device
$sock = &OpenUDP($IP_Address, $Port);
# Request the port data
print $sock "$CardPort";
# Read the value (ie Ax where A is the port and x is the data)
$val = &ReadUDP($sock);
# Ignore the port and read the value
($data) = unpack("x H2", $val);
# Close the connection
close($sock);
return($data);
}
#---------------------------------------------------------------
# Configure the nominated port for input (11111111) or output (00000000)
#---------------------------------------------------------------
sub ConfigurePortDirection
{
my ($IP_Address, $Port, $CardPort, $Direction) = @_;
# Check that a valid port was supplied
if($CardPort !~ /[ABC]/)
{
print "Error: you must supply a valid port of A, B or C\n";
return -1;
}
if($Direction < 0 || $Direction > 255)
{
print "Error: you must supply a valid port direction mask of 0 to 255\n";
return -1;
}
# Open a connection to the device
$sock = &OpenUDP($IP_Address, $Port);
# Configure the port
$data = pack("a a c", "!", $CardPort, $Direction);
print $sock $data;
# Close the connection
close($sock);
return(0);
}
#---------------------------------------------------------------
# Write to the nominated port
#---------------------------------------------------------------
sub WritePortValue
{
my ($IP_Address, $Port, $CardPort, $Value) = @_;
# Check that a valid port was supplied
if($CardPort !~ /[ABC]/)
{
print "Error: you must supply a valid port of A, B or C\n";
return -1;
}
if($Value < 0 || $Value > 255)
{
print "Error: you must supply a valid data value of 0 to 255\n";
return -1;
}
# Open a connection to the device
#print "Writing [$i] to [$IP_Address:$Port] port [$CardPort]\n";
$sock = &OpenUDP($IP_Address, $Port);
# Configure the port
$data = pack("a c", $CardPort, $Value);
print $sock $data;
# Close the connection
close($sock);
return(0);
}