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