script/lsec
changeset 766 97e98cbd0cd2
parent 643 78929d878b2c
child 835 23fd8b510803
equal deleted inserted replaced
765:5eaf2af36427 766:97e98cbd0cd2
    61     my $dirhandle;
    61     my $dirhandle;
    62     my $entry;
    62     my $entry;
    63     my $slave_info_file;
    63     my $slave_info_file;
    64     my @slaves;
    64     my @slaves;
    65     my $slave;
    65     my $slave;
    66     my $abs;
       
    67     my $line;
    66     my $line;
    68     my $ring_col_width;
    67     my $ring_col_width;
    69     my $alias_col_width;
    68     my $alias_col_width;
    70     my $fmt;
    69     my $fmt;
    71     my $width;
    70     my $width;
    72     my $last_alias;
    71     my $last_alias;
    73     my $alias_index;
    72     my $alias_index;
       
    73     my $category;
    74 
    74 
    75     $master_dir = "/sys/ethercat/master" . $master_index;
    75     $master_dir = "/sys/ethercat/master" . $master_index;
    76 
    76 
    77     unless (opendir $dirhandle, $master_dir) {
    77     unless (opendir $dirhandle, $master_dir) {
    78         print "Failed to open directory \"$master_dir\".\n";
    78         print "Failed to open directory \"$master_dir\".\n";
    85         $slave = {};
    85         $slave = {};
    86         $slave_info_file = "$master_dir/$entry/info"; 
    86         $slave_info_file = "$master_dir/$entry/info"; 
    87         open INFO, $slave_info_file or die
    87         open INFO, $slave_info_file or die
    88             "ERROR: Failed to open $slave_info_file.";
    88             "ERROR: Failed to open $slave_info_file.";
    89 
    89 
       
    90         $category = "";
    90         while ($line = <INFO>) {
    91         while ($line = <INFO>) {
    91             if ($line =~ /^Name: (.*)$/) {
    92             # remember category
    92                 $slave->{'name'} = $1;
    93             if ($line =~ /^([^\s][^:]*):$/) {
       
    94                 $category = $1;
       
    95             } elsif ($line =~ /^\s*$/) {
       
    96                 $category = "";
    93             }
    97             }
    94             elsif ($line =~ /^Ring position: (\d+)$/) {
    98 
    95                 $slave->{'ring_position'} = $1;
    99             if ($category eq "") {
    96             }
   100                 if ($line =~ /^Ring position: (\d+)$/) {
    97             elsif ($line =~ /Configured station alias: .* \((\d+)\)$/) {
   101                     $slave->{'ring_position'} = $1;
    98                 $slave->{'alias'} = $1;
   102                 } elsif ($line =~ /^State: (.+) /) {
    99             }
   103                     $slave->{'state'} = $1;
   100             elsif ($line =~ /^State: (.+) /) {
   104                 } elsif ($line =~ /^Configured station alias: .* \((\d+)\)$/) {
   101                 $slave->{'state'} = $1;
   105                     $slave->{'alias'} = $1;
   102             }
   106                 } elsif ($line =~ /^Current consumption: (-?\d+) mA$/) {
   103             elsif ($line =~ /^Current consumption: (-?\d+) mA$/) {
   107                     $slave->{'current'} = $1;
   104                 $slave->{'current'} = $1;
   108                 }
       
   109             } elsif ($category eq "Identity") {
       
   110                 if ($line =~ /Vendor ID: .* \((\d+)\)$/) {
       
   111                     $slave->{'vendor'} = $1;
       
   112                 } elsif ($line =~ /Product code: .* \((\d+)\)$/) {
       
   113                     $slave->{'product'} = $1;
       
   114                 }
       
   115             } elsif ($category eq "General") {
       
   116                 if ($line =~ /Name: (.*)$/) {
       
   117                     $slave->{'name'} = $1;
       
   118                 }
   105             }
   119             }
   106         }
   120         }
   107 
   121 
   108         close INFO;
   122         close INFO;
   109         push @slaves, $slave;
   123         push @slaves, $slave;
   129         $width = length $slave->{'ring_position'};
   143         $width = length $slave->{'ring_position'};
   130         $ring_col_width = $width if ($width > $ring_col_width);
   144         $ring_col_width = $width if ($width > $ring_col_width);
   131         $alias_index++;
   145         $alias_index++;
   132     }
   146     }
   133 
   147 
       
   148     # replace empty name with vendor id and product code
       
   149     for $slave (@slaves) {
       
   150         unless (defined $slave->{'name'}) {
       
   151             $slave->{'name'} = sprintf("0x%08X:0x%08X", $slave->{'vendor'},
       
   152                     $slave->{'product'});
       
   153         }
       
   154     }
       
   155 
   134     if (defined $opt{'c'}) { # display power consumtion
   156     if (defined $opt{'c'}) { # display power consumtion
   135         $fmt = sprintf " %%%is  %%%is  %%6i  %%6i  %%s\n",
   157         $fmt = sprintf " %%%is  %%%is  %%6i  %%6i  %%s\n",
   136             $ring_col_width, $alias_col_width;
   158             $ring_col_width, $alias_col_width;
   137 
   159 
   138         my $current_sum = 0;
   160         my $current_sum = 0;
   180     chomp $cmd;
   202     chomp $cmd;
   181     print "Usage: $cmd [OPTIONS]\n";
   203     print "Usage: $cmd [OPTIONS]\n";
   182     print "        -m <IDX>    Query master <IDX>.\n";
   204     print "        -m <IDX>    Query master <IDX>.\n";
   183     print "        -c          Display current [mA] ";
   205     print "        -c          Display current [mA] ";
   184     print "(3: consumption, 4: remaining).\n";
   206     print "(3: consumption, 4: remaining).\n";
   185     print "        -n          Display no coupler lines.\n";
   207     print "        -n          Do not display lines before aliased slaves.\n";
   186     print "        -h          Show this help.\n";
   208     print "        -h          Show this help.\n";
   187     exit 0;
   209     exit 0;
   188 }
   210 }
   189 
   211 
   190 #------------------------------------------------------------------------------
   212 #------------------------------------------------------------------------------