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 "CommandUpload.h" fp@1826: #include "MasterDevice.h" fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: CommandUpload::CommandUpload(): fp@1327: SdoCommand("upload", "Read an SDO entry from a slave.") fp@1142: { fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1968: string CommandUpload::helpString(const string &binaryBaseName) const fp@1142: { fp@1142: stringstream str; fp@1142: fp@1968: str << binaryBaseName << " " << getName() fp@1968: << " [OPTIONS] " << endl fp@1142: << endl fp@1142: << getBriefDescription() << endl fp@1142: << endl fp@1157: << "This command requires a single slave to be selected." << endl fp@1804: << endl fp@1327: << "The data type of the SDO entry is taken from the SDO" << endl fp@1142: << "dictionary by default. It can be overridden with the" << endl fp@1327: << "--type option. If the slave does not support the SDO" << endl fp@1327: << "information service or the SDO is not in the dictionary," << endl fp@1142: << "the --type option is mandatory." << endl fp@1142: << endl fp@1868: << typeInfo() fp@1142: << endl fp@1142: << "Arguments:" << endl fp@1327: << " INDEX is the SDO index and must be an unsigned" << endl fp@1142: << " 16 bit number." << endl fp@1327: << " SUBINDEX is the SDO entry subindex and must be an" << endl fp@1142: << " unsigned 8 bit number." << endl fp@1142: << endl fp@1142: << "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@1327: << " --type -t SDO entry data type (see above)." << endl fp@1142: << endl fp@1142: << numericInfo(); fp@1142: fp@1142: return str.str(); fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1826: void CommandUpload::execute(const StringVector &args) fp@1142: { fp@1151: SlaveList slaves; fp@1142: stringstream err, strIndex, strSubIndex; fp@1142: ec_ioctl_slave_sdo_upload_t data; fp@1393: const DataType *dataType = NULL; fp@1142: unsigned int uval; fp@1142: fp@1142: if (args.size() != 2) { fp@1142: err << "'" << getName() << "' takes two arguments!"; fp@1142: throwInvalidUsageException(err); fp@1142: } 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@1142: strSubIndex << args[1]; fp@1142: strSubIndex fp@1142: >> resetiosflags(ios::basefield) // guess base from prefix fp@1142: >> uval; fp@1142: if (strSubIndex.fail() || uval > 0xff) { fp@1327: err << "Invalid SDO subindex '" << args[1] << "'!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: data.sdo_entry_subindex = uval; fp@1142: fp@1870: MasterDevice m(getSingleMasterIndex()); fp@1151: m.open(MasterDevice::Read); 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@1142: if (dataType->byteSize) { fp@1142: data.target_size = dataType->byteSize; fp@1142: } else { fp@1142: data.target_size = DefaultBufferSize; fp@1142: } fp@1142: fp@1142: data.target = new uint8_t[data.target_size + 1]; fp@1142: fp@1804: try { fp@1804: m.sdoUpload(&data); fp@1804: } catch (MasterDeviceSdoAbortException &e) { fp@1184: delete [] data.target; 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.target; fp@1142: throw e; fp@1142: } fp@1142: fp@1142: m.close(); fp@1142: fp@1835: try { fp@1835: outputData(cout, dataType, data.target, data.data_size); fp@1835: } catch (SizeException &e) { fp@1835: delete [] data.target; fp@1835: throwCommandException(e.what()); fp@1142: } fp@1142: fp@1142: delete [] data.target; fp@1142: } fp@1142: fp@1142: /*****************************************************************************/