author | Florian Pose <fp@igh-essen.com> |
Tue, 28 Oct 2014 16:31:44 +0100 | |
branch | stable-1.5 |
changeset 2587 | afd76ee3aa87 |
parent 2529 | c7e1f2616a9d |
permissions | -rw-r--r-- |
1388 | 1 |
/***************************************************************************** |
2 |
* |
|
3 |
* $Id$ |
|
4 |
* |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
5 |
* Copyright (C) 2006-2012 Florian Pose, Ingenieurgemeinschaft IgH |
1388 | 6 |
* |
7 |
* This file is part of the IgH EtherCAT Master. |
|
8 |
* |
|
9 |
* The IgH EtherCAT Master is free software; you can redistribute it and/or |
|
10 |
* modify it under the terms of the GNU General Public License version 2, as |
|
11 |
* published by the Free Software Foundation. |
|
12 |
* |
|
13 |
* The IgH EtherCAT Master is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
16 |
* Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License along |
|
19 |
* with the IgH EtherCAT Master; if not, write to the Free Software |
|
20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
21 |
* |
|
22 |
* --- |
|
23 |
* |
|
24 |
* The license mentioned above concerns the source code only. Using the |
|
25 |
* EtherCAT technology and brand is only permitted in compliance with the |
|
26 |
* industrial property and similar rights of Beckhoff Automation GmbH. |
|
27 |
* |
|
28 |
****************************************************************************/ |
|
29 |
||
30 |
#include <iostream> |
|
31 |
#include <iomanip> |
|
32 |
#include <fstream> |
|
33 |
using namespace std; |
|
34 |
||
35 |
#include "CommandRegWrite.h" |
|
36 |
#include "sii_crc.h" |
|
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
37 |
#include "MasterDevice.h" |
1388 | 38 |
|
39 |
/*****************************************************************************/ |
|
40 |
||
41 |
CommandRegWrite::CommandRegWrite(): |
|
1835
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
42 |
Command("reg_write", "Write data to a slave's registers.") |
1388 | 43 |
{ |
44 |
} |
|
45 |
||
46 |
/*****************************************************************************/ |
|
47 |
||
1968
4f682084c643
Implemented drive_no for command-line tool; binary base name is now a
Florian Pose <fp@igh-essen.com>
parents:
1870
diff
changeset
|
48 |
string CommandRegWrite::helpString(const string &binaryBaseName) const |
1388 | 49 |
{ |
50 |
stringstream str; |
|
51 |
||
1968
4f682084c643
Implemented drive_no for command-line tool; binary base name is now a
Florian Pose <fp@igh-essen.com>
parents:
1870
diff
changeset
|
52 |
str << binaryBaseName << " " << getName() |
4f682084c643
Implemented drive_no for command-line tool; binary base name is now a
Florian Pose <fp@igh-essen.com>
parents:
1870
diff
changeset
|
53 |
<< " [OPTIONS] <OFFSET> <DATA>" << endl |
2421 | 54 |
<< endl |
1388 | 55 |
<< getBriefDescription() << endl |
56 |
<< endl |
|
57 |
<< "This command requires a single slave to be selected." << endl |
|
1804 | 58 |
<< endl |
1388 | 59 |
<< "Arguments:" << endl |
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
60 |
<< " ADDRESS is the register address to write to." << endl |
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
61 |
<< " DATA depends on whether a datatype was specified" << endl |
1804 | 62 |
<< " with the --type option: If not, DATA must be" << endl |
63 |
<< " either a path to a file with data to write," << endl |
|
64 |
<< " or '-', which means, that data are read from" << endl |
|
65 |
<< " stdin. If a datatype was specified, VALUE is" << endl |
|
66 |
<< " interpreted respective to the given type." << endl |
|
1388 | 67 |
<< 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
|
68 |
<< typeInfo() |
1804 | 69 |
<< endl |
1388 | 70 |
<< "Command-specific options:" << endl |
2529
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
71 |
<< " --alias -a <alias>" << endl |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
72 |
<< " --position -p <pos> Slave selection. See the help of" |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
73 |
<< endl |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
74 |
<< " the 'slaves' command." << endl |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
75 |
<< " --type -t <type> Data type (see above)." << endl |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
76 |
<< " --emergency -e Send as emergency request." << endl |
1388 | 77 |
<< endl |
78 |
<< numericInfo(); |
|
79 |
||
80 |
return str.str(); |
|
81 |
} |
|
82 |
||
83 |
/****************************************************************************/ |
|
84 |
||
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
85 |
void CommandRegWrite::execute(const StringVector &args) |
1388 | 86 |
{ |
87 |
stringstream strOffset, err; |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
88 |
ec_ioctl_slave_reg_t io; |
1388 | 89 |
ifstream file; |
90 |
||
91 |
if (args.size() != 2) { |
|
92 |
err << "'" << getName() << "' takes exactly two arguments!"; |
|
93 |
throwInvalidUsageException(err); |
|
94 |
} |
|
2421 | 95 |
|
1388 | 96 |
strOffset << args[0]; |
97 |
strOffset |
|
98 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
99 |
>> io.address; |
1388 | 100 |
if (strOffset.fail()) { |
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
101 |
err << "Invalid address '" << args[0] << "'!"; |
1388 | 102 |
throwInvalidUsageException(err); |
103 |
} |
|
2421 | 104 |
|
1804 | 105 |
if (getDataType().empty()) { |
106 |
if (args[1] == "-") { |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
107 |
loadRegData(&io, cin); |
1804 | 108 |
} else { |
109 |
file.open(args[1].c_str(), ifstream::in | ifstream::binary); |
|
110 |
if (file.fail()) { |
|
111 |
err << "Failed to open '" << args[1] << "'!"; |
|
112 |
throwCommandException(err); |
|
113 |
} |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
114 |
loadRegData(&io, file); |
1804 | 115 |
file.close(); |
116 |
} |
|
117 |
} else { |
|
118 |
stringstream strValue; |
|
119 |
const DataType *dataType = findDataType(getDataType()); |
|
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
120 |
|
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
121 |
if (!dataType) { |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
122 |
err << "Invalid data type '" << getDataType() << "'!"; |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
123 |
throwInvalidUsageException(err); |
1388 | 124 |
} |
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
125 |
|
1804 | 126 |
if (dataType->byteSize) { |
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
127 |
io.size = dataType->byteSize; |
1835
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
128 |
} else { |
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
129 |
io.size = 1024; // FIXME |
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
130 |
} |
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
131 |
|
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
132 |
io.data = new uint8_t[io.size]; |
1804 | 133 |
|
134 |
try { |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
135 |
io.size = interpretAsType( |
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
136 |
dataType, args[1], io.data, io.size); |
1835
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
137 |
} catch (SizeException &e) { |
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
138 |
delete [] io.data; |
1835
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
139 |
throwCommandException(e.what()); |
1804 | 140 |
} catch (ios::failure &e) { |
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
141 |
delete [] io.data; |
1804 | 142 |
err << "Invalid value argument '" << args[1] |
143 |
<< "' for type '" << dataType->name << "'!"; |
|
144 |
throwInvalidUsageException(err); |
|
145 |
} |
|
146 |
} |
|
1388 | 147 |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
148 |
if ((uint32_t) io.address + io.size > 0xffff) { |
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
149 |
err << "Address and size exceeding 64k!"; |
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
150 |
delete [] io.data; |
1388 | 151 |
throwInvalidUsageException(err); |
152 |
} |
|
153 |
||
1870
0cee1fb7f0fd
Added getSingleMasterIndex() for command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
1869
diff
changeset
|
154 |
MasterDevice m(getSingleMasterIndex()); |
1388 | 155 |
try { |
156 |
m.open(MasterDevice::ReadWrite); |
|
157 |
} catch (MasterDeviceException &e) { |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
158 |
delete [] io.data; |
1388 | 159 |
throw e; |
160 |
} |
|
161 |
||
2529
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
162 |
if (getEmergency()) { |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
163 |
io.slave_position = emergencySlave(); |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
164 |
io.emergency = true; |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
165 |
} |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
166 |
else { |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
167 |
SlaveList slaves = selectedSlaves(m); |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
168 |
if (slaves.size() != 1) { |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
169 |
delete [] io.data; |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
170 |
throwSingleSlaveRequired(slaves.size()); |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
171 |
} |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
172 |
io.slave_position = slaves.front().position; |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
173 |
io.emergency = false; |
c7e1f2616a9d
Implemented emergency register requests to analyze a broken bus.
Florian Pose <fp@igh-essen.com>
parents:
2443
diff
changeset
|
174 |
} |
1388 | 175 |
|
176 |
// send data to master |
|
177 |
try { |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
178 |
m.writeReg(&io); |
1388 | 179 |
} catch (MasterDeviceException &e) { |
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
180 |
delete [] io.data; |
1388 | 181 |
throw e; |
182 |
} |
|
183 |
||
184 |
if (getVerbosity() == Verbose) { |
|
185 |
cerr << "Register writing finished." << endl; |
|
186 |
} |
|
187 |
||
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
188 |
delete [] io.data; |
1388 | 189 |
} |
190 |
||
191 |
/*****************************************************************************/ |
|
192 |
||
193 |
void CommandRegWrite::loadRegData( |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
194 |
ec_ioctl_slave_reg_t *io, |
1388 | 195 |
const istream &in |
196 |
) |
|
197 |
{ |
|
198 |
stringstream err; |
|
199 |
ostringstream tmp; |
|
200 |
||
201 |
tmp << in.rdbuf(); |
|
202 |
string const &contents = tmp.str(); |
|
203 |
||
204 |
if (getVerbosity() == Verbose) { |
|
205 |
cerr << "Read " << contents.size() << " bytes of data." << endl; |
|
206 |
} |
|
207 |
||
208 |
if (contents.size() > 0xffff) { |
|
209 |
err << "Invalid data size " << contents.size() << "!"; |
|
210 |
throwInvalidUsageException(err); |
|
211 |
} |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
212 |
io->size = contents.size(); |
1388 | 213 |
|
214 |
// allocate buffer and read file into buffer |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
215 |
io->data = new uint8_t[io->size]; |
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
216 |
contents.copy((char *) io->data, contents.size()); |
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
217 |
} |
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
218 |
|
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
219 |
/*****************************************************************************/ |