author | Florian Pose <fp@igh-essen.com> |
Mon, 26 Jan 2009 16:35:50 +0000 | |
changeset 1344 | e5756d4ed12c |
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 "CommandUpload.h" |
|
12 |
||
13 |
/*****************************************************************************/ |
|
14 |
||
15 |
CommandUpload::CommandUpload(): |
|
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("upload", "Read an SDO entry from a slave.") |
1142 | 17 |
{ |
18 |
} |
|
19 |
||
20 |
/*****************************************************************************/ |
|
21 |
||
22 |
string CommandUpload::helpString() const |
|
23 |
{ |
|
24 |
stringstream str; |
|
25 |
||
26 |
str << getName() << " [OPTIONS] <INDEX> <SUBINDEX>" << endl |
|
27 |
<< endl |
|
28 |
<< getBriefDescription() << endl |
|
29 |
<< endl |
|
1157
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 |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
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 |
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 |
<< endl |
|
48 |
<< "Command-specific options:" << endl |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
49 |
<< " --alias -a <alias>" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
50 |
<< " --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
|
51 |
<< " 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
|
52 |
<< " --type -t <type> SDO entry data type (see above)." << endl |
1142 | 53 |
<< endl |
54 |
<< numericInfo(); |
|
55 |
||
56 |
return str.str(); |
|
57 |
} |
|
58 |
||
59 |
/****************************************************************************/ |
|
60 |
||
61 |
void CommandUpload::execute(MasterDevice &m, const StringVector &args) |
|
62 |
{ |
|
1151 | 63 |
SlaveList slaves; |
1142 | 64 |
stringstream err, strIndex, strSubIndex; |
65 |
int sval; |
|
66 |
ec_ioctl_slave_sdo_upload_t data; |
|
67 |
unsigned int uval; |
|
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; |
1142 | 69 |
|
70 |
if (args.size() != 2) { |
|
71 |
err << "'" << getName() << "' takes two arguments!"; |
|
72 |
throwInvalidUsageException(err); |
|
73 |
} |
|
74 |
||
75 |
strIndex << args[0]; |
|
76 |
strIndex |
|
77 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
78 |
>> data.sdo_index; |
|
79 |
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
|
80 |
err << "Invalid SDO index '" << args[0] << "'!"; |
1142 | 81 |
throwInvalidUsageException(err); |
82 |
} |
|
83 |
||
84 |
strSubIndex << args[1]; |
|
85 |
strSubIndex |
|
86 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
87 |
>> uval; |
|
88 |
if (strSubIndex.fail() || uval > 0xff) { |
|
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
89 |
err << "Invalid SDO subindex '" << args[1] << "'!"; |
1142 | 90 |
throwInvalidUsageException(err); |
91 |
} |
|
92 |
data.sdo_entry_subindex = uval; |
|
93 |
||
1151 | 94 |
m.open(MasterDevice::Read); |
95 |
slaves = selectedSlaves(m); |
|
96 |
if (slaves.size() != 1) { |
|
1155
bd4e5b544473
Common message for single slave selections.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
97 |
throwSingleSlaveRequired(slaves.size()); |
1151 | 98 |
} |
99 |
data.slave_position = slaves.front().position; |
|
100 |
||
1166 | 101 |
if (!getDataType().empty()) { // data type specified |
102 |
if (!(dataType = findDataType(getDataType()))) { |
|
103 |
err << "Invalid data type '" << getDataType() << "'!"; |
|
1142 | 104 |
throwInvalidUsageException(err); |
105 |
} |
|
106 |
} else { // no data type specified: fetch from dictionary |
|
107 |
ec_ioctl_slave_sdo_entry_t entry; |
|
108 |
||
109 |
try { |
|
1151 | 110 |
m.getSdoEntry(&entry, data.slave_position, |
1142 | 111 |
data.sdo_index, data.sdo_entry_subindex); |
112 |
} 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
|
113 |
err << "Failed to determine SDO entry data type. " |
1142 | 114 |
<< "Please specify --type."; |
115 |
throwCommandException(err); |
|
116 |
} |
|
117 |
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
|
118 |
err << "PDO entry has unknown data type 0x" |
1142 | 119 |
<< hex << setfill('0') << setw(4) << entry.data_type << "!" |
120 |
<< " Please specify --type."; |
|
121 |
throwCommandException(err); |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
if (dataType->byteSize) { |
|
126 |
data.target_size = dataType->byteSize; |
|
127 |
} else { |
|
128 |
data.target_size = DefaultBufferSize; |
|
129 |
} |
|
130 |
||
131 |
data.target = new uint8_t[data.target_size + 1]; |
|
132 |
||
133 |
try { |
|
134 |
m.sdoUpload(&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
|
135 |
} 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
|
136 |
delete [] data.target; |
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 << "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
|
138 |
<< 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
|
139 |
<< ": " << 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
|
140 |
throwCommandException(err); |
1142 | 141 |
} catch (MasterDeviceException &e) { |
142 |
delete [] data.target; |
|
143 |
throw e; |
|
144 |
} |
|
145 |
||
146 |
m.close(); |
|
147 |
||
148 |
if (dataType->byteSize && data.data_size != dataType->byteSize) { |
|
149 |
err << "Data type mismatch. Expected " << dataType->name |
|
150 |
<< " with " << dataType->byteSize << " byte, but got " |
|
151 |
<< data.data_size << " byte."; |
|
152 |
throwCommandException(err); |
|
153 |
} |
|
154 |
||
155 |
cout << setfill('0'); |
|
156 |
switch (dataType->coeCode) { |
|
157 |
case 0x0002: // int8 |
|
158 |
sval = *(int8_t *) data.target; |
|
159 |
cout << sval << " 0x" << hex << setw(2) << sval << endl; |
|
160 |
break; |
|
161 |
case 0x0003: // int16 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
162 |
sval = le16_to_cpup(data.target); |
1142 | 163 |
cout << sval << " 0x" << hex << setw(4) << sval << endl; |
164 |
break; |
|
165 |
case 0x0004: // int32 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
166 |
sval = le32_to_cpup(data.target); |
1142 | 167 |
cout << sval << " 0x" << hex << setw(8) << sval << endl; |
168 |
break; |
|
169 |
case 0x0005: // uint8 |
|
170 |
uval = (unsigned int) *(uint8_t *) data.target; |
|
171 |
cout << uval << " 0x" << hex << setw(2) << uval << endl; |
|
172 |
break; |
|
173 |
case 0x0006: // uint16 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
174 |
uval = le16_to_cpup(data.target); |
1142 | 175 |
cout << uval << " 0x" << hex << setw(4) << uval << endl; |
176 |
break; |
|
177 |
case 0x0007: // uint32 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
178 |
uval = le32_to_cpup(data.target); |
1142 | 179 |
cout << uval << " 0x" << hex << setw(8) << uval << endl; |
180 |
break; |
|
181 |
case 0x0009: // string |
|
182 |
cout << string((const char *) data.target, data.data_size) |
|
183 |
<< endl; |
|
184 |
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
|
185 |
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
|
186 |
cout << string((const char *) data.target, 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
|
187 |
<< 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
|
188 |
break; |
1142 | 189 |
default: |
190 |
printRawData(data.target, data.data_size); // FIXME |
|
191 |
break; |
|
192 |
} |
|
193 |
||
194 |
delete [] data.target; |
|
195 |
} |
|
196 |
||
197 |
/****************************************************************************/ |
|
198 |
||
199 |
void CommandUpload::printRawData( |
|
200 |
const uint8_t *data, |
|
201 |
unsigned int size |
|
202 |
) |
|
203 |
{ |
|
204 |
cout << hex << setfill('0'); |
|
205 |
while (size--) { |
|
206 |
cout << "0x" << setw(2) << (unsigned int) *data++; |
|
207 |
if (size) |
|
208 |
cout << " "; |
|
209 |
} |
|
210 |
cout << endl; |
|
211 |
} |
|
212 |
||
213 |
/*****************************************************************************/ |