58 sub query |
58 sub query |
59 { |
59 { |
60 my $master_dir; |
60 my $master_dir; |
61 my $dirhandle; |
61 my $dirhandle; |
62 my $entry; |
62 my $entry; |
|
63 my $slave_info_file; |
63 my @slaves; |
64 my @slaves; |
64 my $slave; |
65 my $slave; |
65 my $abs; |
66 my $abs; |
66 my $line; |
67 my $line; |
67 my $ring_cols; |
68 my $ring_col_width; |
68 my $adv_cols; |
69 my $alias_col_width; |
69 my $fmt; |
70 my $fmt; |
70 my $cols; |
71 my $width; |
|
72 my $last_alias; |
|
73 my $alias_index; |
71 |
74 |
72 $master_dir = "/sys/ethercat/master" . $master_index; |
75 $master_dir = "/sys/ethercat/master" . $master_index; |
73 |
76 |
74 unless (opendir $dirhandle, $master_dir) { |
77 unless (opendir $dirhandle, $master_dir) { |
75 print "Failed to open directory \"$master_dir\".\n"; |
78 print "Failed to open directory \"$master_dir\".\n"; |
76 exit 1; |
79 exit 1; |
77 } |
80 } |
78 |
81 |
79 while ($entry = readdir $dirhandle) { |
82 while ($entry = readdir $dirhandle) { |
80 next unless $entry =~ /^slave(\d+)$/; |
83 next unless $entry =~ /^slave(\d+)$/; |
81 |
84 |
82 $slave = {}; |
85 $slave = {}; |
83 |
86 $slave_info_file = "$master_dir/$entry/info"; |
84 open INFO, "$master_dir/$entry/info" or die |
87 open INFO, $slave_info_file or die |
85 "ERROR: Failed to open $master_dir/$entry/info"; |
88 "ERROR: Failed to open $slave_info_file."; |
86 |
89 |
87 while ($line = <INFO>) { |
90 while ($line = <INFO>) { |
88 if ($line =~ /^Name: (.*)$/) { |
91 if ($line =~ /^Name: (.*)$/) { |
89 $slave->{'name'} = $1; |
92 $slave->{'name'} = $1; |
90 } |
93 } |
91 elsif ($line =~ /^Ring position: (\d+)$/) { |
94 elsif ($line =~ /^Ring position: (\d+)$/) { |
92 $slave->{'ring_position'} = $1; |
95 $slave->{'ring_position'} = $1; |
93 } |
96 } |
94 elsif ($line =~ /^Advanced position: (\d+:\d+)$/) { |
97 elsif ($line =~ /Configured station alias: .* \((\d+)\)$/) { |
95 $slave->{'advanced_position'} = $1; |
98 $slave->{'alias'} = $1; |
96 } |
99 } |
97 elsif ($line =~ /^State: (.+) /) { |
100 elsif ($line =~ /^State: (.+) /) { |
98 $slave->{'state'} = $1; |
101 $slave->{'state'} = $1; |
99 } |
102 } |
100 elsif ($line =~ /^Coupler: ([a-z]+)$/) { |
103 elsif ($line =~ /^Current consumption: (-?\d+) mA$/) { |
101 $slave->{'coupler'} = $1; |
104 $slave->{'current'} = $1; |
102 } |
105 } |
103 elsif ($line =~ /^Current consumption: (-?\d+) mA$/) { |
106 } |
104 $slave->{'current'} = $1; |
107 |
105 } |
108 close INFO; |
106 } |
109 push @slaves, $slave; |
107 |
|
108 close INFO; |
|
109 |
|
110 push @slaves, $slave; |
|
111 } |
110 } |
112 closedir $dirhandle; |
111 closedir $dirhandle; |
113 |
112 |
114 @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves; |
113 @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves; |
115 |
114 |
116 $ring_cols = 0; |
115 # create field addresses and calculate column widths |
117 $adv_cols = 0; |
116 $ring_col_width = 0; |
|
117 $alias_col_width = 0; |
|
118 $last_alias = ""; |
118 for $slave (@slaves) { |
119 for $slave (@slaves) { |
119 $cols = length $slave->{'ring_position'}; |
120 if ($slave->{'alias'}) { |
120 $ring_cols = $cols if ($cols > $ring_cols); |
121 $last_alias = $slave->{'alias'}; |
121 $cols = length $slave->{'advanced_position'}; |
122 $alias_index = 0; |
122 $adv_cols = $cols if ($cols > $adv_cols); |
123 } |
|
124 if ($last_alias) { |
|
125 $slave->{'field_address'} = "#" . $last_alias . ":" . $alias_index; |
|
126 $width = length $slave->{'field_address'}; |
|
127 $alias_col_width = $width if ($width > $alias_col_width); |
|
128 } |
|
129 $width = length $slave->{'ring_position'}; |
|
130 $ring_col_width = $width if ($width > $ring_col_width); |
|
131 $alias_index++; |
123 } |
132 } |
124 |
133 |
125 if (defined $opt{'c'}) { # display power consumtion |
134 if (defined $opt{'c'}) { # display power consumtion |
126 $fmt = sprintf " %%%is %%-%is %%6i %%6i %%s\n", |
135 $fmt = sprintf " %%%is %%%is %%6i %%6i %%s\n", |
127 $ring_cols, $adv_cols; |
136 $ring_col_width, $alias_col_width; |
128 |
137 |
129 my $current_sum = 0; |
138 my $current_sum = 0; |
130 for $slave (@slaves) { |
139 for $slave (@slaves) { |
131 if ($slave->{'coupler'} eq "yes") { |
140 &print_line if $slave->{'alias'} and !defined $opt{n}; |
132 &print_line if !defined $opt{n}; |
141 $current_sum = 0 if $slave->{'current'} < 0; |
133 $current_sum = 0; # reset current sum |
142 $current_sum -= $slave->{'current'}; |
134 } |
143 printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'}, |
135 $current_sum -= $slave->{'current'}; |
144 $slave->{'current'}, $current_sum, $slave->{'name'}); |
136 printf($fmt, $slave->{'ring_position'}, |
145 } |
137 $slave->{'advanced_position'}, $slave->{'current'}, |
146 } |
138 $current_sum, $slave->{'name'}); |
147 else { # normal display |
139 } |
148 $fmt = sprintf " %%%is %%%is %%-6s %%s\n", |
|
149 $ring_col_width, $alias_col_width; |
|
150 |
|
151 for $slave (@slaves) { |
|
152 &print_line if $slave->{'alias'} and !defined $opt{n}; |
|
153 printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'}, |
|
154 $slave->{'state'}, $slave->{'name'}); |
|
155 } |
|
156 } |
|
157 } |
|
158 |
|
159 #------------------------------------------------------------------------------ |
|
160 |
|
161 sub get_options |
|
162 { |
|
163 my $optret = getopts "m:cnh", \%opt; |
|
164 |
|
165 &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret; |
|
166 |
|
167 if (defined $opt{m}) { |
|
168 $master_index = $opt{m}; |
140 } |
169 } |
141 else { |
170 else { |
142 $fmt = sprintf " %%%is %%-%is %%-6s %%s\n", $ring_cols, $adv_cols; |
171 $master_index = 0; |
143 |
|
144 for $slave (@slaves) { |
|
145 &print_line if $slave->{'coupler'} eq "yes" and !defined $opt{n}; |
|
146 printf($fmt, $slave->{'ring_position'}, |
|
147 $slave->{'advanced_position'}, $slave->{'state'}, |
|
148 $slave->{'name'}); |
|
149 } |
|
150 } |
|
151 } |
|
152 |
|
153 #------------------------------------------------------------------------------ |
|
154 |
|
155 sub get_options |
|
156 { |
|
157 my $optret = getopts "m:cnh", \%opt; |
|
158 |
|
159 &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret; |
|
160 |
|
161 if (defined $opt{m}) { |
|
162 $master_index = $opt{m}; |
|
163 } |
|
164 else { |
|
165 $master_index = 0; |
|
166 } |
172 } |
167 } |
173 } |
168 |
174 |
169 #------------------------------------------------------------------------------ |
175 #------------------------------------------------------------------------------ |
170 |
176 |
187 { |
193 { |
188 my $winsize; |
194 my $winsize; |
189 die "no TIOCGWINSZ " unless defined &TIOCGWINSZ; |
195 die "no TIOCGWINSZ " unless defined &TIOCGWINSZ; |
190 open(TTY, "+</dev/tty") or die "No tty: $!"; |
196 open(TTY, "+</dev/tty") or die "No tty: $!"; |
191 unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) { |
197 unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) { |
192 die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ; |
198 die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ; |
193 } |
199 } |
194 (my $row, my $col, my $xpixel, my $ypixel) = unpack('S4', $winsize); |
200 (my $row, my $col, my $xpixel, my $ypixel) = unpack('S4', $winsize); |
195 return $col; |
201 return $col; |
196 } |
202 } |
197 #------------------------------------------------------------------------------ |
203 #------------------------------------------------------------------------------ |
198 |
204 |
199 sub print_line |
205 sub print_line |
200 { |
206 { |
201 for (my $i = 0; $i < $term_width; $i++) {print "-";} |
207 for (my $i = 0; $i < $term_width; $i++) { |
|
208 print "-"; |
|
209 } |
202 print "\n"; |
210 print "\n"; |
203 } |
211 } |
204 |
212 |
205 #------------------------------------------------------------------------------ |
213 #------------------------------------------------------------------------------ |