tools/ec_list.pl
branchstable-1.0
changeset 1619 0d4119024f55
parent 1618 5cff10efb927
child 1620 9d7453c16ade
equal deleted inserted replaced
1618:5cff10efb927 1619:0d4119024f55
     1 #!/usr/bin/perl
       
     2 
       
     3 #------------------------------------------------------------------------------
       
     4 #
       
     5 #  e c _ l i s t . p l
       
     6 #
       
     7 #  Userspace tool for listing EtherCAT slaves.
       
     8 #
       
     9 #  $Id: slave.c 340 2006-04-11 10:17:30Z fp $
       
    10 #
       
    11 #  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
       
    12 #
       
    13 #  This file is part of the IgH EtherCAT Master.
       
    14 #
       
    15 #  The IgH EtherCAT Master is free software; you can redistribute it
       
    16 #  and/or modify it under the terms of the GNU General Public License
       
    17 #  as published by the Free Software Foundation; version 2 of the License.
       
    18 #
       
    19 #  The IgH EtherCAT Master is distributed in the hope that it will be
       
    20 #  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    21 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    22 #  GNU General Public License for more details.
       
    23 #
       
    24 #  You should have received a copy of the GNU General Public License
       
    25 #  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    26 #  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    27 #
       
    28 #------------------------------------------------------------------------------
       
    29 
       
    30 use strict;
       
    31 use Getopt::Std;
       
    32 
       
    33 my $master_index;
       
    34 my $master_dir;
       
    35 
       
    36 #------------------------------------------------------------------------------
       
    37 
       
    38 &get_options;
       
    39 &query_master;
       
    40 exit 0;
       
    41 
       
    42 #------------------------------------------------------------------------------
       
    43 
       
    44 sub query_master
       
    45 {
       
    46     $master_dir = "/sys/ethercat" . $master_index;
       
    47     &query_slaves;
       
    48 }
       
    49 
       
    50 #------------------------------------------------------------------------------
       
    51 
       
    52 sub query_slaves
       
    53 {
       
    54     my $dirhandle;
       
    55     my $slave_dir;
       
    56     my $entry;
       
    57     my $slave_index;
       
    58     my $file_name;
       
    59     my $vendor_name;
       
    60     my @slaves;
       
    61     my $slave;
       
    62     my $abs;
       
    63 
       
    64     unless (opendir $dirhandle, $master_dir) {
       
    65 	print "Failed to open directory \"$master_dir\".\n";
       
    66 	exit 1;
       
    67     }
       
    68 
       
    69     while ($entry = readdir $dirhandle) {
       
    70         next unless $entry =~ /^slave(\d+)$/;
       
    71 	$slave_dir = $master_dir . "/" . $entry;
       
    72 
       
    73 	$slave = {};
       
    74 	$slave->{'ring_position'} =
       
    75 	    &read_integer("$slave_dir/ring_position");
       
    76 	$slave->{'coupler_address'} =
       
    77 	    &read_string("$slave_dir/coupler_address");
       
    78 	$slave->{'vendor_name'} =
       
    79 	    &read_string("$slave_dir/vendor_name");
       
    80 	$slave->{'product_name'} =
       
    81 	    &read_string("$slave_dir/product_name");
       
    82 	$slave->{'product_desc'} =
       
    83 	    &read_string("$slave_dir/product_desc");
       
    84 	$slave->{'type'} =
       
    85 	    &read_string("$slave_dir/type");
       
    86 
       
    87 	push @slaves, $slave;
       
    88     }
       
    89     closedir $dirhandle;
       
    90 
       
    91     @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
       
    92 
       
    93     print "EtherCAT bus listing for master $master_index:\n";
       
    94     for $slave (@slaves) {
       
    95 	if ($slave->{'type'} eq "coupler") {
       
    96 	    print "--------------------------------------------------------\n";
       
    97 	}
       
    98 
       
    99 	$abs = sprintf "%i", $slave->{'ring_position'};
       
   100 	printf(" %3s %8s   %-12s %-10s %s\n", $abs,
       
   101 	       $slave->{'coupler_address'}, $slave->{'vendor_name'},
       
   102 	       $slave->{'product_name'}, $slave->{'product_desc'});
       
   103     }
       
   104 }
       
   105 
       
   106 #------------------------------------------------------------------------------
       
   107 
       
   108 sub read_string
       
   109 {
       
   110     (my $file_name) = @_;
       
   111     my $data;
       
   112 
       
   113     $data = `cat $file_name 2>/dev/null`;
       
   114     if ($?) {
       
   115 	print "ERROR: Unable to read string $file_name!\n";
       
   116 	exit 1;
       
   117     }
       
   118 
       
   119     chomp $data;
       
   120     return $data;
       
   121 }
       
   122 
       
   123 #------------------------------------------------------------------------------
       
   124 
       
   125 sub read_integer
       
   126 {
       
   127     (my $file_name) = @_;
       
   128 
       
   129     if (`cat $file_name 2>/dev/null` !~ /^(\d+)$/) {
       
   130 	print "ERROR: Unable to read integer $file_name!\n";
       
   131 	exit 1;
       
   132     }
       
   133 
       
   134     return int $1;
       
   135 }
       
   136 
       
   137 #------------------------------------------------------------------------------
       
   138 
       
   139 sub get_options
       
   140 {
       
   141     my %opt;
       
   142     my $optret;
       
   143 
       
   144     $optret = getopts "m:h", \%opt;
       
   145 
       
   146     &print_usage if defined $opt{'h'} or $#ARGV > -1;
       
   147 
       
   148     if (defined $opt{'m'}) {
       
   149 	$master_index = $opt{'m'};
       
   150     }
       
   151     else {
       
   152 	$master_index = 0;
       
   153     }
       
   154 }
       
   155 
       
   156 #------------------------------------------------------------------------------
       
   157 
       
   158 sub print_usage
       
   159 {
       
   160     print "Usage: ec_list [OPTIONS]\n";
       
   161     print "        -m <IDX>    Query master IDX.\n";
       
   162     print "        -h          Show this help.\n";
       
   163     exit 0;
       
   164 }
       
   165 
       
   166 #------------------------------------------------------------------------------