author | Florian Pose <fp@igh-essen.com> |
Tue, 23 Feb 2010 13:18:31 +0100 | |
changeset 1821 | 40321a37d085 |
parent 1804 | 742607c464c4 |
child 1826 | ec6223c3b7ec |
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" |
|
37 |
||
38 |
/*****************************************************************************/ |
|
39 |
||
40 |
CommandRegWrite::CommandRegWrite(): |
|
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
41 |
CommandReg("reg_write", "Write data to a slave's registers.") |
1388 | 42 |
{ |
43 |
} |
|
44 |
||
45 |
/*****************************************************************************/ |
|
46 |
||
47 |
string CommandRegWrite::helpString() const |
|
48 |
{ |
|
49 |
stringstream str; |
|
50 |
||
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
51 |
str << getName() << " [OPTIONS] <OFFSET> <DATA>" << endl |
1388 | 52 |
<< endl |
53 |
<< getBriefDescription() << endl |
|
54 |
<< endl |
|
55 |
<< "This command requires a single slave to be selected." << endl |
|
1804 | 56 |
<< endl |
1388 | 57 |
<< "Arguments:" << endl |
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
58 |
<< " 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
|
59 |
<< " DATA depends on whether a datatype was specified" << endl |
1804 | 60 |
<< " with the --type option: If not, DATA must be" << endl |
61 |
<< " either a path to a file with data to write," << endl |
|
62 |
<< " or '-', which means, that data are read from" << endl |
|
63 |
<< " stdin. If a datatype was specified, VALUE is" << endl |
|
64 |
<< " interpreted respective to the given type." << endl |
|
1388 | 65 |
<< endl |
1389
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
66 |
<< "These are the valid data types:" << endl |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
67 |
<< " int8, int16, int32, int64, uint8, uint16, uint32," << endl |
5b392b22a2da
reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents:
1388
diff
changeset
|
68 |
<< " uint64, string." << endl |
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 |
||
83 |
void CommandRegWrite::execute(MasterDevice &m, const StringVector &args) |
|
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 |
} |
|
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; |
|
127 |
data.data = new uint8_t[data.length]; |
|
128 |
} |
|
129 |
||
130 |
strValue << args[1]; |
|
131 |
strValue >> resetiosflags(ios::basefield); // guess base from prefix |
|
132 |
strValue.exceptions(ios::failbit); |
|
133 |
||
134 |
try { |
|
135 |
if (string(dataType->name) == "int8") { |
|
136 |
int16_t val; // uint8_t is interpreted as char |
|
137 |
strValue >> val; |
|
138 |
if (val > 127 || val < -128) |
|
139 |
throw ios::failure("Value out of range"); |
|
140 |
*data.data = (int8_t) val; |
|
141 |
} else if (string(dataType->name) == "int16") { |
|
142 |
int16_t val; |
|
143 |
strValue >> val; |
|
144 |
*(int16_t *) data.data = cpu_to_le16(val); |
|
145 |
} else if (string(dataType->name) == "int32") { |
|
146 |
int32_t val; |
|
147 |
strValue >> val; |
|
148 |
*(int32_t *) data.data = cpu_to_le32(val); |
|
149 |
} else if (string(dataType->name) == "int64") { |
|
150 |
int64_t val; |
|
151 |
strValue >> val; |
|
152 |
*(int64_t *) data.data = cpu_to_le64(val); |
|
153 |
} else if (string(dataType->name) == "uint8") { |
|
154 |
uint16_t val; // uint8_t is interpreted as char |
|
155 |
strValue >> val; |
|
156 |
if (val > 0xff) |
|
157 |
throw ios::failure("Value out of range"); |
|
158 |
*data.data = (uint8_t) val; |
|
159 |
} else if (string(dataType->name) == "uint16") { |
|
160 |
uint16_t val; |
|
161 |
strValue >> val; |
|
162 |
*(uint16_t *) data.data = cpu_to_le16(val); |
|
163 |
} else if (string(dataType->name) == "uint32") { |
|
164 |
uint32_t val; |
|
165 |
strValue >> val; |
|
166 |
*(uint32_t *) data.data = cpu_to_le32(val); |
|
167 |
} else if (string(dataType->name) == "uint64") { |
|
168 |
uint64_t val; |
|
169 |
strValue >> val; |
|
170 |
*(uint64_t *) data.data = cpu_to_le64(val); |
|
171 |
} else if (string(dataType->name) == "string" || |
|
172 |
string(dataType->name) == "octet_string") { |
|
173 |
data.length = strValue.str().size(); |
|
174 |
if (!data.length) { |
|
175 |
err << "Zero-size string now allowed!"; |
|
176 |
throwCommandException(err); |
|
177 |
} |
|
178 |
data.data = new uint8_t[data.length]; |
|
179 |
strValue >> (char *) data.data; |
|
180 |
} else { |
|
181 |
err << "Invalid data type " << dataType->name; |
|
182 |
throwCommandException(err); |
|
183 |
} |
|
184 |
} catch (ios::failure &e) { |
|
185 |
delete [] data.data; |
|
186 |
err << "Invalid value argument '" << args[1] |
|
187 |
<< "' for type '" << dataType->name << "'!"; |
|
188 |
throwInvalidUsageException(err); |
|
189 |
} |
|
190 |
} |
|
1388 | 191 |
|
192 |
if ((uint32_t) data.offset + data.length > 0xffff) { |
|
193 |
err << "Offset and length exceeding 64k!"; |
|
194 |
delete [] data.data; |
|
195 |
throwInvalidUsageException(err); |
|
196 |
} |
|
197 |
||
198 |
try { |
|
199 |
m.open(MasterDevice::ReadWrite); |
|
200 |
} catch (MasterDeviceException &e) { |
|
201 |
delete [] data.data; |
|
202 |
throw e; |
|
203 |
} |
|
204 |
||
205 |
slaves = selectedSlaves(m); |
|
206 |
if (slaves.size() != 1) { |
|
207 |
delete [] data.data; |
|
208 |
throwSingleSlaveRequired(slaves.size()); |
|
209 |
} |
|
210 |
data.slave_position = slaves.front().position; |
|
211 |
||
212 |
// send data to master |
|
213 |
try { |
|
214 |
m.writeReg(&data); |
|
215 |
} catch (MasterDeviceException &e) { |
|
216 |
delete [] data.data; |
|
217 |
throw e; |
|
218 |
} |
|
219 |
||
220 |
if (getVerbosity() == Verbose) { |
|
221 |
cerr << "Register writing finished." << endl; |
|
222 |
} |
|
223 |
||
224 |
delete [] data.data; |
|
225 |
} |
|
226 |
||
227 |
/*****************************************************************************/ |
|
228 |
||
229 |
void CommandRegWrite::loadRegData( |
|
230 |
ec_ioctl_slave_reg_t *data, |
|
231 |
const istream &in |
|
232 |
) |
|
233 |
{ |
|
234 |
stringstream err; |
|
235 |
ostringstream tmp; |
|
236 |
||
237 |
tmp << in.rdbuf(); |
|
238 |
string const &contents = tmp.str(); |
|
239 |
||
240 |
if (getVerbosity() == Verbose) { |
|
241 |
cerr << "Read " << contents.size() << " bytes of data." << endl; |
|
242 |
} |
|
243 |
||
244 |
if (contents.size() > 0xffff) { |
|
245 |
err << "Invalid data size " << contents.size() << "!"; |
|
246 |
throwInvalidUsageException(err); |
|
247 |
} |
|
248 |
data->length = contents.size(); |
|
249 |
||
250 |
// allocate buffer and read file into buffer |
|
251 |
data->data = new uint8_t[data->length]; |
|
252 |
contents.copy((char *) data->data, contents.size()); |
|
253 |
} |
|
254 |
||
255 |
/*****************************************************************************/ |