author | Florian Pose <fp@igh-essen.com> |
Fri, 19 Jun 2009 11:46:53 +0000 | |
branch | stable-1.4 |
changeset 1699 | 89ba41136b21 |
parent 1695 | 69a948e240df |
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 |
#include "byteorder.h" |
|
13 |
||
14 |
/*****************************************************************************/ |
|
15 |
||
16 |
CommandUpload::CommandUpload(): |
|
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
17 |
SdoCommand("upload", "Read an SDO entry from a slave.") |
1142 | 18 |
{ |
19 |
} |
|
20 |
||
21 |
/*****************************************************************************/ |
|
22 |
||
23 |
string CommandUpload::helpString() const |
|
24 |
{ |
|
25 |
stringstream str; |
|
26 |
||
27 |
str << getName() << " [OPTIONS] <INDEX> <SUBINDEX>" << endl |
|
28 |
<< endl |
|
29 |
<< getBriefDescription() << endl |
|
30 |
<< endl |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
31 |
<< "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
|
32 |
<< endl |
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
33 |
<< "The data type of the SDO entry is taken from the SDO" << endl |
1142 | 34 |
<< "dictionary by default. It can be overridden with the" << endl |
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
35 |
<< "--type option. If the slave does not support the SDO" << endl |
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
36 |
<< "information service or the SDO is not in the dictionary," << endl |
1142 | 37 |
<< "the --type option is mandatory." << endl |
38 |
<< endl |
|
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
39 |
<< "These are the valid SDO entry data types:" << endl |
1695
69a948e240df
Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1686
diff
changeset
|
40 |
<< " int8, int16, int32, uint8, uint16, uint32, string," << endl |
69a948e240df
Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1686
diff
changeset
|
41 |
<< " octet_string." << endl |
1142 | 42 |
<< endl |
43 |
<< "Arguments:" << endl |
|
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
44 |
<< " INDEX is the SDO index and must be an unsigned" << endl |
1142 | 45 |
<< " 16 bit number." << endl |
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
46 |
<< " SUBINDEX is the SDO entry subindex and must be an" << endl |
1142 | 47 |
<< " unsigned 8 bit number." << endl |
48 |
<< endl |
|
49 |
<< "Command-specific options:" << endl |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
50 |
<< " --alias -a <alias>" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
51 |
<< " --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
|
52 |
<< " the 'slaves' command." << endl |
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
53 |
<< " --type -t <type> SDO entry data type (see above)." << endl |
1142 | 54 |
<< endl |
55 |
<< numericInfo(); |
|
56 |
||
57 |
return str.str(); |
|
58 |
} |
|
59 |
||
60 |
/****************************************************************************/ |
|
61 |
||
62 |
void CommandUpload::execute(MasterDevice &m, const StringVector &args) |
|
63 |
{ |
|
1151 | 64 |
SlaveList slaves; |
1142 | 65 |
stringstream err, strIndex, strSubIndex; |
66 |
int sval; |
|
67 |
ec_ioctl_slave_sdo_upload_t data; |
|
68 |
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
|
69 |
const DataType *dataType = NULL; |
1142 | 70 |
|
71 |
if (args.size() != 2) { |
|
72 |
err << "'" << getName() << "' takes two 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()) { |
|
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
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 |
>> uval; |
|
89 |
if (strSubIndex.fail() || uval > 0xff) { |
|
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
90 |
err << "Invalid SDO subindex '" << args[1] << "'!"; |
1142 | 91 |
throwInvalidUsageException(err); |
92 |
} |
|
93 |
data.sdo_entry_subindex = uval; |
|
94 |
||
1151 | 95 |
m.open(MasterDevice::Read); |
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) { |
|
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
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))) { |
|
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
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.target_size = dataType->byteSize; |
|
128 |
} else { |
|
129 |
data.target_size = DefaultBufferSize; |
|
130 |
} |
|
131 |
||
132 |
data.target = new uint8_t[data.target_size + 1]; |
|
133 |
||
134 |
try { |
|
135 |
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
|
136 |
} 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
|
137 |
delete [] data.target; |
1686
e206f4485f60
Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
138 |
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
|
139 |
<< 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
|
140 |
<< ": " << 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
|
141 |
throwCommandException(err); |
1142 | 142 |
} catch (MasterDeviceException &e) { |
143 |
delete [] data.target; |
|
144 |
throw e; |
|
145 |
} |
|
146 |
||
147 |
m.close(); |
|
148 |
||
149 |
if (dataType->byteSize && data.data_size != dataType->byteSize) { |
|
150 |
err << "Data type mismatch. Expected " << dataType->name |
|
151 |
<< " with " << dataType->byteSize << " byte, but got " |
|
152 |
<< data.data_size << " byte."; |
|
153 |
throwCommandException(err); |
|
154 |
} |
|
155 |
||
156 |
cout << setfill('0'); |
|
157 |
switch (dataType->coeCode) { |
|
158 |
case 0x0002: // int8 |
|
159 |
sval = *(int8_t *) data.target; |
|
160 |
cout << sval << " 0x" << hex << setw(2) << sval << endl; |
|
161 |
break; |
|
162 |
case 0x0003: // int16 |
|
163 |
sval = le16tocpu(*(int16_t *) data.target); |
|
164 |
cout << sval << " 0x" << hex << setw(4) << sval << endl; |
|
165 |
break; |
|
166 |
case 0x0004: // int32 |
|
167 |
sval = le32tocpu(*(int32_t *) data.target); |
|
168 |
cout << sval << " 0x" << hex << setw(8) << sval << endl; |
|
169 |
break; |
|
170 |
case 0x0005: // uint8 |
|
171 |
uval = (unsigned int) *(uint8_t *) data.target; |
|
172 |
cout << uval << " 0x" << hex << setw(2) << uval << endl; |
|
173 |
break; |
|
174 |
case 0x0006: // uint16 |
|
175 |
uval = le16tocpu(*(uint16_t *) data.target); |
|
176 |
cout << uval << " 0x" << hex << setw(4) << uval << endl; |
|
177 |
break; |
|
178 |
case 0x0007: // uint32 |
|
179 |
uval = le32tocpu(*(uint32_t *) data.target); |
|
180 |
cout << uval << " 0x" << hex << setw(8) << uval << endl; |
|
181 |
break; |
|
182 |
case 0x0009: // string |
|
183 |
cout << string((const char *) data.target, data.data_size) |
|
184 |
<< endl; |
|
185 |
break; |
|
1695
69a948e240df
Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1686
diff
changeset
|
186 |
case 0x000a: // octet_string |
69a948e240df
Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1686
diff
changeset
|
187 |
cout << string((const char *) data.target, data.data_size) |
69a948e240df
Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1686
diff
changeset
|
188 |
<< endl; |
69a948e240df
Added octet_string as an SDO data type.
Florian Pose <fp@igh-essen.com>
parents:
1686
diff
changeset
|
189 |
break; |
1142 | 190 |
default: |
191 |
printRawData(data.target, data.data_size); // FIXME |
|
192 |
break; |
|
193 |
} |
|
194 |
||
195 |
delete [] data.target; |
|
196 |
} |
|
197 |
||
198 |
/****************************************************************************/ |
|
199 |
||
200 |
void CommandUpload::printRawData( |
|
201 |
const uint8_t *data, |
|
202 |
unsigned int size |
|
203 |
) |
|
204 |
{ |
|
205 |
cout << hex << setfill('0'); |
|
206 |
while (size--) { |
|
207 |
cout << "0x" << setw(2) << (unsigned int) *data++; |
|
208 |
if (size) |
|
209 |
cout << " "; |
|
210 |
} |
|
211 |
cout << endl; |
|
212 |
} |
|
213 |
||
214 |
/*****************************************************************************/ |