script/lsec.pl
branchstable-1.1
changeset 1715 e675450f2174
child 1722 14024a941c2e
equal deleted inserted replaced
1714:f149465f4c63 1715:e675450f2174
       
     1 #!/usr/bin/perl
       
     2 
       
     3 #------------------------------------------------------------------------------
       
     4 #
       
     5 #  l s e c  -  List EtherCAT
       
     6 #
       
     7 #  Userspace tool for listing EtherCAT slaves.
       
     8 #
       
     9 #  $Id$
       
    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 $entry;
       
    66     my @slaves;
       
    67     my $slave;
       
    68     my $abs;
       
    69 	my $line;
       
    70 
       
    71     unless (opendir $dirhandle, $master_dir) {
       
    72 		print "Failed to open directory \"$master_dir\".\n";
       
    73 		exit 1;
       
    74     }
       
    75 
       
    76     while ($entry = readdir $dirhandle) {
       
    77         next unless $entry =~ /^slave(\d+)$/;
       
    78 
       
    79 		$slave = {};
       
    80 
       
    81 		open INFO, "$master_dir/$entry/info" or die
       
    82 			"ERROR: Failed to open $master_dir/$entry/info";
       
    83 
       
    84 		while ($line = <INFO>) {
       
    85 			if ($line =~ /^Name: (.*)$/) {
       
    86 				$slave->{'name'} = $1;
       
    87 			}
       
    88 			elsif ($line =~ /^Ring position: (\d+)$/) {
       
    89 				$slave->{'ring_position'} = $1;
       
    90 			}
       
    91 			elsif ($line =~ /^Advanced position: (\d+:\d+)$/) {
       
    92 				$slave->{'advanced_position'} = $1;
       
    93 			}
       
    94 			elsif ($line =~ /^State: (.+)$/) {
       
    95 				$slave->{'state'} = $1;
       
    96 			}
       
    97 		}
       
    98 
       
    99 		close INFO;
       
   100 
       
   101 		push @slaves, $slave;
       
   102     }
       
   103     closedir $dirhandle;
       
   104 
       
   105     @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
       
   106 
       
   107     print "EtherCAT bus listing for master $master_index:\n";
       
   108     for $slave (@slaves) {
       
   109 		$abs = sprintf "%i", $slave->{'ring_position'};
       
   110 		printf(" %3s %8s  %-6s  %s\n",
       
   111 			   $abs, $slave->{'advanced_position'},
       
   112 			   $slave->{'state'}, $slave->{'name'});
       
   113     }
       
   114 }
       
   115 
       
   116 #------------------------------------------------------------------------------
       
   117 
       
   118 sub get_options
       
   119 {
       
   120     my %opt;
       
   121     my $optret;
       
   122 
       
   123     $optret = getopts "m:h", \%opt;
       
   124 
       
   125     &print_usage if defined $opt{'h'} or $#ARGV > -1 or !$optret;
       
   126 
       
   127     if (defined $opt{'m'}) {
       
   128 		$master_index = $opt{'m'};
       
   129     }
       
   130     else {
       
   131 		$master_index = 0;
       
   132     }
       
   133 }
       
   134 
       
   135 #------------------------------------------------------------------------------
       
   136 
       
   137 sub print_usage
       
   138 {
       
   139 	my $cmd = `basename $0`;
       
   140 	chomp $cmd;
       
   141     print "Usage: $cmd [OPTIONS]\n";
       
   142     print "        -m <IDX>    Query master <IDX>.\n";
       
   143     print "        -h          Show this help.\n";
       
   144     exit 0;
       
   145 }
       
   146 
       
   147 #------------------------------------------------------------------------------