author | Florian Pose <fp@igh-essen.com> |
Wed, 08 Apr 2009 08:13:33 +0000 | |
changeset 1382 | cb2188d111f3 |
parent 1363 | 11c0b2caa253 |
child 1393 | 979a7b26036d |
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 "CommandUpload.h" |
|
35 |
||
36 |
/*****************************************************************************/ |
|
37 |
||
38 |
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
|
39 |
SdoCommand("upload", "Read an SDO entry from a slave.") |
1142 | 40 |
{ |
41 |
} |
|
42 |
||
43 |
/*****************************************************************************/ |
|
44 |
||
45 |
string CommandUpload::helpString() const |
|
46 |
{ |
|
47 |
stringstream str; |
|
48 |
||
49 |
str << getName() << " [OPTIONS] <INDEX> <SUBINDEX>" << endl |
|
50 |
<< endl |
|
51 |
<< getBriefDescription() << endl |
|
52 |
<< endl |
|
1157
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 |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
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 |
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 |
<< endl |
|
71 |
<< "Command-specific options:" << endl |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
72 |
<< " --alias -a <alias>" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
73 |
<< " --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
|
74 |
<< " 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
|
75 |
<< " --type -t <type> SDO entry data type (see above)." << endl |
1142 | 76 |
<< endl |
77 |
<< numericInfo(); |
|
78 |
||
79 |
return str.str(); |
|
80 |
} |
|
81 |
||
82 |
/****************************************************************************/ |
|
83 |
||
84 |
void CommandUpload::execute(MasterDevice &m, const StringVector &args) |
|
85 |
{ |
|
1151 | 86 |
SlaveList slaves; |
1142 | 87 |
stringstream err, strIndex, strSubIndex; |
88 |
int sval; |
|
89 |
ec_ioctl_slave_sdo_upload_t data; |
|
90 |
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
|
91 |
const DataType *dataType = NULL; |
1142 | 92 |
|
93 |
if (args.size() != 2) { |
|
94 |
err << "'" << getName() << "' takes two arguments!"; |
|
95 |
throwInvalidUsageException(err); |
|
96 |
} |
|
97 |
||
98 |
strIndex << args[0]; |
|
99 |
strIndex |
|
100 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
101 |
>> data.sdo_index; |
|
102 |
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
|
103 |
err << "Invalid SDO index '" << args[0] << "'!"; |
1142 | 104 |
throwInvalidUsageException(err); |
105 |
} |
|
106 |
||
107 |
strSubIndex << args[1]; |
|
108 |
strSubIndex |
|
109 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
110 |
>> uval; |
|
111 |
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
|
112 |
err << "Invalid SDO subindex '" << args[1] << "'!"; |
1142 | 113 |
throwInvalidUsageException(err); |
114 |
} |
|
115 |
data.sdo_entry_subindex = uval; |
|
116 |
||
1151 | 117 |
m.open(MasterDevice::Read); |
118 |
slaves = selectedSlaves(m); |
|
119 |
if (slaves.size() != 1) { |
|
1155
bd4e5b544473
Common message for single slave selections.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
120 |
throwSingleSlaveRequired(slaves.size()); |
1151 | 121 |
} |
122 |
data.slave_position = slaves.front().position; |
|
123 |
||
1166 | 124 |
if (!getDataType().empty()) { // data type specified |
125 |
if (!(dataType = findDataType(getDataType()))) { |
|
126 |
err << "Invalid data type '" << getDataType() << "'!"; |
|
1142 | 127 |
throwInvalidUsageException(err); |
128 |
} |
|
129 |
} else { // no data type specified: fetch from dictionary |
|
130 |
ec_ioctl_slave_sdo_entry_t entry; |
|
131 |
||
132 |
try { |
|
1151 | 133 |
m.getSdoEntry(&entry, data.slave_position, |
1142 | 134 |
data.sdo_index, data.sdo_entry_subindex); |
135 |
} 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
|
136 |
err << "Failed to determine SDO entry data type. " |
1142 | 137 |
<< "Please specify --type."; |
138 |
throwCommandException(err); |
|
139 |
} |
|
140 |
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
|
141 |
err << "PDO entry has unknown data type 0x" |
1142 | 142 |
<< hex << setfill('0') << setw(4) << entry.data_type << "!" |
143 |
<< " Please specify --type."; |
|
144 |
throwCommandException(err); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
if (dataType->byteSize) { |
|
149 |
data.target_size = dataType->byteSize; |
|
150 |
} else { |
|
151 |
data.target_size = DefaultBufferSize; |
|
152 |
} |
|
153 |
||
154 |
data.target = new uint8_t[data.target_size + 1]; |
|
155 |
||
156 |
try { |
|
157 |
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
|
158 |
} 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
|
159 |
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
|
160 |
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
|
161 |
<< 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
|
162 |
<< ": " << 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
|
163 |
throwCommandException(err); |
1142 | 164 |
} catch (MasterDeviceException &e) { |
165 |
delete [] data.target; |
|
166 |
throw e; |
|
167 |
} |
|
168 |
||
169 |
m.close(); |
|
170 |
||
171 |
if (dataType->byteSize && data.data_size != dataType->byteSize) { |
|
172 |
err << "Data type mismatch. Expected " << dataType->name |
|
173 |
<< " with " << dataType->byteSize << " byte, but got " |
|
174 |
<< data.data_size << " byte."; |
|
175 |
throwCommandException(err); |
|
176 |
} |
|
177 |
||
178 |
cout << setfill('0'); |
|
179 |
switch (dataType->coeCode) { |
|
180 |
case 0x0002: // int8 |
|
181 |
sval = *(int8_t *) data.target; |
|
182 |
cout << sval << " 0x" << hex << setw(2) << sval << endl; |
|
183 |
break; |
|
184 |
case 0x0003: // int16 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
185 |
sval = le16_to_cpup(data.target); |
1142 | 186 |
cout << sval << " 0x" << hex << setw(4) << sval << endl; |
187 |
break; |
|
188 |
case 0x0004: // int32 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
189 |
sval = le32_to_cpup(data.target); |
1142 | 190 |
cout << sval << " 0x" << hex << setw(8) << sval << endl; |
191 |
break; |
|
192 |
case 0x0005: // uint8 |
|
193 |
uval = (unsigned int) *(uint8_t *) data.target; |
|
194 |
cout << uval << " 0x" << hex << setw(2) << uval << endl; |
|
195 |
break; |
|
196 |
case 0x0006: // uint16 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
197 |
uval = le16_to_cpup(data.target); |
1142 | 198 |
cout << uval << " 0x" << hex << setw(4) << uval << endl; |
199 |
break; |
|
200 |
case 0x0007: // uint32 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
201 |
uval = le32_to_cpup(data.target); |
1142 | 202 |
cout << uval << " 0x" << hex << setw(8) << uval << endl; |
203 |
break; |
|
204 |
case 0x0009: // string |
|
205 |
cout << string((const char *) data.target, data.data_size) |
|
206 |
<< endl; |
|
207 |
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
|
208 |
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
|
209 |
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
|
210 |
<< 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
|
211 |
break; |
1142 | 212 |
default: |
213 |
printRawData(data.target, data.data_size); // FIXME |
|
214 |
break; |
|
215 |
} |
|
216 |
||
217 |
delete [] data.target; |
|
218 |
} |
|
219 |
||
220 |
/****************************************************************************/ |
|
221 |
||
222 |
void CommandUpload::printRawData( |
|
223 |
const uint8_t *data, |
|
224 |
unsigned int size |
|
225 |
) |
|
226 |
{ |
|
227 |
cout << hex << setfill('0'); |
|
228 |
while (size--) { |
|
229 |
cout << "0x" << setw(2) << (unsigned int) *data++; |
|
230 |
if (size) |
|
231 |
cout << " "; |
|
232 |
} |
|
233 |
cout << endl; |
|
234 |
} |
|
235 |
||
236 |
/*****************************************************************************/ |