author | Florian Pose <fp@igh-essen.com> |
Mon, 14 Dec 2009 13:25:50 +0100 | |
changeset 1586 | eb9185dfa8ac |
parent 1363 | 11c0b2caa253 |
child 1804 | 742607c464c4 |
permissions | -rw-r--r-- |
1142 | 1 |
/***************************************************************************** |
2 |
* |
|
1363
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
diff
changeset
|
3 |
* $Id$ |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
diff
changeset
|
4 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
diff
changeset
|
5 |
* Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
diff
changeset
|
6 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
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:
1334
diff
changeset
|
8 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
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:
1334
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:
1334
diff
changeset
|
11 |
* published by the Free Software Foundation. |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
diff
changeset
|
12 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
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:
1334
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:
1334
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:
1334
diff
changeset
|
16 |
* Public License for more details. |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
diff
changeset
|
17 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
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:
1334
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:
1334
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:
1334
diff
changeset
|
21 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
diff
changeset
|
22 |
* --- |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
diff
changeset
|
23 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1334
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:
1334
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:
1334
diff
changeset
|
26 |
* industrial property and similar rights of Beckhoff Automation GmbH. |
1142 | 27 |
* |
28 |
****************************************************************************/ |
|
29 |
||
30 |
#include <iostream> |
|
31 |
#include <iomanip> |
|
32 |
using namespace std; |
|
33 |
||
34 |
#include "CommandDownload.h" |
|
35 |
||
36 |
/*****************************************************************************/ |
|
37 |
||
38 |
CommandDownload::CommandDownload(): |
|
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
39 |
SdoCommand("download", "Write an SDO entry to a slave.") |
1142 | 40 |
{ |
41 |
} |
|
42 |
||
43 |
/*****************************************************************************/ |
|
44 |
||
45 |
string CommandDownload::helpString() const |
|
46 |
{ |
|
47 |
stringstream str; |
|
48 |
||
49 |
str << getName() << " [OPTIONS] <INDEX> <SUBINDEX> <VALUE>" << endl |
|
50 |
<< endl |
|
51 |
<< getBriefDescription() << endl |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
52 |
<< endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
53 |
<< "This command requires a single slave to be selected." << endl |
1142 | 54 |
<< endl |
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
55 |
<< "The data type of the SDO entry is taken from the SDO" << endl |
1142 | 56 |
<< "dictionary by default. It can be overridden with the" << endl |
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
57 |
<< "--type option. If the slave does not support the SDO" << endl |
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
58 |
<< "information service or the SDO is not in the dictionary," << endl |
1142 | 59 |
<< "the --type option is mandatory." << endl |
60 |
<< endl |
|
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
61 |
<< "These are the valid SDO entry data types:" << endl |
1334
da3d22a27500
merge -c1617 branches/stable-1.4: Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1327
diff
changeset
|
62 |
<< " int8, int16, int32, uint8, uint16, uint32, string," << endl |
da3d22a27500
merge -c1617 branches/stable-1.4: Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1327
diff
changeset
|
63 |
<< " octet_string." << endl |
1142 | 64 |
<< endl |
1144
7dbfdd61812c
Bugfixes and improvements.
Florian Pose <fp@igh-essen.com>
parents:
1142
diff
changeset
|
65 |
<< "Arguments:" << endl |
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
66 |
<< " INDEX is the SDO index and must be an unsigned" << endl |
1142 | 67 |
<< " 16 bit number." << endl |
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
68 |
<< " SUBINDEX is the SDO entry subindex and must be an" << endl |
1142 | 69 |
<< " unsigned 8 bit number." << endl |
70 |
<< " VALUE is the value to download and must correspond" << endl |
|
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
71 |
<< " to the SDO entry datatype (see above)." << endl |
1142 | 72 |
<< endl |
73 |
<< "Command-specific options:" << endl |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
74 |
<< " --alias -a <alias>" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
75 |
<< " --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:
1155
diff
changeset
|
76 |
<< " the 'slaves' command." << endl |
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
77 |
<< " --type -t <type> SDO entry data type (see above)." << endl |
1142 | 78 |
<< endl |
79 |
<< numericInfo(); |
|
80 |
||
81 |
return str.str(); |
|
82 |
} |
|
83 |
||
84 |
/****************************************************************************/ |
|
85 |
||
86 |
void CommandDownload::execute(MasterDevice &m, const StringVector &args) |
|
87 |
{ |
|
88 |
stringstream strIndex, strSubIndex, strValue, err; |
|
89 |
ec_ioctl_slave_sdo_download_t data; |
|
90 |
unsigned int number; |
|
1184
75cd6681eb08
Introduced SdoCommand class to replace coe_datatypes.[ch]; added CoE abort codes in user space-
Florian Pose <fp@igh-essen.com>
parents:
1166
diff
changeset
|
91 |
const DataType *dataType = NULL; |
1151 | 92 |
SlaveList slaves; |
1142 | 93 |
|
94 |
if (args.size() != 3) { |
|
95 |
err << "'" << getName() << "' takes 3 arguments!"; |
|
96 |
throwInvalidUsageException(err); |
|
97 |
} |
|
98 |
||
99 |
strIndex << args[0]; |
|
100 |
strIndex |
|
101 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
102 |
>> data.sdo_index; |
|
103 |
if (strIndex.fail()) { |
|
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
104 |
err << "Invalid SDO index '" << args[0] << "'!"; |
1142 | 105 |
throwInvalidUsageException(err); |
106 |
} |
|
107 |
||
108 |
strSubIndex << args[1]; |
|
109 |
strSubIndex |
|
110 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
111 |
>> number; |
|
112 |
if (strSubIndex.fail() || number > 0xff) { |
|
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
113 |
err << "Invalid SDO subindex '" << args[1] << "'!"; |
1142 | 114 |
throwInvalidUsageException(err); |
115 |
} |
|
116 |
data.sdo_entry_subindex = number; |
|
117 |
||
1151 | 118 |
m.open(MasterDevice::ReadWrite); |
119 |
slaves = selectedSlaves(m); |
|
120 |
if (slaves.size() != 1) { |
|
1155
bd4e5b544473
Common message for single slave selections.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
121 |
throwSingleSlaveRequired(slaves.size()); |
1151 | 122 |
} |
123 |
data.slave_position = slaves.front().position; |
|
124 |
||
1166 | 125 |
if (!getDataType().empty()) { // data type specified |
126 |
if (!(dataType = findDataType(getDataType()))) { |
|
127 |
err << "Invalid data type '" << getDataType() << "'!"; |
|
1142 | 128 |
throwInvalidUsageException(err); |
129 |
} |
|
130 |
} else { // no data type specified: fetch from dictionary |
|
131 |
ec_ioctl_slave_sdo_entry_t entry; |
|
132 |
||
133 |
try { |
|
1151 | 134 |
m.getSdoEntry(&entry, data.slave_position, |
1142 | 135 |
data.sdo_index, data.sdo_entry_subindex); |
136 |
} catch (MasterDeviceException &e) { |
|
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
137 |
err << "Failed to determine SDO entry data type. " |
1142 | 138 |
<< "Please specify --type."; |
139 |
throwCommandException(err); |
|
140 |
} |
|
141 |
if (!(dataType = findDataType(entry.data_type))) { |
|
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
142 |
err << "PDO entry has unknown data type 0x" |
1142 | 143 |
<< hex << setfill('0') << setw(4) << entry.data_type << "!" |
144 |
<< " Please specify --type."; |
|
145 |
throwCommandException(err); |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
if (dataType->byteSize) { |
|
150 |
data.data_size = dataType->byteSize; |
|
151 |
} else { |
|
152 |
data.data_size = DefaultBufferSize; |
|
153 |
} |
|
154 |
||
155 |
data.data = new uint8_t[data.data_size + 1]; |
|
156 |
||
157 |
strValue << args[2]; |
|
158 |
strValue >> resetiosflags(ios::basefield); // guess base from prefix |
|
159 |
strValue.exceptions(ios::failbit); |
|
160 |
||
161 |
try { |
|
162 |
switch (dataType->coeCode) { |
|
163 |
case 0x0002: // int8 |
|
164 |
{ |
|
165 |
int16_t val; // uint8_t is interpreted as char |
|
166 |
strValue >> val; |
|
167 |
if (val > 127 || val < -128) |
|
168 |
throw ios::failure("Value out of range"); |
|
169 |
*data.data = val; |
|
170 |
break; |
|
171 |
} |
|
172 |
case 0x0003: // int16 |
|
173 |
{ |
|
174 |
int16_t val; |
|
175 |
strValue >> val; |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
176 |
*(int16_t *) data.data = cpu_to_le16(val); |
1142 | 177 |
break; |
178 |
} |
|
179 |
case 0x0004: // int32 |
|
180 |
{ |
|
181 |
int32_t val; |
|
182 |
strValue >> val; |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
183 |
*(int32_t *) data.data = cpu_to_le32(val); |
1142 | 184 |
break; |
185 |
} |
|
186 |
case 0x0005: // uint8 |
|
187 |
{ |
|
188 |
uint16_t val; // uint8_t is interpreted as char |
|
189 |
strValue >> val; |
|
190 |
if (val > 0xff) |
|
191 |
throw ios::failure("Value out of range"); |
|
192 |
*data.data = val; |
|
193 |
break; |
|
194 |
} |
|
195 |
case 0x0006: // uint16 |
|
196 |
{ |
|
197 |
uint16_t val; |
|
198 |
strValue >> val; |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
199 |
*(uint16_t *) data.data = cpu_to_le16(val); |
1142 | 200 |
break; |
201 |
} |
|
202 |
case 0x0007: // uint32 |
|
203 |
{ |
|
204 |
uint32_t val; |
|
205 |
strValue >> val; |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
206 |
*(uint32_t *) data.data = cpu_to_le32(val); |
1142 | 207 |
break; |
208 |
} |
|
209 |
case 0x0009: // string |
|
210 |
if (strValue.str().size() >= data.data_size) { |
|
211 |
err << "String too large"; |
|
212 |
throwCommandException(err); |
|
213 |
} |
|
214 |
data.data_size = strValue.str().size(); |
|
215 |
strValue >> (char *) data.data; |
|
216 |
break; |
|
1334
da3d22a27500
merge -c1617 branches/stable-1.4: Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1327
diff
changeset
|
217 |
case 0x000a: // octet_string |
da3d22a27500
merge -c1617 branches/stable-1.4: Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1327
diff
changeset
|
218 |
if (strValue.str().size() >= data.data_size) { |
da3d22a27500
merge -c1617 branches/stable-1.4: Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1327
diff
changeset
|
219 |
err << "String too large"; |
da3d22a27500
merge -c1617 branches/stable-1.4: Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1327
diff
changeset
|
220 |
throwCommandException(err); |
da3d22a27500
merge -c1617 branches/stable-1.4: Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1327
diff
changeset
|
221 |
} |
da3d22a27500
merge -c1617 branches/stable-1.4: Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1327
diff
changeset
|
222 |
data.data_size = strValue.str().size(); |
da3d22a27500
merge -c1617 branches/stable-1.4: Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1327
diff
changeset
|
223 |
strValue >> (char *) data.data; |
da3d22a27500
merge -c1617 branches/stable-1.4: Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1327
diff
changeset
|
224 |
break; |
1142 | 225 |
|
226 |
default: |
|
227 |
delete [] data.data; |
|
228 |
err << "Unknown data type 0x" << hex << dataType->coeCode; |
|
229 |
throwCommandException(err); |
|
230 |
} |
|
231 |
} catch (ios::failure &e) { |
|
232 |
delete [] data.data; |
|
233 |
err << "Invalid value argument '" << args[2] |
|
234 |
<< "' for type '" << dataType->name << "'!"; |
|
235 |
throwInvalidUsageException(err); |
|
236 |
} |
|
237 |
||
238 |
try { |
|
1151 | 239 |
m.sdoDownload(&data); |
1184
75cd6681eb08
Introduced SdoCommand class to replace coe_datatypes.[ch]; added CoE abort codes in user space-
Florian Pose <fp@igh-essen.com>
parents:
1166
diff
changeset
|
240 |
} catch (MasterDeviceSdoAbortException &e) { |
75cd6681eb08
Introduced SdoCommand class to replace coe_datatypes.[ch]; added CoE abort codes in user space-
Florian Pose <fp@igh-essen.com>
parents:
1166
diff
changeset
|
241 |
delete [] data.data; |
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
242 |
err << "SDO transfer aborted with code 0x" |
1184
75cd6681eb08
Introduced SdoCommand class to replace coe_datatypes.[ch]; added CoE abort codes in user space-
Florian Pose <fp@igh-essen.com>
parents:
1166
diff
changeset
|
243 |
<< setfill('0') << hex << setw(8) << e.abortCode |
75cd6681eb08
Introduced SdoCommand class to replace coe_datatypes.[ch]; added CoE abort codes in user space-
Florian Pose <fp@igh-essen.com>
parents:
1166
diff
changeset
|
244 |
<< ": " << abortText(e.abortCode); |
75cd6681eb08
Introduced SdoCommand class to replace coe_datatypes.[ch]; added CoE abort codes in user space-
Florian Pose <fp@igh-essen.com>
parents:
1166
diff
changeset
|
245 |
throwCommandException(err); |
1142 | 246 |
} catch(MasterDeviceException &e) { |
247 |
delete [] data.data; |
|
248 |
throw e; |
|
249 |
} |
|
250 |
||
251 |
delete [] data.data; |
|
252 |
} |
|
253 |
||
254 |
/*****************************************************************************/ |