author | Florian Pose <fp@igh-essen.com> |
Fri, 13 May 2011 15:34:20 +0200 | |
changeset 2068 | 3001f6523e63 |
parent 1968 | 4f682084c643 |
child 2421 | bc2d4bf9cbe5 |
permissions | -rw-r--r-- |
1142 | 1 |
/***************************************************************************** |
2 |
* |
|
1363
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
3 |
* $Id$ |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
4 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
5 |
* Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
6 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
7 |
* This file is part of the IgH EtherCAT Master. |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
8 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
9 |
* The IgH EtherCAT Master is free software; you can redistribute it and/or |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
10 |
* modify it under the terms of the GNU General Public License version 2, as |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
11 |
* published by the Free Software Foundation. |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
12 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
13 |
* The IgH EtherCAT Master is distributed in the hope that it will be useful, |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
16 |
* Public License for more details. |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
17 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
18 |
* You should have received a copy of the GNU General Public License along |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
19 |
* with the IgH EtherCAT Master; if not, write to the Free Software |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
21 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
22 |
* --- |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
23 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
24 |
* The license mentioned above concerns the source code only. Using the |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
25 |
* EtherCAT technology and brand is only permitted in compliance with the |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
26 |
* industrial property and similar rights of Beckhoff Automation GmbH. |
1142 | 27 |
* |
28 |
****************************************************************************/ |
|
29 |
||
30 |
#include <iostream> |
|
31 |
#include <iomanip> |
|
32 |
#include <sstream> |
|
33 |
using namespace std; |
|
34 |
||
35 |
#include "CommandAlias.h" |
|
36 |
#include "sii_crc.h" |
|
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1363
diff
changeset
|
37 |
#include "MasterDevice.h" |
1142 | 38 |
|
39 |
/*****************************************************************************/ |
|
40 |
||
41 |
CommandAlias::CommandAlias(): |
|
42 |
Command("alias", "Write alias addresses.") |
|
43 |
{ |
|
44 |
} |
|
45 |
||
46 |
/*****************************************************************************/ |
|
47 |
||
1968
4f682084c643
Implemented drive_no for command-line tool; binary base name is now a
Florian Pose <fp@igh-essen.com>
parents:
1870
diff
changeset
|
48 |
string CommandAlias::helpString(const string &binaryBaseName) const |
1142 | 49 |
{ |
50 |
stringstream str; |
|
51 |
||
1968
4f682084c643
Implemented drive_no for command-line tool; binary base name is now a
Florian Pose <fp@igh-essen.com>
parents:
1870
diff
changeset
|
52 |
str << binaryBaseName << " " << getName() << " [OPTIONS] <ALIAS>" << endl |
1142 | 53 |
<< endl |
54 |
<< getBriefDescription() << endl |
|
55 |
<< endl |
|
56 |
<< "Arguments:" << endl |
|
1144
7dbfdd61812c
Bugfixes and improvements.
Florian Pose <fp@igh-essen.com>
parents:
1142
diff
changeset
|
57 |
<< " ALIAS must be an unsigned 16 bit number. Zero means" << endl |
7dbfdd61812c
Bugfixes and improvements.
Florian Pose <fp@igh-essen.com>
parents:
1142
diff
changeset
|
58 |
<< " removing an alias address." << endl |
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
59 |
<< endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
60 |
<< "If multiple slaves are selected, the --force option" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
61 |
<< "is required." << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
62 |
<< endl |
1142 | 63 |
<< "Command-specific options:" << endl |
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
64 |
<< " --alias -a <alias>" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
65 |
<< " --position -p <pos> Slave selection. See the help of" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
66 |
<< " the 'slaves' command." << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
67 |
<< " --force -f Acknowledge writing aliases of" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
68 |
<< " multiple slaves." << endl |
1142 | 69 |
<< endl |
70 |
<< numericInfo(); |
|
71 |
||
72 |
return str.str(); |
|
73 |
} |
|
74 |
||
75 |
/*****************************************************************************/ |
|
76 |
||
77 |
/** Writes the Secondary slave address (alias) to the slave's SII. |
|
78 |
*/ |
|
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1363
diff
changeset
|
79 |
void CommandAlias::execute(const StringVector &args) |
1142 | 80 |
{ |
81 |
uint16_t alias; |
|
82 |
stringstream err, strAlias; |
|
83 |
int number; |
|
1151 | 84 |
SlaveList slaves; |
85 |
SlaveList::const_iterator si; |
|
1142 | 86 |
|
87 |
if (args.size() != 1) { |
|
88 |
err << "'" << getName() << "' takes exactly one argument!"; |
|
89 |
throwInvalidUsageException(err); |
|
90 |
} |
|
91 |
||
92 |
strAlias << args[0]; |
|
93 |
strAlias |
|
94 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
95 |
>> number; |
|
96 |
if (strAlias.fail() || number < 0x0000 || number > 0xffff) { |
|
97 |
err << "Invalid alias '" << args[0] << "'!"; |
|
98 |
throwInvalidUsageException(err); |
|
99 |
} |
|
100 |
alias = number; |
|
101 |
||
1870
0cee1fb7f0fd
Added getSingleMasterIndex() for command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
1869
diff
changeset
|
102 |
MasterDevice m(getSingleMasterIndex()); |
1151 | 103 |
m.open(MasterDevice::ReadWrite); |
104 |
slaves = selectedSlaves(m); |
|
105 |
||
1166 | 106 |
if (slaves.size() > 1 && !getForce()) { |
1151 | 107 |
err << "This will write the alias addresses of " |
108 |
<< slaves.size() << " slaves to " << alias |
|
109 |
<< "! Please specify --force to proceed."; |
|
110 |
throwCommandException(err); |
|
111 |
} |
|
1142 | 112 |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
113 |
if (!slaves.size() && getVerbosity() != Quiet) { |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
114 |
cerr << "Warning: Selection matches no slaves!" << endl; |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
115 |
} |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
116 |
|
1151 | 117 |
for (si = slaves.begin(); si != slaves.end(); si++) { |
118 |
writeSlaveAlias(m, *si, alias); |
|
1142 | 119 |
} |
120 |
} |
|
121 |
||
122 |
/*****************************************************************************/ |
|
123 |
||
124 |
/** Writes the Secondary slave address (alias) to the slave's SII. |
|
125 |
*/ |
|
126 |
void CommandAlias::writeSlaveAlias( |
|
127 |
MasterDevice &m, |
|
1151 | 128 |
const ec_ioctl_slave_t &slave, |
1142 | 129 |
uint16_t alias |
130 |
) |
|
131 |
{ |
|
132 |
ec_ioctl_slave_sii_t data; |
|
133 |
stringstream err; |
|
134 |
uint8_t crc; |
|
135 |
||
136 |
if (slave.sii_nwords < 8) { |
|
137 |
err << "Current SII contents are too small to set an alias " |
|
138 |
<< "(" << slave.sii_nwords << " words)!"; |
|
139 |
throwCommandException(err); |
|
140 |
} |
|
141 |
||
142 |
// read first 8 SII words |
|
1151 | 143 |
data.slave_position = slave.position; |
1142 | 144 |
data.offset = 0; |
145 |
data.nwords = 8; |
|
146 |
data.words = new uint16_t[data.nwords]; |
|
147 |
||
148 |
try { |
|
149 |
m.readSii(&data); |
|
150 |
} catch (MasterDeviceException &e) { |
|
151 |
delete [] data.words; |
|
152 |
err << "Failed to read SII: " << e.what(); |
|
153 |
throwCommandException(err); |
|
154 |
} |
|
155 |
||
156 |
// write new alias address in word 4 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1166
diff
changeset
|
157 |
data.words[4] = cpu_to_le16(alias); |
1142 | 158 |
|
159 |
// calculate checksum over words 0 to 6 |
|
160 |
crc = calcSiiCrc((const uint8_t *) data.words, 14); |
|
161 |
||
162 |
// write new checksum into first byte of word 7 |
|
163 |
*(uint8_t *) (data.words + 7) = crc; |
|
164 |
||
165 |
// write first 8 words with new alias and checksum |
|
166 |
try { |
|
167 |
m.writeSii(&data); |
|
168 |
} catch (MasterDeviceException &e) { |
|
169 |
delete [] data.words; |
|
170 |
err << "Failed to read SII: " << e.what(); |
|
171 |
throwCommandException(err); |
|
172 |
} |
|
173 |
||
174 |
delete [] data.words; |
|
175 |
} |
|
176 |
||
177 |
/*****************************************************************************/ |