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@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@1142: string CommandUpload::helpString() const fp@1142: { fp@1142: stringstream str; fp@1142: fp@1142: str << getName() << " [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@1157: << 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@1327: << "These are the valid SDO entry data types:" << endl fp@1334: << " int8, int16, int32, uint8, uint16, uint32, string," << endl fp@1334: << " octet_string." << endl 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@1142: void CommandUpload::execute(MasterDevice &m, 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@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@1142: try { fp@1142: m.sdoUpload(&data); fp@1184: } 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@1142: } catch (MasterDeviceException &e) { fp@1142: delete [] data.target; fp@1142: throw e; fp@1142: } fp@1142: fp@1142: m.close(); fp@1142: fp@1142: if (dataType->byteSize && data.data_size != dataType->byteSize) { fp@1142: err << "Data type mismatch. Expected " << dataType->name fp@1142: << " with " << dataType->byteSize << " byte, but got " fp@1142: << data.data_size << " byte."; fp@1142: throwCommandException(err); fp@1142: } fp@1142: fp@1142: cout << setfill('0'); fp@1142: switch (dataType->coeCode) { fp@1142: case 0x0002: // int8 fp@1393: { fp@1393: int val = (int) *data.target; fp@1393: cout << "0x" << hex << setw(2) << val fp@1393: << " " << dec << val << endl; fp@1393: } fp@1142: break; fp@1142: case 0x0003: // int16 fp@1393: { fp@1393: int16_t val = le16_to_cpup(data.target); fp@1393: cout << "0x" << hex << setw(4) << val fp@1393: << " " << dec << val << endl; fp@1393: } fp@1142: break; fp@1142: case 0x0004: // int32 fp@1393: { fp@1393: int32_t val = le32_to_cpup(data.target); fp@1393: cout << "0x" << hex << setw(8) << val fp@1393: << " " << dec << val << endl; fp@1393: } fp@1142: break; fp@1142: case 0x0005: // uint8 fp@1393: { fp@1393: unsigned int val = (unsigned int) *data.target; fp@1393: cout << "0x" << hex << setw(2) << val fp@1393: << " " << dec << val << endl; fp@1393: } fp@1142: break; fp@1142: case 0x0006: // uint16 fp@1393: { fp@1393: uint16_t val = le16_to_cpup(data.target); fp@1393: cout << "0x" << hex << setw(4) << val fp@1393: << " " << dec << val << endl; fp@1393: } fp@1142: break; fp@1142: case 0x0007: // uint32 fp@1393: { fp@1393: uint32_t val = le32_to_cpup(data.target); fp@1393: cout << "0x" << hex << setw(8) << val fp@1393: << " " << dec << val << endl; fp@1393: } fp@1142: break; fp@1142: case 0x0009: // string fp@1142: cout << string((const char *) data.target, data.data_size) fp@1142: << endl; fp@1142: break; fp@1334: case 0x000a: // octet_string fp@1334: cout << string((const char *) data.target, data.data_size) fp@1334: << endl; fp@1334: break; fp@1142: default: fp@1142: printRawData(data.target, data.data_size); // FIXME fp@1142: break; fp@1142: } fp@1142: fp@1142: delete [] data.target; fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1142: void CommandUpload::printRawData( fp@1142: const uint8_t *data, fp@1142: unsigned int size fp@1142: ) fp@1142: { fp@1142: cout << hex << setfill('0'); fp@1142: while (size--) { fp@1142: cout << "0x" << setw(2) << (unsigned int) *data++; fp@1142: if (size) fp@1142: cout << " "; fp@1142: } fp@1142: cout << endl; fp@1142: } fp@1142: fp@1142: /*****************************************************************************/