|
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 using namespace std; |
|
33 |
|
34 #include "CommandSoeWrite.h" |
|
35 #include "MasterDevice.h" |
|
36 |
|
37 /*****************************************************************************/ |
|
38 |
|
39 CommandSoeWrite::CommandSoeWrite(): |
|
40 Command("soe_write", "Write an SoE IDN to a slave.") |
|
41 { |
|
42 } |
|
43 |
|
44 /*****************************************************************************/ |
|
45 |
|
46 string CommandSoeWrite::helpString() const |
|
47 { |
|
48 stringstream str; |
|
49 |
|
50 str << getName() << " [OPTIONS] <INDEX> <SUBINDEX> <VALUE>" << endl |
|
51 << endl |
|
52 << getBriefDescription() << endl |
|
53 << endl |
|
54 << "This command requires a single slave to be selected." << endl |
|
55 << endl |
|
56 << "Arguments:" << endl |
|
57 << " IDN is the IDN and must be an unsigned" << endl |
|
58 << " 16 bit number." << endl |
|
59 << " VALUE is the value to write and is interpreted" << endl |
|
60 << " as the given datatype (see above)." << 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 << " --type -t <type> Data type (see above)." << endl |
|
67 << endl |
|
68 << numericInfo(); |
|
69 |
|
70 return str.str(); |
|
71 } |
|
72 |
|
73 /****************************************************************************/ |
|
74 |
|
75 void CommandSoeWrite::execute(const StringVector &args) |
|
76 { |
|
77 stringstream strIdn, err; |
|
78 const DataType *dataType = NULL; |
|
79 ec_ioctl_slave_soe_write_t ioctl; |
|
80 SlaveList slaves; |
|
81 size_t memSize; |
|
82 |
|
83 if (args.size() != 2) { |
|
84 err << "'" << getName() << "' takes 2 arguments!"; |
|
85 throwInvalidUsageException(err); |
|
86 } |
|
87 |
|
88 strIdn << args[0]; |
|
89 strIdn |
|
90 >> resetiosflags(ios::basefield) // guess base from prefix |
|
91 >> ioctl.idn; |
|
92 if (strIdn.fail()) { |
|
93 err << "Invalid IDN '" << args[0] << "'!"; |
|
94 throwInvalidUsageException(err); |
|
95 } |
|
96 |
|
97 if (getMasterIndices().size() != 1) { |
|
98 err << getName() << " requires to select a single master!"; |
|
99 throwInvalidUsageException(err); |
|
100 } |
|
101 MasterDevice m(getMasterIndices().front()); |
|
102 m.open(MasterDevice::ReadWrite); |
|
103 slaves = selectedSlaves(m); |
|
104 if (slaves.size() != 1) { |
|
105 throwSingleSlaveRequired(slaves.size()); |
|
106 } |
|
107 ioctl.slave_position = slaves.front().position; |
|
108 |
|
109 if (!getDataType().empty()) { // data type specified |
|
110 if (!(dataType = findDataType(getDataType()))) { |
|
111 err << "Invalid data type '" << getDataType() << "'!"; |
|
112 throwInvalidUsageException(err); |
|
113 } |
|
114 } else { // no data type specified |
|
115 err << "Please specify a data type."; |
|
116 throwInvalidUsageException(err); // FIXME read from stream |
|
117 } |
|
118 |
|
119 if (dataType->byteSize) { |
|
120 memSize = dataType->byteSize; |
|
121 } else { |
|
122 // guess string type size |
|
123 memSize = args[1].size(); |
|
124 if (!memSize) { |
|
125 err << "Empty argument not allowed."; |
|
126 throwInvalidUsageException(err); |
|
127 } |
|
128 } |
|
129 |
|
130 ioctl.data = new uint8_t[memSize + 1]; |
|
131 |
|
132 try { |
|
133 ioctl.data_size = interpretAsType( |
|
134 dataType, args[1], ioctl.data, memSize); |
|
135 } catch (SizeException &e) { |
|
136 delete [] ioctl.data; |
|
137 throwCommandException(e.what()); |
|
138 } catch (ios::failure &e) { |
|
139 delete [] ioctl.data; |
|
140 err << "Invalid value argument '" << args[1] |
|
141 << "' for type '" << dataType->name << "'!"; |
|
142 throwInvalidUsageException(err); |
|
143 } |
|
144 |
|
145 try { |
|
146 m.writeSoe(&ioctl); |
|
147 } catch (MasterDeviceSoeException &e) { |
|
148 delete [] ioctl.data; |
|
149 err << "SoE write command aborted with code 0x" |
|
150 << setfill('0') << hex << setw(4) << e.errorCode << "."; |
|
151 throwCommandException(err); |
|
152 } catch (MasterDeviceException &e) { |
|
153 delete [] ioctl.data; |
|
154 throw e; |
|
155 } |
|
156 |
|
157 delete [] ioctl.data; |
|
158 } |
|
159 |
|
160 /*****************************************************************************/ |