author | Florian Pose <fp@igh-essen.com> |
Wed, 04 Jan 2012 14:04:24 +0100 | |
changeset 2186 | 064fe2a6835c |
parent 1968 | 4f682084c643 |
child 2421 | bc2d4bf9cbe5 |
child 2589 | 2b9c78543663 |
permissions | -rw-r--r-- |
1388 | 1 |
/***************************************************************************** |
2 |
* |
|
3 |
* $Id$ |
|
4 |
* |
|
5 |
* Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH |
|
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 |
1388 | 54 |
<< endl |
55 |
<< getBriefDescription() << endl |
|
56 |
<< endl |
|
57 |
<< "This command requires a single slave to be selected." << endl |
|
1804 | 58 |
<< endl |
1388 | 59 |
<< "Arguments:" << endl |
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
60 |
<< " OFFSET is the register address to write to." << endl |
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; |
|
86 |
ec_ioctl_slave_reg_t data; |
|
87 |
ifstream file; |
|
88 |
SlaveList slaves; |
|
89 |
||
90 |
if (args.size() != 2) { |
|
91 |
err << "'" << getName() << "' takes exactly two arguments!"; |
|
92 |
throwInvalidUsageException(err); |
|
93 |
} |
|
94 |
||
95 |
strOffset << args[0]; |
|
96 |
strOffset |
|
97 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
98 |
>> data.offset; |
|
99 |
if (strOffset.fail()) { |
|
100 |
err << "Invalid offset '" << args[0] << "'!"; |
|
101 |
throwInvalidUsageException(err); |
|
102 |
} |
|
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
103 |
|
1804 | 104 |
if (getDataType().empty()) { |
105 |
if (args[1] == "-") { |
|
106 |
loadRegData(&data, cin); |
|
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 |
} |
|
113 |
loadRegData(&data, file); |
|
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) { |
126 |
data.length = 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 { |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
128 |
data.length = 1024; // FIXME |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
129 |
} |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
130 |
|
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
131 |
data.data = new uint8_t[data.length]; |
1804 | 132 |
|
133 |
try { |
|
1835
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
134 |
data.length = interpretAsType( |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
135 |
dataType, args[1], data.data, data.length); |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
136 |
} catch (SizeException &e) { |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
137 |
delete [] data.data; |
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) { |
140 |
delete [] data.data; |
|
141 |
err << "Invalid value argument '" << args[1] |
|
142 |
<< "' for type '" << dataType->name << "'!"; |
|
143 |
throwInvalidUsageException(err); |
|
144 |
} |
|
145 |
} |
|
1388 | 146 |
|
147 |
if ((uint32_t) data.offset + data.length > 0xffff) { |
|
148 |
err << "Offset and length exceeding 64k!"; |
|
149 |
delete [] data.data; |
|
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) { |
|
157 |
delete [] data.data; |
|
158 |
throw e; |
|
159 |
} |
|
160 |
||
161 |
slaves = selectedSlaves(m); |
|
162 |
if (slaves.size() != 1) { |
|
163 |
delete [] data.data; |
|
164 |
throwSingleSlaveRequired(slaves.size()); |
|
165 |
} |
|
166 |
data.slave_position = slaves.front().position; |
|
167 |
||
168 |
// send data to master |
|
169 |
try { |
|
170 |
m.writeReg(&data); |
|
171 |
} catch (MasterDeviceException &e) { |
|
172 |
delete [] data.data; |
|
173 |
throw e; |
|
174 |
} |
|
175 |
||
176 |
if (getVerbosity() == Verbose) { |
|
177 |
cerr << "Register writing finished." << endl; |
|
178 |
} |
|
179 |
||
180 |
delete [] data.data; |
|
181 |
} |
|
182 |
||
183 |
/*****************************************************************************/ |
|
184 |
||
185 |
void CommandRegWrite::loadRegData( |
|
186 |
ec_ioctl_slave_reg_t *data, |
|
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 |
} |
|
204 |
data->length = contents.size(); |
|
205 |
||
206 |
// allocate buffer and read file into buffer |
|
207 |
data->data = new uint8_t[data->length]; |
|
208 |
contents.copy((char *) data->data, contents.size()); |
|
209 |
} |
|
210 |
||
211 |
/*****************************************************************************/ |