fp@186: #!/usr/bin/perl fp@186: fp@186: #------------------------------------------------------------------------------ fp@186: # fp@186: # e c _ l i s t . p l fp@186: # fp@186: # Userspace tool for listing EtherCAT slaves. fp@186: # fp@186: # $Id: slave.c 340 2006-04-11 10:17:30Z fp $ fp@186: # fp@1618: # Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@1618: # fp@1618: # This file is part of the IgH EtherCAT Master. fp@1618: # fp@1618: # The IgH EtherCAT Master is free software; you can redistribute it fp@1618: # and/or modify it under the terms of the GNU General Public License fp@1618: # as published by the Free Software Foundation; version 2 of the License. fp@1618: # fp@1618: # The IgH EtherCAT Master is distributed in the hope that it will be fp@1618: # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1618: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1618: # GNU General Public License for more details. fp@1618: # fp@1618: # You should have received a copy of the GNU General Public License fp@1618: # along with the IgH EtherCAT Master; if not, write to the Free Software fp@1618: # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1618: # fp@186: #------------------------------------------------------------------------------ fp@186: fp@186: use strict; fp@186: use Getopt::Std; fp@186: fp@186: my $master_index; fp@186: my $master_dir; fp@186: fp@186: #------------------------------------------------------------------------------ fp@186: fp@186: &get_options; fp@186: &query_master; fp@186: exit 0; fp@186: fp@186: #------------------------------------------------------------------------------ fp@186: fp@186: sub query_master fp@186: { fp@186: $master_dir = "/sys/ethercat" . $master_index; fp@186: &query_slaves; fp@186: } fp@186: fp@186: #------------------------------------------------------------------------------ fp@186: fp@186: sub query_slaves fp@186: { fp@186: my $dirhandle; fp@186: my $slave_dir; fp@186: my $entry; fp@186: my $slave_index; fp@186: my $file_name; fp@186: my $vendor_name; fp@186: my @slaves; fp@186: my $slave; fp@188: my $abs; fp@186: fp@186: unless (opendir $dirhandle, $master_dir) { fp@186: print "Failed to open directory \"$master_dir\".\n"; fp@186: exit 1; fp@186: } fp@186: fp@186: while ($entry = readdir $dirhandle) { fp@186: next unless $entry =~ /^slave(\d+)$/; fp@186: $slave_dir = $master_dir . "/" . $entry; fp@186: fp@186: $slave = {}; fp@186: $slave->{'ring_position'} = fp@186: &read_integer("$slave_dir/ring_position"); fp@188: $slave->{'coupler_address'} = fp@188: &read_string("$slave_dir/coupler_address"); fp@186: $slave->{'vendor_name'} = fp@186: &read_string("$slave_dir/vendor_name"); fp@186: $slave->{'product_name'} = fp@186: &read_string("$slave_dir/product_name"); fp@186: $slave->{'product_desc'} = fp@186: &read_string("$slave_dir/product_desc"); fp@186: $slave->{'type'} = fp@186: &read_string("$slave_dir/type"); fp@186: fp@186: push @slaves, $slave; fp@186: } fp@186: closedir $dirhandle; fp@186: fp@186: @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves; fp@186: fp@186: print "EtherCAT bus listing for master $master_index:\n"; fp@186: for $slave (@slaves) { fp@186: if ($slave->{'type'} eq "coupler") { fp@186: print "--------------------------------------------------------\n"; fp@186: } fp@186: fp@186: $abs = sprintf "%i", $slave->{'ring_position'}; fp@188: printf(" %3s %8s %-12s %-10s %s\n", $abs, fp@188: $slave->{'coupler_address'}, $slave->{'vendor_name'}, fp@188: $slave->{'product_name'}, $slave->{'product_desc'}); fp@186: } fp@186: } fp@186: fp@186: #------------------------------------------------------------------------------ fp@186: fp@186: sub read_string fp@186: { fp@186: (my $file_name) = @_; fp@186: my $data; fp@186: fp@186: $data = `cat $file_name 2>/dev/null`; fp@186: if ($?) { fp@186: print "ERROR: Unable to read string $file_name!\n"; fp@186: exit 1; fp@186: } fp@186: fp@186: chomp $data; fp@186: return $data; fp@186: } fp@186: fp@186: #------------------------------------------------------------------------------ fp@186: fp@186: sub read_integer fp@186: { fp@186: (my $file_name) = @_; fp@186: fp@186: if (`cat $file_name 2>/dev/null` !~ /^(\d+)$/) { fp@186: print "ERROR: Unable to read integer $file_name!\n"; fp@186: exit 1; fp@186: } fp@186: fp@186: return int $1; fp@186: } fp@186: fp@186: #------------------------------------------------------------------------------ fp@186: fp@186: sub get_options fp@186: { fp@186: my %opt; fp@186: my $optret; fp@186: fp@186: $optret = getopts "m:h", \%opt; fp@186: fp@186: &print_usage if defined $opt{'h'} or $#ARGV > -1; fp@186: fp@186: if (defined $opt{'m'}) { fp@186: $master_index = $opt{'m'}; fp@186: } fp@186: else { fp@186: $master_index = 0; fp@186: } fp@186: } fp@186: fp@186: #------------------------------------------------------------------------------ fp@186: fp@186: sub print_usage fp@186: { fp@186: print "Usage: ec_list [OPTIONS]\n"; fp@186: print " -m Query master IDX.\n"; fp@186: print " -h Show this help.\n"; fp@186: exit 0; fp@186: } fp@186: fp@186: #------------------------------------------------------------------------------