author | Florian Pose <fp@igh-essen.com> |
Thu, 19 Mar 2009 08:44:48 +0000 | |
changeset 1374 | 1ca1178a3f93 |
parent 1363 | 11c0b2caa253 |
child 1383 | 0c88c50c31e3 |
permissions | -rw-r--r-- |
1200 | 1 |
/***************************************************************************** |
2 |
* |
|
1363
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
3 |
* $Id$ |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
4 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
5 |
* Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
6 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
7 |
* This file is part of the IgH EtherCAT Master. |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
8 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
9 |
* The IgH EtherCAT Master is free software; you can redistribute it and/or |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
10 |
* modify it under the terms of the GNU General Public License version 2, as |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
11 |
* published by the Free Software Foundation. |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
12 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
13 |
* The IgH EtherCAT Master is distributed in the hope that it will be useful, |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
16 |
* Public License for more details. |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
17 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
18 |
* You should have received a copy of the GNU General Public License along |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
19 |
* with the IgH EtherCAT Master; if not, write to the Free Software |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
21 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
22 |
* --- |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
23 |
* |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
24 |
* The license mentioned above concerns the source code only. Using the |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
25 |
* EtherCAT technology and brand is only permitted in compliance with the |
11c0b2caa253
Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents:
1254
diff
changeset
|
26 |
* industrial property and similar rights of Beckhoff Automation GmbH. |
1200 | 27 |
* |
28 |
****************************************************************************/ |
|
29 |
||
30 |
#include <iostream> |
|
31 |
#include <iomanip> |
|
32 |
#include <fstream> |
|
33 |
using namespace std; |
|
34 |
||
35 |
#include "CommandPhyWrite.h" |
|
36 |
#include "sii_crc.h" |
|
37 |
||
38 |
/*****************************************************************************/ |
|
39 |
||
40 |
CommandPhyWrite::CommandPhyWrite(): |
|
41 |
Command("phy_write", "Write data to a slave's physical memory.") |
|
42 |
{ |
|
43 |
} |
|
44 |
||
45 |
/*****************************************************************************/ |
|
46 |
||
47 |
string CommandPhyWrite::helpString() const |
|
48 |
{ |
|
49 |
stringstream str; |
|
50 |
||
51 |
str << getName() << " [OPTIONS] <OFFSET> <FILENAME>" << endl |
|
52 |
<< endl |
|
53 |
<< getBriefDescription() << endl |
|
54 |
<< endl |
|
55 |
<< "This command requires a single slave to be selected." << endl |
|
56 |
<< endl |
|
57 |
<< "Arguments:" << endl |
|
58 |
<< " OFFSET must be the physical memory offset to start." << endl |
|
59 |
<< " FILENAME must be a path to a file with data to write." << endl |
|
60 |
<< " If it is '-', data are read from stdin." << endl |
|
61 |
<< endl |
|
62 |
<< "Command-specific options:" << endl |
|
63 |
<< " --alias -a <alias>" << endl |
|
64 |
<< " --position -p <pos> Slave selection. See the help of" << endl |
|
65 |
<< " the 'slaves' command." << endl |
|
66 |
<< endl |
|
67 |
<< numericInfo(); |
|
68 |
||
69 |
return str.str(); |
|
70 |
} |
|
71 |
||
72 |
/****************************************************************************/ |
|
73 |
||
74 |
void CommandPhyWrite::execute(MasterDevice &m, const StringVector &args) |
|
75 |
{ |
|
76 |
stringstream strOffset, err; |
|
77 |
ec_ioctl_slave_phy_t data; |
|
78 |
ifstream file; |
|
79 |
SlaveList slaves; |
|
80 |
||
81 |
if (args.size() != 2) { |
|
82 |
err << "'" << getName() << "' takes exactly one argument!"; |
|
83 |
throwInvalidUsageException(err); |
|
84 |
} |
|
85 |
||
86 |
strOffset << args[0]; |
|
87 |
strOffset |
|
88 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
89 |
>> data.offset; |
|
90 |
if (strOffset.fail()) { |
|
91 |
err << "Invalid offset '" << args[0] << "'!"; |
|
92 |
throwInvalidUsageException(err); |
|
93 |
} |
|
94 |
||
95 |
if (args[1] == "-") { |
|
96 |
loadPhyData(&data, cin); |
|
97 |
} else { |
|
98 |
file.open(args[1].c_str(), ifstream::in | ifstream::binary); |
|
99 |
if (file.fail()) { |
|
100 |
err << "Failed to open '" << args[0] << "'!"; |
|
101 |
throwCommandException(err); |
|
102 |
} |
|
103 |
loadPhyData(&data, file); |
|
104 |
file.close(); |
|
105 |
} |
|
106 |
||
107 |
if ((uint32_t) data.offset + data.length > 0xffff) { |
|
108 |
err << "Offset and length exceeding 64k!"; |
|
109 |
delete [] data.data; |
|
110 |
throwInvalidUsageException(err); |
|
111 |
} |
|
112 |
||
113 |
try { |
|
114 |
m.open(MasterDevice::ReadWrite); |
|
115 |
} catch (MasterDeviceException &e) { |
|
116 |
delete [] data.data; |
|
117 |
throw e; |
|
118 |
} |
|
119 |
||
120 |
slaves = selectedSlaves(m); |
|
121 |
if (slaves.size() != 1) { |
|
122 |
delete [] data.data; |
|
123 |
throwSingleSlaveRequired(slaves.size()); |
|
124 |
} |
|
125 |
data.slave_position = slaves.front().position; |
|
126 |
||
127 |
// send data to master |
|
128 |
try { |
|
129 |
m.writePhy(&data); |
|
130 |
} catch (MasterDeviceException &e) { |
|
131 |
delete [] data.data; |
|
132 |
throw e; |
|
133 |
} |
|
134 |
||
135 |
if (getVerbosity() == Verbose) { |
|
136 |
cerr << "Physical memory writing finished." << endl; |
|
137 |
} |
|
138 |
||
139 |
delete [] data.data; |
|
140 |
} |
|
141 |
||
142 |
/*****************************************************************************/ |
|
143 |
||
144 |
void CommandPhyWrite::loadPhyData( |
|
145 |
ec_ioctl_slave_phy_t *data, |
|
146 |
const istream &in |
|
147 |
) |
|
148 |
{ |
|
149 |
stringstream err; |
|
150 |
ostringstream tmp; |
|
151 |
||
152 |
tmp << in.rdbuf(); |
|
153 |
string const &contents = tmp.str(); |
|
154 |
||
155 |
if (getVerbosity() == Verbose) { |
|
156 |
cerr << "Read " << contents.size() << " bytes of data." << endl; |
|
157 |
} |
|
158 |
||
159 |
if (contents.size() > 0xffff) { |
|
160 |
err << "Invalid data size " << contents.size() << "!"; |
|
161 |
throwInvalidUsageException(err); |
|
162 |
} |
|
163 |
data->length = contents.size(); |
|
164 |
||
165 |
// allocate buffer and read file into buffer |
|
166 |
data->data = new uint8_t[data->length]; |
|
167 |
contents.copy((char *) data->data, contents.size()); |
|
168 |
} |
|
169 |
||
170 |
/*****************************************************************************/ |