author | Florian Pose <fp@igh-essen.com> |
Tue, 06 Jul 2010 18:30:35 +0200 | |
changeset 1950 | 237b71828a9d |
parent 1870 | 0cee1fb7f0fd |
child 1968 | 4f682084c643 |
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" |
|
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
35 |
#include "MasterDevice.h" |
1142 | 36 |
|
37 |
/*****************************************************************************/ |
|
38 |
||
39 |
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
|
40 |
SdoCommand("upload", "Read an SDO entry from a slave.") |
1142 | 41 |
{ |
42 |
} |
|
43 |
||
44 |
/*****************************************************************************/ |
|
45 |
||
46 |
string CommandUpload::helpString() const |
|
47 |
{ |
|
48 |
stringstream str; |
|
49 |
||
50 |
str << getName() << " [OPTIONS] <INDEX> <SUBINDEX>" << endl |
|
51 |
<< endl |
|
52 |
<< getBriefDescription() << endl |
|
53 |
<< endl |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
54 |
<< "This command requires a single slave to be selected." << endl |
1804 | 55 |
<< endl |
1327
4d179b06dd3c
merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
56 |
<< "The data type of the SDO entry is taken from the SDO" << endl |
1142 | 57 |
<< "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
|
58 |
<< "--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
|
59 |
<< "information service or the SDO is not in the dictionary," << endl |
1142 | 60 |
<< "the --type option is mandatory." << endl |
61 |
<< endl |
|
1868
489ea0becd74
Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents:
1835
diff
changeset
|
62 |
<< typeInfo() |
1142 | 63 |
<< endl |
64 |
<< "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
|
65 |
<< " INDEX is the SDO index and must be an unsigned" << endl |
1142 | 66 |
<< " 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
|
67 |
<< " SUBINDEX is the SDO entry subindex and must be an" << endl |
1142 | 68 |
<< " unsigned 8 bit number." << endl |
69 |
<< endl |
|
70 |
<< "Command-specific options:" << endl |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
71 |
<< " --alias -a <alias>" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
72 |
<< " --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
|
73 |
<< " 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
|
74 |
<< " --type -t <type> SDO entry data type (see above)." << endl |
1142 | 75 |
<< endl |
76 |
<< numericInfo(); |
|
77 |
||
78 |
return str.str(); |
|
79 |
} |
|
80 |
||
81 |
/****************************************************************************/ |
|
82 |
||
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
83 |
void CommandUpload::execute(const StringVector &args) |
1142 | 84 |
{ |
1151 | 85 |
SlaveList slaves; |
1142 | 86 |
stringstream err, strIndex, strSubIndex; |
87 |
ec_ioctl_slave_sdo_upload_t data; |
|
1393
979a7b26036d
Output (fixed-size) hex value before decimal value.
Florian Pose <fp@igh-essen.com>
parents:
1363
diff
changeset
|
88 |
const DataType *dataType = NULL; |
1142 | 89 |
unsigned int uval; |
90 |
||
91 |
if (args.size() != 2) { |
|
92 |
err << "'" << getName() << "' takes two arguments!"; |
|
93 |
throwInvalidUsageException(err); |
|
94 |
} |
|
95 |
||
96 |
strIndex << args[0]; |
|
97 |
strIndex |
|
98 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
99 |
>> data.sdo_index; |
|
100 |
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
|
101 |
err << "Invalid SDO index '" << args[0] << "'!"; |
1142 | 102 |
throwInvalidUsageException(err); |
103 |
} |
|
104 |
||
105 |
strSubIndex << args[1]; |
|
106 |
strSubIndex |
|
107 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
108 |
>> uval; |
|
109 |
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
|
110 |
err << "Invalid SDO subindex '" << args[1] << "'!"; |
1142 | 111 |
throwInvalidUsageException(err); |
112 |
} |
|
113 |
data.sdo_entry_subindex = uval; |
|
114 |
||
1870
0cee1fb7f0fd
Added getSingleMasterIndex() for command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
1869
diff
changeset
|
115 |
MasterDevice m(getSingleMasterIndex()); |
1151 | 116 |
m.open(MasterDevice::Read); |
117 |
slaves = selectedSlaves(m); |
|
118 |
if (slaves.size() != 1) { |
|
1155
bd4e5b544473
Common message for single slave selections.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
119 |
throwSingleSlaveRequired(slaves.size()); |
1151 | 120 |
} |
121 |
data.slave_position = slaves.front().position; |
|
122 |
||
1166 | 123 |
if (!getDataType().empty()) { // data type specified |
124 |
if (!(dataType = findDataType(getDataType()))) { |
|
125 |
err << "Invalid data type '" << getDataType() << "'!"; |
|
1142 | 126 |
throwInvalidUsageException(err); |
127 |
} |
|
128 |
} else { // no data type specified: fetch from dictionary |
|
129 |
ec_ioctl_slave_sdo_entry_t entry; |
|
130 |
||
131 |
try { |
|
1151 | 132 |
m.getSdoEntry(&entry, data.slave_position, |
1142 | 133 |
data.sdo_index, data.sdo_entry_subindex); |
134 |
} 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
|
135 |
err << "Failed to determine SDO entry data type. " |
1142 | 136 |
<< "Please specify --type."; |
137 |
throwCommandException(err); |
|
138 |
} |
|
139 |
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
|
140 |
err << "PDO entry has unknown data type 0x" |
1142 | 141 |
<< hex << setfill('0') << setw(4) << entry.data_type << "!" |
142 |
<< " Please specify --type."; |
|
143 |
throwCommandException(err); |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
if (dataType->byteSize) { |
|
148 |
data.target_size = dataType->byteSize; |
|
149 |
} else { |
|
150 |
data.target_size = DefaultBufferSize; |
|
151 |
} |
|
152 |
||
153 |
data.target = new uint8_t[data.target_size + 1]; |
|
154 |
||
1804 | 155 |
try { |
156 |
m.sdoUpload(&data); |
|
157 |
} catch (MasterDeviceSdoAbortException &e) { |
|
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 |
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
|
159 |
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
|
160 |
<< 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
|
161 |
<< ": " << 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
|
162 |
throwCommandException(err); |
1804 | 163 |
} catch (MasterDeviceException &e) { |
1142 | 164 |
delete [] data.target; |
165 |
throw e; |
|
166 |
} |
|
167 |
||
168 |
m.close(); |
|
169 |
||
1835
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
170 |
try { |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
171 |
outputData(cout, dataType, data.target, data.data_size); |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
172 |
} catch (SizeException &e) { |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
173 |
delete [] data.target; |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
174 |
throwCommandException(e.what()); |
1142 | 175 |
} |
176 |
||
177 |
delete [] data.target; |
|
178 |
} |
|
179 |
||
180 |
/*****************************************************************************/ |