author | Florian Pose <fp@igh-essen.com> |
Thu, 15 Apr 2010 12:31:12 +0200 | |
changeset 1892 | 0c254f0e7f7d |
parent 1870 | 0cee1fb7f0fd |
child 1968 | 4f682084c643 |
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 |
||
48 |
string CommandRegWrite::helpString() const |
|
49 |
{ |
|
50 |
stringstream str; |
|
51 |
||
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
52 |
str << getName() << " [OPTIONS] <OFFSET> <DATA>" << endl |
1388 | 53 |
<< endl |
54 |
<< getBriefDescription() << endl |
|
55 |
<< endl |
|
56 |
<< "This command requires a single slave to be selected." << endl |
|
1804 | 57 |
<< endl |
1388 | 58 |
<< "Arguments:" << endl |
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
59 |
<< " 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
|
60 |
<< " DATA depends on whether a datatype was specified" << endl |
1804 | 61 |
<< " with the --type option: If not, DATA must be" << endl |
62 |
<< " either a path to a file with data to write," << endl |
|
63 |
<< " or '-', which means, that data are read from" << endl |
|
64 |
<< " stdin. If a datatype was specified, VALUE is" << endl |
|
65 |
<< " interpreted respective to the given type." << endl |
|
1388 | 66 |
<< 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
|
67 |
<< typeInfo() |
1804 | 68 |
<< endl |
1388 | 69 |
<< "Command-specific options:" << endl |
70 |
<< " --alias -a <alias>" << endl |
|
71 |
<< " --position -p <pos> Slave selection. See the help of" << endl |
|
72 |
<< " the 'slaves' command." << endl |
|
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
73 |
<< " --type -t <type> Data type (see above)." << endl |
1388 | 74 |
<< endl |
75 |
<< numericInfo(); |
|
76 |
||
77 |
return str.str(); |
|
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 CommandRegWrite::execute(const StringVector &args) |
1388 | 83 |
{ |
84 |
stringstream strOffset, err; |
|
85 |
ec_ioctl_slave_reg_t data; |
|
86 |
ifstream file; |
|
87 |
SlaveList slaves; |
|
88 |
||
89 |
if (args.size() != 2) { |
|
90 |
err << "'" << getName() << "' takes exactly two arguments!"; |
|
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 |
} |
|
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
102 |
|
1804 | 103 |
if (getDataType().empty()) { |
104 |
if (args[1] == "-") { |
|
105 |
loadRegData(&data, cin); |
|
106 |
} else { |
|
107 |
file.open(args[1].c_str(), ifstream::in | ifstream::binary); |
|
108 |
if (file.fail()) { |
|
109 |
err << "Failed to open '" << args[1] << "'!"; |
|
110 |
throwCommandException(err); |
|
111 |
} |
|
112 |
loadRegData(&data, file); |
|
113 |
file.close(); |
|
114 |
} |
|
115 |
} else { |
|
116 |
stringstream strValue; |
|
117 |
const DataType *dataType = findDataType(getDataType()); |
|
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
118 |
|
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
119 |
if (!dataType) { |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
120 |
err << "Invalid data type '" << getDataType() << "'!"; |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
121 |
throwInvalidUsageException(err); |
1388 | 122 |
} |
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
123 |
|
1804 | 124 |
if (dataType->byteSize) { |
125 |
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
|
126 |
} else { |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
127 |
data.length = 1024; // FIXME |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
128 |
} |
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 |
data.data = new uint8_t[data.length]; |
1804 | 131 |
|
132 |
try { |
|
1835
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
133 |
data.length = interpretAsType( |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
134 |
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
|
135 |
} catch (SizeException &e) { |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
136 |
delete [] data.data; |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
137 |
throwCommandException(e.what()); |
1804 | 138 |
} catch (ios::failure &e) { |
139 |
delete [] data.data; |
|
140 |
err << "Invalid value argument '" << args[1] |
|
141 |
<< "' for type '" << dataType->name << "'!"; |
|
142 |
throwInvalidUsageException(err); |
|
143 |
} |
|
144 |
} |
|
1388 | 145 |
|
146 |
if ((uint32_t) data.offset + data.length > 0xffff) { |
|
147 |
err << "Offset and length exceeding 64k!"; |
|
148 |
delete [] data.data; |
|
149 |
throwInvalidUsageException(err); |
|
150 |
} |
|
151 |
||
1870
0cee1fb7f0fd
Added getSingleMasterIndex() for command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
1869
diff
changeset
|
152 |
MasterDevice m(getSingleMasterIndex()); |
1388 | 153 |
try { |
154 |
m.open(MasterDevice::ReadWrite); |
|
155 |
} catch (MasterDeviceException &e) { |
|
156 |
delete [] data.data; |
|
157 |
throw e; |
|
158 |
} |
|
159 |
||
160 |
slaves = selectedSlaves(m); |
|
161 |
if (slaves.size() != 1) { |
|
162 |
delete [] data.data; |
|
163 |
throwSingleSlaveRequired(slaves.size()); |
|
164 |
} |
|
165 |
data.slave_position = slaves.front().position; |
|
166 |
||
167 |
// send data to master |
|
168 |
try { |
|
169 |
m.writeReg(&data); |
|
170 |
} catch (MasterDeviceException &e) { |
|
171 |
delete [] data.data; |
|
172 |
throw e; |
|
173 |
} |
|
174 |
||
175 |
if (getVerbosity() == Verbose) { |
|
176 |
cerr << "Register writing finished." << endl; |
|
177 |
} |
|
178 |
||
179 |
delete [] data.data; |
|
180 |
} |
|
181 |
||
182 |
/*****************************************************************************/ |
|
183 |
||
184 |
void CommandRegWrite::loadRegData( |
|
185 |
ec_ioctl_slave_reg_t *data, |
|
186 |
const istream &in |
|
187 |
) |
|
188 |
{ |
|
189 |
stringstream err; |
|
190 |
ostringstream tmp; |
|
191 |
||
192 |
tmp << in.rdbuf(); |
|
193 |
string const &contents = tmp.str(); |
|
194 |
||
195 |
if (getVerbosity() == Verbose) { |
|
196 |
cerr << "Read " << contents.size() << " bytes of data." << endl; |
|
197 |
} |
|
198 |
||
199 |
if (contents.size() > 0xffff) { |
|
200 |
err << "Invalid data size " << contents.size() << "!"; |
|
201 |
throwInvalidUsageException(err); |
|
202 |
} |
|
203 |
data->length = contents.size(); |
|
204 |
||
205 |
// allocate buffer and read file into buffer |
|
206 |
data->data = new uint8_t[data->length]; |
|
207 |
contents.copy((char *) data->data, contents.size()); |
|
208 |
} |
|
209 |
||
210 |
/*****************************************************************************/ |