author | Florian Pose <fp@igh-essen.com> |
Wed, 29 Apr 2009 09:43:04 +0000 | |
changeset 1421 | 043a518831b2 |
parent 1363 | 11c0b2caa253 |
child 1826 | ec6223c3b7ec |
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" |
|
37 |
||
38 |
/*****************************************************************************/ |
|
39 |
||
40 |
CommandAlias::CommandAlias(): |
|
41 |
Command("alias", "Write alias addresses.") |
|
42 |
{ |
|
43 |
} |
|
44 |
||
45 |
/*****************************************************************************/ |
|
46 |
||
47 |
string CommandAlias::helpString() const |
|
48 |
{ |
|
49 |
stringstream str; |
|
50 |
||
51 |
str << getName() << " [OPTIONS] <ALIAS>" << endl |
|
52 |
<< endl |
|
53 |
<< getBriefDescription() << endl |
|
54 |
<< endl |
|
55 |
<< "Arguments:" << endl |
|
1144
7dbfdd61812c
Bugfixes and improvements.
Florian Pose <fp@igh-essen.com>
parents:
1142
diff
changeset
|
56 |
<< " 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
|
57 |
<< " removing an alias address." << endl |
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
58 |
<< endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
59 |
<< "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
|
60 |
<< "is required." << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
61 |
<< endl |
1142 | 62 |
<< "Command-specific options:" << endl |
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
63 |
<< " --alias -a <alias>" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
64 |
<< " --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
|
65 |
<< " the 'slaves' command." << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
66 |
<< " --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
|
67 |
<< " multiple slaves." << endl |
1142 | 68 |
<< endl |
69 |
<< numericInfo(); |
|
70 |
||
71 |
return str.str(); |
|
72 |
} |
|
73 |
||
74 |
/*****************************************************************************/ |
|
75 |
||
76 |
/** Writes the Secondary slave address (alias) to the slave's SII. |
|
77 |
*/ |
|
78 |
void CommandAlias::execute(MasterDevice &m, const StringVector &args) |
|
79 |
{ |
|
80 |
uint16_t alias; |
|
81 |
stringstream err, strAlias; |
|
82 |
int number; |
|
1151 | 83 |
SlaveList slaves; |
84 |
SlaveList::const_iterator si; |
|
1142 | 85 |
|
86 |
if (args.size() != 1) { |
|
87 |
err << "'" << getName() << "' takes exactly one argument!"; |
|
88 |
throwInvalidUsageException(err); |
|
89 |
} |
|
90 |
||
91 |
strAlias << args[0]; |
|
92 |
strAlias |
|
93 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
94 |
>> number; |
|
95 |
if (strAlias.fail() || number < 0x0000 || number > 0xffff) { |
|
96 |
err << "Invalid alias '" << args[0] << "'!"; |
|
97 |
throwInvalidUsageException(err); |
|
98 |
} |
|
99 |
alias = number; |
|
100 |
||
1151 | 101 |
m.open(MasterDevice::ReadWrite); |
102 |
slaves = selectedSlaves(m); |
|
103 |
||
1166 | 104 |
if (slaves.size() > 1 && !getForce()) { |
1151 | 105 |
err << "This will write the alias addresses of " |
106 |
<< slaves.size() << " slaves to " << alias |
|
107 |
<< "! Please specify --force to proceed."; |
|
108 |
throwCommandException(err); |
|
109 |
} |
|
1142 | 110 |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
111 |
if (!slaves.size() && getVerbosity() != Quiet) { |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
112 |
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
|
113 |
} |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
114 |
|
1151 | 115 |
for (si = slaves.begin(); si != slaves.end(); si++) { |
116 |
writeSlaveAlias(m, *si, alias); |
|
1142 | 117 |
} |
118 |
} |
|
119 |
||
120 |
/*****************************************************************************/ |
|
121 |
||
122 |
/** Writes the Secondary slave address (alias) to the slave's SII. |
|
123 |
*/ |
|
124 |
void CommandAlias::writeSlaveAlias( |
|
125 |
MasterDevice &m, |
|
1151 | 126 |
const ec_ioctl_slave_t &slave, |
1142 | 127 |
uint16_t alias |
128 |
) |
|
129 |
{ |
|
130 |
ec_ioctl_slave_sii_t data; |
|
131 |
stringstream err; |
|
132 |
uint8_t crc; |
|
133 |
||
134 |
if (slave.sii_nwords < 8) { |
|
135 |
err << "Current SII contents are too small to set an alias " |
|
136 |
<< "(" << slave.sii_nwords << " words)!"; |
|
137 |
throwCommandException(err); |
|
138 |
} |
|
139 |
||
140 |
// read first 8 SII words |
|
1151 | 141 |
data.slave_position = slave.position; |
1142 | 142 |
data.offset = 0; |
143 |
data.nwords = 8; |
|
144 |
data.words = new uint16_t[data.nwords]; |
|
145 |
||
146 |
try { |
|
147 |
m.readSii(&data); |
|
148 |
} catch (MasterDeviceException &e) { |
|
149 |
delete [] data.words; |
|
150 |
err << "Failed to read SII: " << e.what(); |
|
151 |
throwCommandException(err); |
|
152 |
} |
|
153 |
||
154 |
// 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
|
155 |
data.words[4] = cpu_to_le16(alias); |
1142 | 156 |
|
157 |
// calculate checksum over words 0 to 6 |
|
158 |
crc = calcSiiCrc((const uint8_t *) data.words, 14); |
|
159 |
||
160 |
// write new checksum into first byte of word 7 |
|
161 |
*(uint8_t *) (data.words + 7) = crc; |
|
162 |
||
163 |
// write first 8 words with new alias and checksum |
|
164 |
try { |
|
165 |
m.writeSii(&data); |
|
166 |
} catch (MasterDeviceException &e) { |
|
167 |
delete [] data.words; |
|
168 |
err << "Failed to read SII: " << e.what(); |
|
169 |
throwCommandException(err); |
|
170 |
} |
|
171 |
||
172 |
delete [] data.words; |
|
173 |
} |
|
174 |
||
175 |
/*****************************************************************************/ |