author | Florian Pose <fp@igh-essen.com> |
Fri, 05 Mar 2010 17:48:50 +0100 | |
changeset 1842 | 6d3190b9d464 |
parent 1835 | 20748e9d2238 |
child 1868 | 489ea0becd74 |
permissions | -rw-r--r-- |
1200 | 1 |
/***************************************************************************** |
2 |
* |
|
1363
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
diff
changeset
|
3 |
* $Id$ |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
diff
changeset
|
4 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
diff
changeset
|
5 |
* Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
diff
changeset
|
6 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
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:
1200
diff
changeset
|
8 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
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:
1200
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:
1200
diff
changeset
|
11 |
* published by the Free Software Foundation. |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
diff
changeset
|
12 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
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:
1200
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:
1200
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:
1200
diff
changeset
|
16 |
* Public License for more details. |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
diff
changeset
|
17 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
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:
1200
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:
1200
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:
1200
diff
changeset
|
21 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
diff
changeset
|
22 |
* --- |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
diff
changeset
|
23 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1200
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:
1200
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:
1200
diff
changeset
|
26 |
* industrial property and similar rights of Beckhoff Automation GmbH. |
1200 | 27 |
* |
28 |
****************************************************************************/ |
|
29 |
||
30 |
#include <iostream> |
|
31 |
#include <iomanip> |
|
32 |
using namespace std; |
|
33 |
||
1388
3c886ec376f5
Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
1387
diff
changeset
|
34 |
#include "CommandRegRead.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" |
1200 | 36 |
|
37 |
/*****************************************************************************/ |
|
38 |
||
1388
3c886ec376f5
Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
1387
diff
changeset
|
39 |
CommandRegRead::CommandRegRead(): |
1835
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
40 |
Command("reg_read", "Output a slave's register contents.") |
1200 | 41 |
{ |
42 |
} |
|
43 |
||
44 |
/*****************************************************************************/ |
|
45 |
||
1388
3c886ec376f5
Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
1387
diff
changeset
|
46 |
string CommandRegRead::helpString() const |
1200 | 47 |
{ |
48 |
stringstream str; |
|
49 |
||
1387 | 50 |
str << getName() << " [OPTIONS] <OFFSET> [LENGTH]" << endl |
1804 | 51 |
<< endl |
52 |
<< getBriefDescription() << endl |
|
53 |
<< endl |
|
1200 | 54 |
<< "This command requires a single slave to be selected." << endl |
1804 | 55 |
<< endl |
1200 | 56 |
<< "Arguments:" << endl |
1388
3c886ec376f5
Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
1387
diff
changeset
|
57 |
<< " OFFSET is the register address. Must" << endl |
1200 | 58 |
<< " be an unsigned 16 bit number." << endl |
59 |
<< " LENGTH is the number of bytes to read and must also be" << endl |
|
60 |
<< " an unsigned 16 bit number. OFFSET plus LENGTH" << endl |
|
1387 | 61 |
<< " may not exceed 64k. The length is ignored (and" << endl |
62 |
<< " can be omitted), if a selected data type" << endl |
|
63 |
<< " implies a length." << endl |
|
64 |
<< endl |
|
65 |
<< "These are the valid data types:" << endl |
|
66 |
<< " int8, int16, int32, int64, uint8, uint16, uint32," << endl |
|
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
67 |
<< " uint64, string, raw." << endl |
1200 | 68 |
<< endl |
1804 | 69 |
<< "Command-specific options:" << endl |
1200 | 70 |
<< " --alias -a <alias>" << endl |
71 |
<< " --position -p <pos> Slave selection. See the help of" << endl |
|
72 |
<< " the 'slaves' command." << endl |
|
1387 | 73 |
<< " --type -t <type> Data type (see above)." << endl |
1804 | 74 |
<< endl |
75 |
<< numericInfo(); |
|
76 |
||
77 |
return str.str(); |
|
1200 | 78 |
} |
79 |
||
80 |
/****************************************************************************/ |
|
81 |
||
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
82 |
void CommandRegRead::execute(const StringVector &args) |
1200 | 83 |
{ |
84 |
SlaveList slaves; |
|
1388
3c886ec376f5
Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
1387
diff
changeset
|
85 |
ec_ioctl_slave_reg_t data; |
1387 | 86 |
stringstream strOffset, err; |
87 |
const DataType *dataType = NULL; |
|
88 |
||
89 |
if (args.size() < 1 || args.size() > 2) { |
|
90 |
err << "'" << getName() << "' takes one or two arguments!"; |
|
1200 | 91 |
throwInvalidUsageException(err); |
92 |
} |
|
93 |
||
94 |
strOffset << args[0]; |
|
95 |
strOffset |
|
96 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
97 |
>> data.offset; |
|
98 |
if (strOffset.fail()) { |
|
99 |
err << "Invalid offset '" << args[0] << "'!"; |
|
100 |
throwInvalidUsageException(err); |
|
101 |
} |
|
102 |
||
1387 | 103 |
if (args.size() > 1) { |
104 |
stringstream strLength; |
|
105 |
strLength << args[1]; |
|
106 |
strLength |
|
107 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
108 |
>> data.length; |
|
109 |
if (strLength.fail()) { |
|
110 |
err << "Invalid length '" << args[1] << "'!"; |
|
111 |
throwInvalidUsageException(err); |
|
112 |
} |
|
113 |
||
114 |
if (!data.length) { |
|
115 |
err << "Length may not be zero!"; |
|
116 |
throwInvalidUsageException(err); |
|
117 |
} |
|
118 |
} else { // no length argument given |
|
119 |
data.length = 0; |
|
120 |
} |
|
121 |
||
122 |
if (!getDataType().empty()) { |
|
123 |
if (!(dataType = findDataType(getDataType()))) { |
|
124 |
err << "Invalid data type '" << getDataType() << "'!"; |
|
125 |
throwInvalidUsageException(err); |
|
126 |
} |
|
127 |
||
128 |
if (dataType->byteSize) { |
|
129 |
// override length argument |
|
130 |
data.length = dataType->byteSize; |
|
131 |
} |
|
1200 | 132 |
} |
133 |
||
134 |
if (!data.length) { |
|
1387 | 135 |
err << "The length argument is mandatory, if no datatype is " << endl |
136 |
<< "specified, or the datatype does not imply a length!"; |
|
137 |
throwInvalidUsageException(err); |
|
1200 | 138 |
} |
139 |
||
140 |
if ((uint32_t) data.offset + data.length > 0xffff) { |
|
141 |
err << "Offset and length exceeding 64k!"; |
|
142 |
throwInvalidUsageException(err); |
|
143 |
} |
|
144 |
||
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
145 |
if (getMasterIndices().size() != 1) { |
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
146 |
err << getName() << " requires to select a single master!"; |
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
147 |
throwInvalidUsageException(err); |
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
148 |
} |
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
149 |
MasterDevice m(getMasterIndices().front()); |
1200 | 150 |
m.open(MasterDevice::Read); |
151 |
slaves = selectedSlaves(m); |
|
152 |
||
153 |
if (slaves.size() != 1) { |
|
154 |
throwSingleSlaveRequired(slaves.size()); |
|
155 |
} |
|
156 |
data.slave_position = slaves.front().position; |
|
157 |
||
158 |
data.data = new uint8_t[data.length]; |
|
159 |
||
1804 | 160 |
try { |
161 |
m.readReg(&data); |
|
162 |
} catch (MasterDeviceException &e) { |
|
1200 | 163 |
delete [] data.data; |
1804 | 164 |
throw e; |
165 |
} |
|
1200 | 166 |
|
1835
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
167 |
try { |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
168 |
outputData(cout, dataType, data.data, data.length); |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
169 |
} catch (SizeException &e) { |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
170 |
delete [] data.data; |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
171 |
throwCommandException(e.what()); |
1200 | 172 |
} |
173 |
||
174 |
delete [] data.data; |
|
175 |
} |
|
176 |
||
177 |
/*****************************************************************************/ |