author | Florian Pose <fp@igh-essen.com> |
Fri, 30 Nov 2012 15:24:38 +0100 | |
branch | stable-1.5 |
changeset 2452 | abc1d1caead7 |
parent 2443 | 2c3ccdde3919 |
child 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 |
71 |
<< " --alias -a <alias>" << endl |
|
72 |
<< " --position -p <pos> Slave selection. See the help of" << endl |
|
73 |
<< " the 'slaves' command." << endl |
|
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
74 |
<< " --type -t <type> Data type (see above)." << endl |
1388 | 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 CommandRegWrite::execute(const StringVector &args) |
1388 | 84 |
{ |
85 |
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
|
86 |
ec_ioctl_slave_reg_t io; |
1388 | 87 |
ifstream file; |
88 |
SlaveList slaves; |
|
89 |
||
90 |
if (args.size() != 2) { |
|
91 |
err << "'" << getName() << "' takes exactly two arguments!"; |
|
92 |
throwInvalidUsageException(err); |
|
93 |
} |
|
2421 | 94 |
|
1388 | 95 |
strOffset << args[0]; |
96 |
strOffset |
|
97 |
>> 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
|
98 |
>> io.address; |
1388 | 99 |
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
|
100 |
err << "Invalid address '" << args[0] << "'!"; |
1388 | 101 |
throwInvalidUsageException(err); |
102 |
} |
|
2421 | 103 |
|
1804 | 104 |
if (getDataType().empty()) { |
105 |
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
|
106 |
loadRegData(&io, cin); |
1804 | 107 |
} else { |
108 |
file.open(args[1].c_str(), ifstream::in | ifstream::binary); |
|
109 |
if (file.fail()) { |
|
110 |
err << "Failed to open '" << args[1] << "'!"; |
|
111 |
throwCommandException(err); |
|
112 |
} |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
113 |
loadRegData(&io, file); |
1804 | 114 |
file.close(); |
115 |
} |
|
116 |
} else { |
|
117 |
stringstream strValue; |
|
118 |
const DataType *dataType = findDataType(getDataType()); |
|
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
119 |
|
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
120 |
if (!dataType) { |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
121 |
err << "Invalid data type '" << getDataType() << "'!"; |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
122 |
throwInvalidUsageException(err); |
1388 | 123 |
} |
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
124 |
|
1804 | 125 |
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
|
126 |
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
|
127 |
} else { |
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
128 |
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
|
129 |
} |
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 |
io.data = new uint8_t[io.size]; |
1804 | 132 |
|
133 |
try { |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
134 |
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
|
135 |
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
|
136 |
} 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
|
137 |
delete [] io.data; |
1835
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
138 |
throwCommandException(e.what()); |
1804 | 139 |
} 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
|
140 |
delete [] io.data; |
1804 | 141 |
err << "Invalid value argument '" << args[1] |
142 |
<< "' for type '" << dataType->name << "'!"; |
|
143 |
throwInvalidUsageException(err); |
|
144 |
} |
|
145 |
} |
|
1388 | 146 |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
delete [] io.data; |
1388 | 150 |
throwInvalidUsageException(err); |
151 |
} |
|
152 |
||
1870
0cee1fb7f0fd
Added getSingleMasterIndex() for command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
1869
diff
changeset
|
153 |
MasterDevice m(getSingleMasterIndex()); |
1388 | 154 |
try { |
155 |
m.open(MasterDevice::ReadWrite); |
|
156 |
} 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
|
157 |
delete [] io.data; |
1388 | 158 |
throw e; |
159 |
} |
|
160 |
||
161 |
slaves = selectedSlaves(m); |
|
162 |
if (slaves.size() != 1) { |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
163 |
delete [] io.data; |
1388 | 164 |
throwSingleSlaveRequired(slaves.size()); |
165 |
} |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
166 |
io.slave_position = slaves.front().position; |
1388 | 167 |
|
168 |
// send data to master |
|
169 |
try { |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
170 |
m.writeReg(&io); |
1388 | 171 |
} 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
|
172 |
delete [] io.data; |
1388 | 173 |
throw e; |
174 |
} |
|
175 |
||
176 |
if (getVerbosity() == Verbose) { |
|
177 |
cerr << "Register writing finished." << endl; |
|
178 |
} |
|
179 |
||
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 |
} |
182 |
||
183 |
/*****************************************************************************/ |
|
184 |
||
185 |
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
|
186 |
ec_ioctl_slave_reg_t *io, |
1388 | 187 |
const istream &in |
188 |
) |
|
189 |
{ |
|
190 |
stringstream err; |
|
191 |
ostringstream tmp; |
|
192 |
||
193 |
tmp << in.rdbuf(); |
|
194 |
string const &contents = tmp.str(); |
|
195 |
||
196 |
if (getVerbosity() == Verbose) { |
|
197 |
cerr << "Read " << contents.size() << " bytes of data." << endl; |
|
198 |
} |
|
199 |
||
200 |
if (contents.size() > 0xffff) { |
|
201 |
err << "Invalid data size " << contents.size() << "!"; |
|
202 |
throwInvalidUsageException(err); |
|
203 |
} |
|
2443
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
204 |
io->size = contents.size(); |
1388 | 205 |
|
206 |
// 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
|
207 |
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
|
208 |
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
|
209 |
} |
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
210 |
|
2c3ccdde3919
Added interface to read/write register contents; re-worked register requests.
Florian Pose <fp@igh-essen.com>
parents:
2421
diff
changeset
|
211 |
/*****************************************************************************/ |