script/lsec.pl
branchstable-1.1
changeset 1722 14024a941c2e
parent 1715 e675450f2174
child 1739 5fcbd29151d2
equal deleted inserted replaced
1721:8d1fcfe68ced 1722:14024a941c2e
    35 #  standard) as the (only) precondition to have the right to use EtherCAT
    35 #  standard) as the (only) precondition to have the right to use EtherCAT
    36 #  Technology, IP and trade marks.
    36 #  Technology, IP and trade marks.
    37 #
    37 #
    38 #------------------------------------------------------------------------------
    38 #------------------------------------------------------------------------------
    39 
    39 
       
    40 require 'sys/ioctl.ph';
       
    41 
    40 use strict;
    42 use strict;
    41 use Getopt::Std;
    43 use Getopt::Std;
    42 
    44 
       
    45 my %opt;
    43 my $master_index;
    46 my $master_index;
    44 my $master_dir;
    47 my $master_dir;
       
    48 my $term_width;
    45 
    49 
    46 #------------------------------------------------------------------------------
    50 #------------------------------------------------------------------------------
    47 
    51 
       
    52 $term_width = &get_terminal_width;
    48 &get_options;
    53 &get_options;
    49 &query_master;
    54 &query_master;
    50 exit 0;
    55 exit 0;
    51 
    56 
    52 #------------------------------------------------------------------------------
    57 #------------------------------------------------------------------------------
    64     my $dirhandle;
    69     my $dirhandle;
    65     my $entry;
    70     my $entry;
    66     my @slaves;
    71     my @slaves;
    67     my $slave;
    72     my $slave;
    68     my $abs;
    73     my $abs;
    69 	my $line;
    74     my $line;
       
    75     my $ring_cols;
       
    76     my $adv_cols;
       
    77     my $fmt;
       
    78     my $cols;
    70 
    79 
    71     unless (opendir $dirhandle, $master_dir) {
    80     unless (opendir $dirhandle, $master_dir) {
    72 		print "Failed to open directory \"$master_dir\".\n";
    81 		print "Failed to open directory \"$master_dir\".\n";
    73 		exit 1;
    82 		exit 1;
    74     }
    83     }
    92 				$slave->{'advanced_position'} = $1;
   101 				$slave->{'advanced_position'} = $1;
    93 			}
   102 			}
    94 			elsif ($line =~ /^State: (.+)$/) {
   103 			elsif ($line =~ /^State: (.+)$/) {
    95 				$slave->{'state'} = $1;
   104 				$slave->{'state'} = $1;
    96 			}
   105 			}
       
   106 			elsif ($line =~ /^Coupler: ([a-z]+)$/) {
       
   107 				$slave->{'coupler'} = $1;
       
   108 			}
    97 		}
   109 		}
    98 
   110 
    99 		close INFO;
   111 		close INFO;
   100 
   112 
   101 		push @slaves, $slave;
   113 		push @slaves, $slave;
   102     }
   114     }
   103     closedir $dirhandle;
   115     closedir $dirhandle;
   104 
   116 
   105     @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
   117     @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
   106 
   118 
   107     print "EtherCAT bus listing for master $master_index:\n";
   119     $ring_cols = 0;
       
   120     $adv_cols = 0;
   108     for $slave (@slaves) {
   121     for $slave (@slaves) {
   109 		$abs = sprintf "%i", $slave->{'ring_position'};
   122 	$cols = length $slave->{'ring_position'};
   110 		printf(" %3s %8s  %-6s  %s\n",
   123 	$ring_cols = $cols if ($cols > $ring_cols);
   111 			   $abs, $slave->{'advanced_position'},
   124 	$cols = length $slave->{'advanced_position'};
   112 			   $slave->{'state'}, $slave->{'name'});
   125 	$adv_cols = $cols if ($cols > $adv_cols);
       
   126     }
       
   127     $fmt = sprintf " %%%is  %%-%is  %%-6s  %%s\n", $ring_cols, $adv_cols;
       
   128 
       
   129     for $slave (@slaves) {
       
   130 	&print_line if $slave->{'coupler'} eq "yes" and !defined $opt{n};
       
   131 	printf($fmt, $slave->{'ring_position'}, $slave->{'advanced_position'},
       
   132 	       $slave->{'state'}, $slave->{'name'});
   113     }
   133     }
   114 }
   134 }
   115 
   135 
   116 #------------------------------------------------------------------------------
   136 #------------------------------------------------------------------------------
   117 
   137 
   118 sub get_options
   138 sub get_options
   119 {
   139 {
   120     my %opt;
   140     my $optret = getopts "m:nh", \%opt;
   121     my $optret;
       
   122 
   141 
   123     $optret = getopts "m:h", \%opt;
   142     &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret;
   124 
   143 
   125     &print_usage if defined $opt{'h'} or $#ARGV > -1 or !$optret;
   144     if (defined $opt{m}) {
   126 
   145 	$master_index = $opt{m};
   127     if (defined $opt{'m'}) {
       
   128 		$master_index = $opt{'m'};
       
   129     }
   146     }
   130     else {
   147     else {
   131 		$master_index = 0;
   148 	$master_index = 0;
   132     }
   149     }
   133 }
   150 }
   134 
   151 
   135 #------------------------------------------------------------------------------
   152 #------------------------------------------------------------------------------
   136 
   153 
   137 sub print_usage
   154 sub print_usage
   138 {
   155 {
   139 	my $cmd = `basename $0`;
   156     my $cmd = `basename $0`;
   140 	chomp $cmd;
   157     chomp $cmd;
   141     print "Usage: $cmd [OPTIONS]\n";
   158     print "Usage: $cmd [OPTIONS]\n";
   142     print "        -m <IDX>    Query master <IDX>.\n";
   159     print "        -m <IDX>    Query master <IDX>.\n";
       
   160     print "        -n          Display no coupler lines.\n";
   143     print "        -h          Show this help.\n";
   161     print "        -h          Show this help.\n";
   144     exit 0;
   162     exit 0;
   145 }
   163 }
   146 
   164 
   147 #------------------------------------------------------------------------------
   165 #------------------------------------------------------------------------------
       
   166 
       
   167 sub get_terminal_width
       
   168 {
       
   169     my $winsize;
       
   170     die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;
       
   171     open(TTY, "+</dev/tty") or die "No tty: $!";
       
   172     unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) {
       
   173 	die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
       
   174     }
       
   175     (my $row, my $col, my $xpixel, my $ypixel) = unpack('S4', $winsize);
       
   176     return $col;
       
   177 }
       
   178 #------------------------------------------------------------------------------
       
   179 
       
   180 sub print_line
       
   181 {
       
   182     for (my $i = 0; $i < $term_width; $i++) {print "-";}
       
   183     print "\n";
       
   184 }
       
   185 
       
   186 #------------------------------------------------------------------------------