ec_list tool for listing the bus in user space
authorFlorian Pose <fp@igh-essen.com>
Tue, 11 Apr 2006 12:54:01 +0000 (2006-04-11)
changeset 186 644a053fe103
parent 185 af570302dcee
child 187 01613bde2796
ec_list tool for listing the bus in user space
install.sh
tools/ec_list.pl
--- a/install.sh	Tue Apr 11 12:51:20 2006 +0000
+++ b/install.sh	Tue Apr 11 12:54:01 2006 +0000
@@ -63,6 +63,11 @@
     ln -s /etc/init.d/ethercat /usr/sbin/rcethercat || exit 1
 fi
 
+# Install tools
+
+echo "  installing tools..."
+cp tools/ec_list.pl /usr/local/bin/ec_list || exit 1
+
 # Finish
 
 echo "EtherCAT installer done."
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/ec_list.pl	Tue Apr 11 12:54:01 2006 +0000
@@ -0,0 +1,162 @@
+#!/usr/bin/perl
+
+#------------------------------------------------------------------------------
+#
+#  e c _ l i s t . p l
+#
+#  Userspace tool for listing EtherCAT slaves.
+#
+#  $Id: slave.c 340 2006-04-11 10:17:30Z fp $
+#
+#------------------------------------------------------------------------------
+
+use strict;
+use Getopt::Std;
+
+my $master_index;
+my $master_dir;
+
+#------------------------------------------------------------------------------
+
+&get_options;
+&query_master;
+exit 0;
+
+#------------------------------------------------------------------------------
+
+sub query_master
+{
+    $master_dir = "/sys/ethercat" . $master_index;
+    &query_slaves;
+}
+
+#------------------------------------------------------------------------------
+
+sub query_slaves
+{
+    my $dirhandle;
+    my $slave_dir;
+    my $entry;
+    my $slave_index;
+    my $file_name;
+    my $vendor_name;
+    my @slaves;
+    my $slave;
+
+    unless (opendir $dirhandle, $master_dir) {
+	print "Failed to open directory \"$master_dir\".\n";
+	exit 1;
+    }
+
+    while ($entry = readdir $dirhandle) {
+        next unless $entry =~ /^slave(\d+)$/;
+	$slave_dir = $master_dir . "/" . $entry;
+
+	$slave = {};
+	$slave->{'ring_position'} =
+	    &read_integer("$slave_dir/ring_position");
+	$slave->{'station_address'} =
+	    &read_integer("$slave_dir/station_address");
+	$slave->{'vendor_name'} =
+	    &read_string("$slave_dir/vendor_name");
+	$slave->{'product_name'} =
+	    &read_string("$slave_dir/product_name");
+	$slave->{'product_desc'} =
+	    &read_string("$slave_dir/product_desc");
+	$slave->{'type'} =
+	    &read_string("$slave_dir/type");
+
+	push @slaves, $slave;
+    }
+    closedir $dirhandle;
+
+    @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
+
+    my $coupler_index = -1;
+    my $slave_index = 0;
+    my $abs;
+    my $rel;
+    print "EtherCAT bus listing for master $master_index:\n";
+    for $slave (@slaves) {
+	if ($slave->{'type'} eq "coupler") {
+	    print "--------------------------------------------------------\n";
+	    $coupler_index++;
+	    $slave_index = 0;
+	}
+
+	$abs = sprintf "%i", $slave->{'ring_position'};
+	$rel = sprintf "%i:%i", $coupler_index, $slave_index;
+	printf("%4s %6s   %-15s %-15s %-15s\n", $abs, $rel,
+	       $slave->{'vendor_name'}, $slave->{'product_name'},
+	       $slave->{'product_desc'});
+
+	if ($slave->{'type'} eq "coupler") {
+	    print "--------------------------------------------------------\n";
+	}
+
+	$slave_index++;
+    }
+    print "--------------------------------------------------------\n";
+}
+
+#------------------------------------------------------------------------------
+
+sub read_string
+{
+    (my $file_name) = @_;
+    my $data;
+
+    $data = `cat $file_name 2>/dev/null`;
+    if ($?) {
+	print "ERROR: Unable to read string $file_name!\n";
+	exit 1;
+    }
+
+    chomp $data;
+    return $data;
+}
+
+#------------------------------------------------------------------------------
+
+sub read_integer
+{
+    (my $file_name) = @_;
+
+    if (`cat $file_name 2>/dev/null` !~ /^(\d+)$/) {
+	print "ERROR: Unable to read integer $file_name!\n";
+	exit 1;
+    }
+
+    return int $1;
+}
+
+#------------------------------------------------------------------------------
+
+sub get_options
+{
+    my %opt;
+    my $optret;
+
+    $optret = getopts "m:h", \%opt;
+
+    &print_usage if defined $opt{'h'} or $#ARGV > -1;
+
+    if (defined $opt{'m'}) {
+	$master_index = $opt{'m'};
+    }
+    else {
+	$master_index = 0;
+    }
+}
+
+#------------------------------------------------------------------------------
+
+sub print_usage
+{
+    print "Usage: ec_list [OPTIONS]\n";
+    print "        -m <IDX>    Query master IDX.\n";
+    print "        -h          Show this help.\n";
+    exit 0;
+}
+
+#------------------------------------------------------------------------------