-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCommandUpload.cpp
executable file
·273 lines (239 loc) · 8.75 KB
/
CommandUpload.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*****************************************************************************
*
* $Id: CommandUpload.cpp,v 4f682084c643 2010/10/25 08:12:26 fp $
*
* Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH
*
* This file is part of the IgH EtherCAT Master.
*
* The IgH EtherCAT Master is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* The IgH EtherCAT Master is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with the IgH EtherCAT Master; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* ---
*
* The license mentioned above concerns the source code only. Using the
* EtherCAT technology and brand is only permitted in compliance with the
* industrial property and similar rights of Beckhoff Automation GmbH.
*
****************************************************************************/
#include <iostream>
#include <iomanip>
#include "CommandUpload.h"
#include "MasterDevice.h"
#include <stdio.h>
#include <string>
using namespace std;
/**
* Added bay Oscar Caravaca 14/05/2015
* Global to store the returning value of any EPOS3 register after sending an Upload command
**/
int regValue;
Command::StringVector dataBuffer;
/*****************************************************************************/
CommandUpload::CommandUpload():
SdoCommand("upload", "Read an SDO entry from a slave.")
{
}
/*****************************************************************************/
string CommandUpload::helpString(const string &binaryBaseName) const
{
stringstream str;
str << binaryBaseName << " " << getName()
<< " [OPTIONS] <INDEX> <SUBINDEX>" << endl
<< endl
<< getBriefDescription() << endl
<< endl
<< "This command requires a single slave to be selected." << endl
<< endl
<< "The data type of the SDO entry is taken from the SDO" << endl
<< "dictionary by default. It can be overridden with the" << endl
<< "--type option. If the slave does not support the SDO" << endl
<< "information service or the SDO is not in the dictionary," << endl
<< "the --type option is mandatory." << endl
<< endl
<< typeInfo()
<< endl
<< "Arguments:" << endl
<< " INDEX is the SDO index and must be an unsigned" << endl
<< " 16 bit number." << endl
<< " SUBINDEX is the SDO entry subindex and must be an" << endl
<< " unsigned 8 bit number." << endl
<< endl
<< "Command-specific options:" << endl
<< " --alias -a <alias>" << endl
<< " --position -p <pos> Slave selection. See the help of" << endl
<< " the 'slaves' command." << endl
<< " --type -t <type> SDO entry data type (see above)." << endl
<< endl
<< numericInfo();
return str.str();
}
/****************************************************************************/
void CommandUpload::execute(const StringVector &args)
{
SlaveList slaves;
stringstream err, strIndex, strSubIndex;
ec_ioctl_slave_sdo_upload_t data;
const DataType *dataType = NULL;
unsigned int uval;
if (args.size() != 2) {
err << "'" << getName() << "' takes two arguments!";
throwInvalidUsageException(err);
}
strIndex << args[0];
strIndex
>> resetiosflags(ios::basefield) // guess base from prefix
>> data.sdo_index;
if (strIndex.fail()) {
err << "Invalid SDO index '" << args[0] << "'!";
throwInvalidUsageException(err);
}
strSubIndex << args[1];
strSubIndex
>> resetiosflags(ios::basefield) // guess base from prefix
>> uval;
if (strSubIndex.fail() || uval > 0xff) {
err << "Invalid SDO subindex '" << args[1] << "'!";
throwInvalidUsageException(err);
}
data.sdo_entry_subindex = uval;
MasterDevice m(getSingleMasterIndex());
m.open(MasterDevice::Read);
slaves = selectedSlaves(m);
if (slaves.size() != 1) {
throwSingleSlaveRequired(slaves.size());
}
data.slave_position = slaves.front().position;
if (!getDataType().empty()) { // data type specified
if (!(dataType = findDataType(getDataType()))) {
err << "Invalid data type '" << getDataType() << "'!";
throwInvalidUsageException(err);
}
} else { // no data type specified: fetch from dictionary
ec_ioctl_slave_sdo_entry_t entry;
try {
m.getSdoEntry(&entry, data.slave_position,
data.sdo_index, data.sdo_entry_subindex);
} catch (MasterDeviceException &e) {
err << "Failed to determine SDO entry data type. "
<< "Please specify --type.";
throwCommandException(err);
}
if (!(dataType = findDataType(entry.data_type))) {
err << "PDO entry has unknown data type 0x"
<< hex << setfill('0') << setw(4) << entry.data_type << "!"
<< " Please specify --type.";
throwCommandException(err);
}
}
if (dataType->byteSize) {
data.target_size = dataType->byteSize;
} else {
data.target_size = DefaultBufferSize;
}
data.target = new uint8_t[data.target_size + 1];
try {
m.sdoUpload(&data);
} catch (MasterDeviceSdoAbortException &e) {
delete [] data.target;
err << "SDO transfer aborted with code 0x"
<< setfill('0') << hex << setw(8) << e.abortCode
<< ": " << abortText(e.abortCode);
throwCommandException(err);
} catch (MasterDeviceException &e) {
delete [] data.target;
throw e;
}
m.close();
try {
outputData(cout, dataType, data.target, data.data_size);
} catch (SizeException &e) {
delete [] data.target;
throwCommandException(e.what());
}
/*Added by Oscar Caravaca 14/05/2015*/
if(args[0] == "0x201B")
{
convertToIntegerDataTargetBuffer(data.target, data.data_size);
}
else
{
convertToIntegerDataTarget(data.target, data.data_size);
}
delete [] data.target;
}
/*****************************************************************************/
/**
* Added by Oscar Caravaca 14/05/2015.
* This method convert the array data.target to integer number representation.
* @param const uint8_t *data_target: array of char which contains the output value.
* @param int data_size: size of the array.
* @return void: change the globa variable regValue.
*/
void CommandUpload::convertToIntegerDataTarget(const uint8_t *data_target, int data_size)
{
int temp = 0;
for(int i=0;i < data_size ; i++)
{
temp = temp + int(data_target[i]<<(i*8));
}
regValue = temp;
}
/*****************************************************************************/
/**
* Added by Oscar Caravaca 14/05/2015.
* This method convert the array data.target to integer number representation.
* @param const uint8_t *data_target: array of char which contains the output value.
* @param int data_size: size of the array.
* @return void: change the globa variable regValue.
*/
void CommandUpload::convertToIntegerDataTargetBuffer(const uint8_t *data_target, int data_size)
{
int temp;
char val[2];
dataBuffer.clear();
// dataBuffer.clear();
cout << "Buffer data_size: "<<data_size<<endl;
for(int d=0;d < data_size ; d=d+4)
{
temp = 0;
for(int i=d;i < (d+2) ; i++)
{
temp = temp + int(data_target[i]<<(i*8));
}
sprintf(val, "%d",temp);
dataBuffer.push_back(val);
}
}
/*****************************************************************************/
/**
* Added by Oscar Caravaca 14/05/2015.
* This method return the value of the global variable regValue which containt the date returned by upload command.
* @param no parameters.
* @return int: retunr the value of the global regValue.
*/
int CommandUpload::getRegValue()
{
return regValue;
}
/*****************************************************************************/
/**
* Added by Oscar Caravaca 14/05/2015.
* This method return the value of the global variable dataBuffer which containt the date returned by upload command.
* @param no parameters.
* @return int: return the value of the global dataBuffer.
*/
Command::StringVector CommandUpload::getRegValueBuffer()
{
return dataBuffer;
}