tools/ec_list.pl
changeset 250 440ae5f6d2c3
parent 249 ee1d766dbc6b
child 251 c1d0b63a9302
equal deleted inserted replaced
249:ee1d766dbc6b 250:440ae5f6d2c3
     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; either version 2 of the
       
    18 #  License, or (at your option) any later version.
       
    19 #
       
    20 #  The IgH EtherCAT Master is distributed in the hope that it will be
       
    21 #  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    22 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    23 #  GNU General Public License for more details.
       
    24 #
       
    25 #  You should have received a copy of the GNU General Public License
       
    26 #  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    27 #  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    28 #
       
    29 #  The right to use EtherCAT Technology is granted and comes free of
       
    30 #  charge under condition of compatibility of product made by
       
    31 #  Licensee. People intending to distribute/sell products based on the
       
    32 #  code, have to sign an agreement to guarantee that products using
       
    33 #  software based on IgH EtherCAT master stay compatible with the actual
       
    34 #  EtherCAT specification (which are released themselves as an open
       
    35 #  standard) as the (only) precondition to have the right to use EtherCAT
       
    36 #  Technology, IP and trade marks.
       
    37 #
       
    38 #------------------------------------------------------------------------------
       
    39 
       
    40 use strict;
       
    41 use Getopt::Std;
       
    42 
       
    43 my $master_index;
       
    44 my $master_dir;
       
    45 
       
    46 #------------------------------------------------------------------------------
       
    47 
       
    48 &get_options;
       
    49 &query_master;
       
    50 exit 0;
       
    51 
       
    52 #------------------------------------------------------------------------------
       
    53 
       
    54 sub query_master
       
    55 {
       
    56     $master_dir = "/sys/ethercat" . $master_index;
       
    57     &query_slaves;
       
    58 }
       
    59 
       
    60 #------------------------------------------------------------------------------
       
    61 
       
    62 sub query_slaves
       
    63 {
       
    64     my $dirhandle;
       
    65     my $slave_dir;
       
    66     my $entry;
       
    67     my $slave_index;
       
    68     my $file_name;
       
    69     my $vendor_name;
       
    70     my @slaves;
       
    71     my $slave;
       
    72     my $abs;
       
    73 
       
    74     unless (opendir $dirhandle, $master_dir) {
       
    75 	print "Failed to open directory \"$master_dir\".\n";
       
    76 	exit 1;
       
    77     }
       
    78 
       
    79     while ($entry = readdir $dirhandle) {
       
    80         next unless $entry =~ /^slave(\d+)$/;
       
    81 	$slave_dir = $master_dir . "/" . $entry;
       
    82 
       
    83 	$slave = {};
       
    84 	$slave->{'ring_position'} =
       
    85 	    &read_integer("$slave_dir/ring_position");
       
    86 	$slave->{'coupler_address'} =
       
    87 	    &read_string("$slave_dir/coupler_address");
       
    88 	$slave->{'vendor_name'} =
       
    89 	    &read_string("$slave_dir/vendor_name");
       
    90 	$slave->{'product_name'} =
       
    91 	    &read_string("$slave_dir/product_name");
       
    92 	$slave->{'product_desc'} =
       
    93 	    &read_string("$slave_dir/product_desc");
       
    94 	$slave->{'type'} =
       
    95 	    &read_string("$slave_dir/type");
       
    96 
       
    97 	push @slaves, $slave;
       
    98     }
       
    99     closedir $dirhandle;
       
   100 
       
   101     @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
       
   102 
       
   103     print "EtherCAT bus listing for master $master_index:\n";
       
   104     for $slave (@slaves) {
       
   105 	if ($slave->{'type'} eq "coupler") {
       
   106 	    print "--------------------------------------------------------\n";
       
   107 	}
       
   108 
       
   109 	$abs = sprintf "%i", $slave->{'ring_position'};
       
   110 	printf(" %3s %8s   %-12s %-10s %s\n", $abs,
       
   111 	       $slave->{'coupler_address'}, $slave->{'vendor_name'},
       
   112 	       $slave->{'product_name'}, $slave->{'product_desc'});
       
   113     }
       
   114 }
       
   115 
       
   116 #------------------------------------------------------------------------------
       
   117 
       
   118 sub read_string
       
   119 {
       
   120     (my $file_name) = @_;
       
   121     my $data;
       
   122 
       
   123     $data = `cat $file_name 2>/dev/null`;
       
   124     if ($?) {
       
   125 	print "ERROR: Unable to read string $file_name!\n";
       
   126 	exit 1;
       
   127     }
       
   128 
       
   129     chomp $data;
       
   130     return $data;
       
   131 }
       
   132 
       
   133 #------------------------------------------------------------------------------
       
   134 
       
   135 sub read_integer
       
   136 {
       
   137     (my $file_name) = @_;
       
   138 
       
   139     if (`cat $file_name 2>/dev/null` !~ /^(\d+)$/) {
       
   140 	print "ERROR: Unable to read integer $file_name!\n";
       
   141 	exit 1;
       
   142     }
       
   143 
       
   144     return int $1;
       
   145 }
       
   146 
       
   147 #------------------------------------------------------------------------------
       
   148 
       
   149 sub get_options
       
   150 {
       
   151     my %opt;
       
   152     my $optret;
       
   153 
       
   154     $optret = getopts "m:h", \%opt;
       
   155 
       
   156     &print_usage if defined $opt{'h'} or $#ARGV > -1;
       
   157 
       
   158     if (defined $opt{'m'}) {
       
   159 	$master_index = $opt{'m'};
       
   160     }
       
   161     else {
       
   162 	$master_index = 0;
       
   163     }
       
   164 }
       
   165 
       
   166 #------------------------------------------------------------------------------
       
   167 
       
   168 sub print_usage
       
   169 {
       
   170     print "Usage: ec_list [OPTIONS]\n";
       
   171     print "        -m <IDX>    Query master IDX.\n";
       
   172     print "        -h          Show this help.\n";
       
   173     exit 0;
       
   174 }
       
   175 
       
   176 #------------------------------------------------------------------------------