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@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@1142: string CommandDownload::helpString() const fp@1142: { fp@1142: stringstream str; fp@1142: fp@1142: str << getName() << " [OPTIONS] " << endl fp@1142: << endl fp@1142: << getBriefDescription() << endl fp@1157: << endl fp@1157: << "This command requires a single slave to be selected." << endl fp@1142: << 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@1144: << "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: << " VALUE is the value to download and must correspond" << endl fp@1327: << " to the SDO entry datatype (see above)." << 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 CommandDownload::execute(MasterDevice &m, const StringVector &args) fp@1142: { fp@1142: stringstream strIndex, strSubIndex, strValue, err; fp@1142: ec_ioctl_slave_sdo_download_t data; fp@1142: unsigned int number; fp@1184: const DataType *dataType = NULL; fp@1151: SlaveList slaves; fp@1142: fp@1142: if (args.size() != 3) { fp@1142: err << "'" << getName() << "' takes 3 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: >> number; fp@1142: if (strSubIndex.fail() || number > 0xff) { fp@1327: err << "Invalid SDO subindex '" << args[1] << "'!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: data.sdo_entry_subindex = number; fp@1142: 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@1142: if (dataType->byteSize) { fp@1142: data.data_size = dataType->byteSize; fp@1142: } else { fp@1142: data.data_size = DefaultBufferSize; fp@1142: } fp@1142: fp@1142: data.data = new uint8_t[data.data_size + 1]; fp@1142: fp@1142: strValue << args[2]; fp@1142: strValue >> resetiosflags(ios::basefield); // guess base from prefix fp@1142: strValue.exceptions(ios::failbit); fp@1142: fp@1142: try { fp@1142: switch (dataType->coeCode) { fp@1142: case 0x0002: // int8 fp@1142: { fp@1142: int16_t val; // uint8_t is interpreted as char fp@1142: strValue >> val; fp@1142: if (val > 127 || val < -128) fp@1142: throw ios::failure("Value out of range"); fp@1142: *data.data = val; fp@1142: break; fp@1142: } fp@1142: case 0x0003: // int16 fp@1142: { fp@1142: int16_t val; fp@1142: strValue >> val; fp@1254: *(int16_t *) data.data = cpu_to_le16(val); fp@1142: break; fp@1142: } fp@1142: case 0x0004: // int32 fp@1142: { fp@1142: int32_t val; fp@1142: strValue >> val; fp@1254: *(int32_t *) data.data = cpu_to_le32(val); fp@1142: break; fp@1142: } fp@1142: case 0x0005: // uint8 fp@1142: { fp@1142: uint16_t val; // uint8_t is interpreted as char fp@1142: strValue >> val; fp@1142: if (val > 0xff) fp@1142: throw ios::failure("Value out of range"); fp@1142: *data.data = val; fp@1142: break; fp@1142: } fp@1142: case 0x0006: // uint16 fp@1142: { fp@1142: uint16_t val; fp@1142: strValue >> val; fp@1254: *(uint16_t *) data.data = cpu_to_le16(val); fp@1142: break; fp@1142: } fp@1142: case 0x0007: // uint32 fp@1142: { fp@1142: uint32_t val; fp@1142: strValue >> val; fp@1254: *(uint32_t *) data.data = cpu_to_le32(val); fp@1142: break; fp@1142: } fp@1142: case 0x0009: // string fp@1142: if (strValue.str().size() >= data.data_size) { fp@1142: err << "String too large"; fp@1142: throwCommandException(err); fp@1142: } fp@1142: data.data_size = strValue.str().size(); fp@1142: strValue >> (char *) data.data; fp@1142: break; fp@1334: case 0x000a: // octet_string fp@1334: if (strValue.str().size() >= data.data_size) { fp@1334: err << "String too large"; fp@1334: throwCommandException(err); fp@1334: } fp@1334: data.data_size = strValue.str().size(); fp@1334: strValue >> (char *) data.data; fp@1334: break; fp@1142: fp@1142: default: fp@1142: delete [] data.data; fp@1142: err << "Unknown data type 0x" << hex << dataType->coeCode; fp@1142: throwCommandException(err); fp@1142: } 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@1142: try { fp@1151: m.sdoDownload(&data); fp@1184: } 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@1142: } 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: /*****************************************************************************/