author | Florian Pose <fp@igh-essen.com> |
Tue, 09 Mar 2010 14:40:25 +0100 | |
changeset 1865 | c6c8b457bb40 |
parent 1835 | 20748e9d2238 |
child 1868 | 489ea0becd74 |
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 |
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
67 |
<< "These are the valid data types:" << endl |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
68 |
<< " int8, int16, int32, int64, uint8, uint16, uint32," << endl |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
69 |
<< " uint64, string." << endl |
1804 | 70 |
<< endl |
1388 | 71 |
<< "Command-specific options:" << endl |
72 |
<< " --alias -a <alias>" << endl |
|
73 |
<< " --position -p <pos> Slave selection. See the help of" << endl |
|
74 |
<< " the 'slaves' command." << endl |
|
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
75 |
<< " --type -t <type> Data type (see above)." << endl |
1388 | 76 |
<< endl |
77 |
<< numericInfo(); |
|
78 |
||
79 |
return str.str(); |
|
80 |
} |
|
81 |
||
82 |
/****************************************************************************/ |
|
83 |
||
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
84 |
void CommandRegWrite::execute(const StringVector &args) |
1388 | 85 |
{ |
86 |
stringstream strOffset, err; |
|
87 |
ec_ioctl_slave_reg_t data; |
|
88 |
ifstream file; |
|
89 |
SlaveList slaves; |
|
90 |
||
91 |
if (args.size() != 2) { |
|
92 |
err << "'" << getName() << "' takes exactly two arguments!"; |
|
93 |
throwInvalidUsageException(err); |
|
94 |
} |
|
95 |
||
96 |
strOffset << args[0]; |
|
97 |
strOffset |
|
98 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
99 |
>> data.offset; |
|
100 |
if (strOffset.fail()) { |
|
101 |
err << "Invalid offset '" << args[0] << "'!"; |
|
102 |
throwInvalidUsageException(err); |
|
103 |
} |
|
1826
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
104 |
|
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
105 |
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
|
106 |
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
|
107 |
throwInvalidUsageException(err); |
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
108 |
} |
ec6223c3b7ec
Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents:
1804
diff
changeset
|
109 |
MasterDevice m(getMasterIndices().front()); |
1388 | 110 |
|
1804 | 111 |
if (getDataType().empty()) { |
112 |
if (args[1] == "-") { |
|
113 |
loadRegData(&data, cin); |
|
114 |
} else { |
|
115 |
file.open(args[1].c_str(), ifstream::in | ifstream::binary); |
|
116 |
if (file.fail()) { |
|
117 |
err << "Failed to open '" << args[1] << "'!"; |
|
118 |
throwCommandException(err); |
|
119 |
} |
|
120 |
loadRegData(&data, file); |
|
121 |
file.close(); |
|
122 |
} |
|
123 |
} else { |
|
124 |
stringstream strValue; |
|
125 |
const DataType *dataType = findDataType(getDataType()); |
|
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
126 |
|
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
127 |
if (!dataType) { |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
128 |
err << "Invalid data type '" << getDataType() << "'!"; |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
129 |
throwInvalidUsageException(err); |
1388 | 130 |
} |
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
131 |
|
1804 | 132 |
if (dataType->byteSize) { |
133 |
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
|
134 |
} else { |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
135 |
data.length = 1024; // FIXME |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
136 |
} |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
137 |
|
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
138 |
data.data = new uint8_t[data.length]; |
1804 | 139 |
|
140 |
try { |
|
1835
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
141 |
data.length = interpretAsType( |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
142 |
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
|
143 |
} catch (SizeException &e) { |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
144 |
delete [] data.data; |
20748e9d2238
Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
1826
diff
changeset
|
145 |
throwCommandException(e.what()); |
1804 | 146 |
} catch (ios::failure &e) { |
147 |
delete [] data.data; |
|
148 |
err << "Invalid value argument '" << args[1] |
|
149 |
<< "' for type '" << dataType->name << "'!"; |
|
150 |
throwInvalidUsageException(err); |
|
151 |
} |
|
152 |
} |
|
1388 | 153 |
|
154 |
if ((uint32_t) data.offset + data.length > 0xffff) { |
|
155 |
err << "Offset and length exceeding 64k!"; |
|
156 |
delete [] data.data; |
|
157 |
throwInvalidUsageException(err); |
|
158 |
} |
|
159 |
||
160 |
try { |
|
161 |
m.open(MasterDevice::ReadWrite); |
|
162 |
} catch (MasterDeviceException &e) { |
|
163 |
delete [] data.data; |
|
164 |
throw e; |
|
165 |
} |
|
166 |
||
167 |
slaves = selectedSlaves(m); |
|
168 |
if (slaves.size() != 1) { |
|
169 |
delete [] data.data; |
|
170 |
throwSingleSlaveRequired(slaves.size()); |
|
171 |
} |
|
172 |
data.slave_position = slaves.front().position; |
|
173 |
||
174 |
// send data to master |
|
175 |
try { |
|
176 |
m.writeReg(&data); |
|
177 |
} catch (MasterDeviceException &e) { |
|
178 |
delete [] data.data; |
|
179 |
throw e; |
|
180 |
} |
|
181 |
||
182 |
if (getVerbosity() == Verbose) { |
|
183 |
cerr << "Register writing finished." << endl; |
|
184 |
} |
|
185 |
||
186 |
delete [] data.data; |
|
187 |
} |
|
188 |
||
189 |
/*****************************************************************************/ |
|
190 |
||
191 |
void CommandRegWrite::loadRegData( |
|
192 |
ec_ioctl_slave_reg_t *data, |
|
193 |
const istream &in |
|
194 |
) |
|
195 |
{ |
|
196 |
stringstream err; |
|
197 |
ostringstream tmp; |
|
198 |
||
199 |
tmp << in.rdbuf(); |
|
200 |
string const &contents = tmp.str(); |
|
201 |
||
202 |
if (getVerbosity() == Verbose) { |
|
203 |
cerr << "Read " << contents.size() << " bytes of data." << endl; |
|
204 |
} |
|
205 |
||
206 |
if (contents.size() > 0xffff) { |
|
207 |
err << "Invalid data size " << contents.size() << "!"; |
|
208 |
throwInvalidUsageException(err); |
|
209 |
} |
|
210 |
data->length = contents.size(); |
|
211 |
||
212 |
// allocate buffer and read file into buffer |
|
213 |
data->data = new uint8_t[data->length]; |
|
214 |
contents.copy((char *) data->data, contents.size()); |
|
215 |
} |
|
216 |
||
217 |
/*****************************************************************************/ |