fp@1142: /***************************************************************************** fp@1142: * fp@1363: * $Id$ fp@1363: * fp@1363: * Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH fp@1363: * fp@1363: * This file is part of the IgH EtherCAT Master. fp@1363: * fp@1363: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1363: * modify it under the terms of the GNU General Public License version 2, as fp@1363: * published by the Free Software Foundation. fp@1363: * fp@1363: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1363: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1363: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1363: * Public License for more details. fp@1363: * fp@1363: * You should have received a copy of the GNU General Public License along fp@1363: * with the IgH EtherCAT Master; if not, write to the Free Software fp@1363: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1363: * fp@1363: * --- fp@1363: * fp@1363: * The license mentioned above concerns the source code only. Using the fp@1363: * EtherCAT technology and brand is only permitted in compliance with the fp@1363: * industrial property and similar rights of Beckhoff Automation GmbH. fp@1142: * fp@1142: ****************************************************************************/ fp@1142: fp@1142: #include fp@1142: #include fp@1142: using namespace std; fp@1142: fp@1142: #include "CommandDownload.h" fp@1826: #include "MasterDevice.h" fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: CommandDownload::CommandDownload(): fp@1327: SdoCommand("download", "Write an SDO entry to a slave.") fp@1142: { fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1968: string CommandDownload::helpString(const string &binaryBaseName) const fp@1142: { fp@1142: stringstream str; fp@1142: fp@1968: str << binaryBaseName << " " << getName() fp@1968: << " [OPTIONS] " << endl fp@2124: << " [OPTIONS] " << endl fp@1804: << endl fp@1804: << getBriefDescription() << endl fp@1157: << endl fp@1157: << "This command requires a single slave to be selected." << endl fp@1804: << endl fp@1804: << "The data type of the SDO entry is taken from the SDO" << endl fp@1804: << "dictionary by default. It can be overridden with the" << endl fp@1804: << "--type option. If the slave does not support the SDO" << endl fp@1804: << "information service or the SDO is not in the dictionary," << endl fp@1804: << "the --type option is mandatory." << endl fp@1804: << endl fp@2124: << "The second call (without ) uses the complete" << endl fp@2124: << "access method." << endl fp@2124: << endl fp@1868: << typeInfo() fp@1804: << endl fp@1804: << "Arguments:" << endl fp@1804: << " INDEX is the SDO index and must be an unsigned" << endl fp@1804: << " 16 bit number." << endl fp@1804: << " SUBINDEX is the SDO entry subindex and must be an" << endl fp@1804: << " unsigned 8 bit number." << endl fp@1804: << " VALUE is the value to download and must correspond" << endl fp@2124: << " to the SDO entry datatype (see above). Use" << endl fp@2124: << " '-' to read from standard input." << endl fp@1804: << endl fp@1804: << "Command-specific options:" << endl fp@1157: << " --alias -a " << endl fp@1157: << " --position -p Slave selection. See the help of" << endl fp@1157: << " the 'slaves' command." << endl fp@1804: << " --type -t SDO entry data type (see above)." << endl fp@1804: << endl fp@1804: << numericInfo(); fp@1804: fp@1804: return str.str(); fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1826: void CommandDownload::execute(const StringVector &args) fp@1142: { fp@2124: stringstream strIndex, err; fp@1142: ec_ioctl_slave_sdo_download_t data; fp@2124: unsigned int valueIndex; fp@1184: const DataType *dataType = NULL; fp@1151: SlaveList slaves; fp@1142: fp@2124: if (args.size() != 2 && args.size() != 3) { fp@2124: err << "'" << getName() << "' takes 2 or 3 arguments!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@2124: data.complete_access = args.size() == 2; fp@2124: valueIndex = data.complete_access ? 1 : 2; fp@1142: fp@1142: strIndex << args[0]; fp@1142: strIndex fp@1142: >> resetiosflags(ios::basefield) // guess base from prefix fp@1142: >> data.sdo_index; fp@1142: if (strIndex.fail()) { fp@1327: err << "Invalid SDO index '" << args[0] << "'!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: fp@2124: if (data.complete_access) { fp@2124: data.sdo_entry_subindex = 0; fp@2124: } else { fp@2124: stringstream strSubIndex; fp@2124: unsigned int number; fp@2124: fp@2124: strSubIndex << args[1]; fp@2124: strSubIndex fp@2124: >> resetiosflags(ios::basefield) // guess base from prefix fp@2124: >> number; fp@2124: if (strSubIndex.fail() || number > 0xff) { fp@2124: err << "Invalid SDO subindex '" << args[1] << "'!"; fp@2124: throwInvalidUsageException(err); fp@2124: } fp@2124: data.sdo_entry_subindex = number; fp@2124: } fp@1142: fp@1870: MasterDevice m(getSingleMasterIndex()); fp@1151: m.open(MasterDevice::ReadWrite); fp@1151: slaves = selectedSlaves(m); fp@1151: if (slaves.size() != 1) { fp@1155: throwSingleSlaveRequired(slaves.size()); fp@1151: } fp@1151: data.slave_position = slaves.front().position; fp@1151: fp@1166: if (!getDataType().empty()) { // data type specified fp@1166: if (!(dataType = findDataType(getDataType()))) { fp@1166: err << "Invalid data type '" << getDataType() << "'!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: } else { // no data type specified: fetch from dictionary fp@1142: ec_ioctl_slave_sdo_entry_t entry; fp@1142: fp@1142: try { fp@1151: m.getSdoEntry(&entry, data.slave_position, fp@1142: data.sdo_index, data.sdo_entry_subindex); fp@1142: } catch (MasterDeviceException &e) { fp@1327: err << "Failed to determine SDO entry data type. " fp@1142: << "Please specify --type."; fp@1142: throwCommandException(err); fp@1142: } fp@1142: if (!(dataType = findDataType(entry.data_type))) { fp@1327: err << "PDO entry has unknown data type 0x" fp@1142: << hex << setfill('0') << setw(4) << entry.data_type << "!" fp@1142: << " Please specify --type."; fp@1142: throwCommandException(err); fp@1142: } fp@1142: } fp@1142: fp@2124: if (args[valueIndex] == "-") { fp@2124: ostringstream tmp; fp@2124: fp@2124: tmp << cin.rdbuf(); fp@2124: string const &contents = tmp.str(); fp@2124: fp@2124: if (!contents.size()) { fp@2124: err << "Invalid data size " << contents.size() << "! " fp@2124: << "Must be non-zero."; fp@2124: throwCommandException(err); fp@2124: } fp@2124: data.data_size = contents.size(); fp@2124: data.data = new uint8_t[data.data_size + 1]; fp@2124: fp@2124: try { fp@2124: data.data_size = interpretAsType( fp@2124: dataType, contents, data.data, data.data_size); fp@2124: } catch (SizeException &e) { fp@2124: delete [] data.data; fp@2124: throwCommandException(e.what()); fp@2124: } catch (ios::failure &e) { fp@2124: delete [] data.data; fp@2124: err << "Invalid value argument '" << args[2] fp@2124: << "' for type '" << dataType->name << "'!"; fp@2124: throwInvalidUsageException(err); fp@2124: } fp@2124: fp@1142: } else { fp@2124: if (dataType->byteSize) { fp@2124: data.data_size = dataType->byteSize; fp@2124: } else { fp@2124: data.data_size = DefaultBufferSize; fp@2124: } fp@2124: fp@2124: data.data = new uint8_t[data.data_size + 1]; fp@2124: fp@2124: try { fp@2124: data.data_size = interpretAsType( fp@2124: dataType, args[valueIndex], data.data, data.data_size); fp@2124: } catch (SizeException &e) { fp@2124: delete [] data.data; fp@2124: throwCommandException(e.what()); fp@2124: } catch (ios::failure &e) { fp@2124: delete [] data.data; fp@2124: err << "Invalid value argument '" << args[2] fp@2124: << "' for type '" << dataType->name << "'!"; fp@2124: throwInvalidUsageException(err); fp@2124: } fp@2124: } fp@1142: fp@1142: try { fp@1835: data.data_size = interpretAsType( fp@2124: dataType, args[valueIndex], data.data, data.data_size); fp@1835: } catch (SizeException &e) { fp@1835: delete [] data.data; fp@1835: throwCommandException(e.what()); fp@1142: } catch (ios::failure &e) { fp@1142: delete [] data.data; fp@1142: err << "Invalid value argument '" << args[2] fp@1142: << "' for type '" << dataType->name << "'!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: fp@1804: try { fp@1151: m.sdoDownload(&data); fp@1804: } catch (MasterDeviceSdoAbortException &e) { fp@1184: delete [] data.data; fp@1327: err << "SDO transfer aborted with code 0x" fp@1184: << setfill('0') << hex << setw(8) << e.abortCode fp@1184: << ": " << abortText(e.abortCode); fp@1184: throwCommandException(err); fp@1804: } catch(MasterDeviceException &e) { fp@1142: delete [] data.data; fp@1142: throw e; fp@1142: } fp@1142: fp@1142: delete [] data.data; fp@1142: } fp@1142: fp@1142: /*****************************************************************************/