author | Florian Pose <fp@igh-essen.com> |
Mon, 01 Mar 2010 18:39:06 +0100 | |
changeset 1832 | fb6a307daf31 |
parent 1826 | ec6223c3b7ec |
child 1835 | 20748e9d2238 |
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(): |
|
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
42 |
CommandReg("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; |
|
134 |
data.data = new uint8_t[data.length]; |
|
135 |
} |
|
136 |
||
137 |
strValue << args[1]; |
|
138 |
strValue >> resetiosflags(ios::basefield); // guess base from prefix |
|
139 |
strValue.exceptions(ios::failbit); |
|
140 |
||
141 |
try { |
|
142 |
if (string(dataType->name) == "int8") { |
|
143 |
int16_t val; // uint8_t is interpreted as char |
|
144 |
strValue >> val; |
|
145 |
if (val > 127 || val < -128) |
|
146 |
throw ios::failure("Value out of range"); |
|
147 |
*data.data = (int8_t) val; |
|
148 |
} else if (string(dataType->name) == "int16") { |
|
149 |
int16_t val; |
|
150 |
strValue >> val; |
|
151 |
*(int16_t *) data.data = cpu_to_le16(val); |
|
152 |
} else if (string(dataType->name) == "int32") { |
|
153 |
int32_t val; |
|
154 |
strValue >> val; |
|
155 |
*(int32_t *) data.data = cpu_to_le32(val); |
|
156 |
} else if (string(dataType->name) == "int64") { |
|
157 |
int64_t val; |
|
158 |
strValue >> val; |
|
159 |
*(int64_t *) data.data = cpu_to_le64(val); |
|
160 |
} else if (string(dataType->name) == "uint8") { |
|
161 |
uint16_t val; // uint8_t is interpreted as char |
|
162 |
strValue >> val; |
|
163 |
if (val > 0xff) |
|
164 |
throw ios::failure("Value out of range"); |
|
165 |
*data.data = (uint8_t) val; |
|
166 |
} else if (string(dataType->name) == "uint16") { |
|
167 |
uint16_t val; |
|
168 |
strValue >> val; |
|
169 |
*(uint16_t *) data.data = cpu_to_le16(val); |
|
170 |
} else if (string(dataType->name) == "uint32") { |
|
171 |
uint32_t val; |
|
172 |
strValue >> val; |
|
173 |
*(uint32_t *) data.data = cpu_to_le32(val); |
|
174 |
} else if (string(dataType->name) == "uint64") { |
|
175 |
uint64_t val; |
|
176 |
strValue >> val; |
|
177 |
*(uint64_t *) data.data = cpu_to_le64(val); |
|
178 |
} else if (string(dataType->name) == "string" || |
|
179 |
string(dataType->name) == "octet_string") { |
|
180 |
data.length = strValue.str().size(); |
|
181 |
if (!data.length) { |
|
182 |
err << "Zero-size string now allowed!"; |
|
183 |
throwCommandException(err); |
|
184 |
} |
|
185 |
data.data = new uint8_t[data.length]; |
|
186 |
strValue >> (char *) data.data; |
|
187 |
} else { |
|
188 |
err << "Invalid data type " << dataType->name; |
|
189 |
throwCommandException(err); |
|
190 |
} |
|
191 |
} catch (ios::failure &e) { |
|
192 |
delete [] data.data; |
|
193 |
err << "Invalid value argument '" << args[1] |
|
194 |
<< "' for type '" << dataType->name << "'!"; |
|
195 |
throwInvalidUsageException(err); |
|
196 |
} |
|
197 |
} |
|
1388 | 198 |
|
199 |
if ((uint32_t) data.offset + data.length > 0xffff) { |
|
200 |
err << "Offset and length exceeding 64k!"; |
|
201 |
delete [] data.data; |
|
202 |
throwInvalidUsageException(err); |
|
203 |
} |
|
204 |
||
205 |
try { |
|
206 |
m.open(MasterDevice::ReadWrite); |
|
207 |
} catch (MasterDeviceException &e) { |
|
208 |
delete [] data.data; |
|
209 |
throw e; |
|
210 |
} |
|
211 |
||
212 |
slaves = selectedSlaves(m); |
|
213 |
if (slaves.size() != 1) { |
|
214 |
delete [] data.data; |
|
215 |
throwSingleSlaveRequired(slaves.size()); |
|
216 |
} |
|
217 |
data.slave_position = slaves.front().position; |
|
218 |
||
219 |
// send data to master |
|
220 |
try { |
|
221 |
m.writeReg(&data); |
|
222 |
} catch (MasterDeviceException &e) { |
|
223 |
delete [] data.data; |
|
224 |
throw e; |
|
225 |
} |
|
226 |
||
227 |
if (getVerbosity() == Verbose) { |
|
228 |
cerr << "Register writing finished." << endl; |
|
229 |
} |
|
230 |
||
231 |
delete [] data.data; |
|
232 |
} |
|
233 |
||
234 |
/*****************************************************************************/ |
|
235 |
||
236 |
void CommandRegWrite::loadRegData( |
|
237 |
ec_ioctl_slave_reg_t *data, |
|
238 |
const istream &in |
|
239 |
) |
|
240 |
{ |
|
241 |
stringstream err; |
|
242 |
ostringstream tmp; |
|
243 |
||
244 |
tmp << in.rdbuf(); |
|
245 |
string const &contents = tmp.str(); |
|
246 |
||
247 |
if (getVerbosity() == Verbose) { |
|
248 |
cerr << "Read " << contents.size() << " bytes of data." << endl; |
|
249 |
} |
|
250 |
||
251 |
if (contents.size() > 0xffff) { |
|
252 |
err << "Invalid data size " << contents.size() << "!"; |
|
253 |
throwInvalidUsageException(err); |
|
254 |
} |
|
255 |
data->length = contents.size(); |
|
256 |
||
257 |
// allocate buffer and read file into buffer |
|
258 |
data->data = new uint8_t[data->length]; |
|
259 |
contents.copy((char *) data->data, contents.size()); |
|
260 |
} |
|
261 |
||
262 |
/*****************************************************************************/ |