fp@1837: /***************************************************************************** fp@1837: * fp@1837: * $Id$ fp@1837: * fp@1837: * Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH fp@1837: * fp@1837: * This file is part of the IgH EtherCAT Master. fp@1837: * fp@1837: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1837: * modify it under the terms of the GNU General Public License version 2, as fp@1837: * published by the Free Software Foundation. fp@1837: * fp@1837: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1837: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1837: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1837: * Public License for more details. fp@1837: * fp@1837: * You should have received a copy of the GNU General Public License along fp@1837: * with the IgH EtherCAT Master; if not, write to the Free Software fp@1837: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1837: * fp@1837: * --- fp@1837: * fp@1837: * The license mentioned above concerns the source code only. Using the fp@1837: * EtherCAT technology and brand is only permitted in compliance with the fp@1837: * industrial property and similar rights of Beckhoff Automation GmbH. fp@1837: * fp@1837: ****************************************************************************/ fp@1837: fp@1837: #include fp@1968: #include fp@1837: using namespace std; fp@1837: fp@1837: #include "CommandSoeWrite.h" fp@1837: #include "MasterDevice.h" fp@1837: fp@1837: /*****************************************************************************/ fp@1837: fp@1837: CommandSoeWrite::CommandSoeWrite(): fp@1966: Command("soe_write", "Write an SoE IDN to a slave.") fp@1837: { fp@1837: } fp@1837: fp@1837: /*****************************************************************************/ fp@1837: fp@1968: string CommandSoeWrite::helpString(const string &binaryBaseName) const fp@1837: { fp@1837: stringstream str; fp@1837: fp@1968: str << binaryBaseName << " " << getName() fp@1968: << " [OPTIONS] " << endl fp@1968: << binaryBaseName << " " << getName() fp@1968: << " [OPTIONS] " << endl fp@1837: << endl fp@1837: << getBriefDescription() << endl fp@1837: << endl fp@1837: << "This command requires a single slave to be selected." << endl fp@1837: << endl fp@1837: << "Arguments:" << endl fp@1968: << " DRIVE is the drive number (0 - 7). If omitted, 0 is assumed." fp@1968: << endl fp@1868: << " IDN is the IDN and must be either an unsigned" << endl fp@1868: << " 16 bit number acc. to IEC 61800-7-204:" << endl fp@1868: << " Bit 15: (0) Standard data, (1) Product data" << endl fp@1868: << " Bit 14 - 12: Parameter set (0 - 7)" << endl fp@1868: << " Bit 11 - 0: Data block number" << endl fp@1868: << " or a string like 'P-0-150'." << endl fp@1872: << " VALUE is the value to write (see below)." << endl fp@1868: << endl fp@1872: << "The VALUE argument is interpreted as the given data type" << endl fp@1872: << "(--type is mandatory) and written to the selected slave." << endl fp@1872: << endl fp@1868: << typeInfo() fp@1837: << endl fp@1837: << "Command-specific options:" << endl fp@1837: << " --alias -a " << endl fp@1837: << " --position -p Slave selection. See the help of" << endl fp@1837: << " the 'slaves' command." << endl fp@1837: << " --type -t Data type (see above)." << endl fp@1837: << endl fp@1837: << numericInfo(); fp@1837: fp@1837: return str.str(); fp@1837: } fp@1837: fp@1837: /****************************************************************************/ fp@1837: fp@1837: void CommandSoeWrite::execute(const StringVector &args) fp@1837: { fp@1968: stringstream err; fp@1837: const DataType *dataType = NULL; fp@1837: ec_ioctl_slave_soe_write_t ioctl; fp@1837: SlaveList slaves; fp@1837: size_t memSize; fp@1968: int driveArgIndex = -1, idnArgIndex = -1, valueArgIndex = -1; fp@1837: fp@1968: if (args.size() == 2) { fp@1968: idnArgIndex = 0; fp@1968: valueArgIndex = 1; fp@1968: } else if (args.size() == 3) { fp@1968: driveArgIndex = 0; fp@1968: idnArgIndex = 1; fp@1968: valueArgIndex = 2; fp@1968: } else { fp@1968: err << "'" << getName() << "' takes eiter 2 or 3 arguments!"; fp@1837: throwInvalidUsageException(err); fp@1837: } fp@1837: fp@1968: if (driveArgIndex >= 0) { fp@1968: stringstream str; fp@1968: unsigned int number; fp@1968: str << args[driveArgIndex]; fp@1968: str fp@1968: >> resetiosflags(ios::basefield) // guess base from prefix fp@1968: >> number; fp@1968: if (str.fail() || number > 7) { fp@1968: err << "Invalid drive number '" << args[driveArgIndex] << "'!"; fp@1968: throwInvalidUsageException(err); fp@1968: } fp@1968: ioctl.drive_no = number; fp@1968: } else { fp@1968: ioctl.drive_no = 0; fp@1968: } fp@1953: fp@1868: try { fp@1968: ioctl.idn = parseIdn(args[idnArgIndex]); fp@1868: } catch (runtime_error &e) { fp@1968: err << "Invalid IDN '" << args[idnArgIndex] << "': " << e.what(); fp@1837: throwInvalidUsageException(err); fp@1837: } fp@1837: fp@1870: MasterDevice m(getSingleMasterIndex()); fp@1837: m.open(MasterDevice::ReadWrite); fp@1837: slaves = selectedSlaves(m); fp@1837: if (slaves.size() != 1) { fp@1837: throwSingleSlaveRequired(slaves.size()); fp@1837: } fp@1837: ioctl.slave_position = slaves.front().position; fp@1837: fp@1837: if (!getDataType().empty()) { // data type specified fp@1837: if (!(dataType = findDataType(getDataType()))) { fp@1837: err << "Invalid data type '" << getDataType() << "'!"; fp@1837: throwInvalidUsageException(err); fp@1837: } fp@1837: } else { // no data type specified fp@1837: err << "Please specify a data type."; fp@1837: throwInvalidUsageException(err); // FIXME read from stream fp@1837: } fp@1837: fp@1837: if (dataType->byteSize) { fp@1837: memSize = dataType->byteSize; fp@1837: } else { fp@1837: // guess string type size fp@1968: memSize = args[valueArgIndex].size() + 1; fp@1837: if (!memSize) { fp@1837: err << "Empty argument not allowed."; fp@1837: throwInvalidUsageException(err); fp@1837: } fp@1837: } fp@1837: fp@1867: ioctl.data = new uint8_t[memSize]; fp@1837: fp@1837: try { fp@1837: ioctl.data_size = interpretAsType( fp@1968: dataType, args[valueArgIndex], ioctl.data, memSize); fp@1837: } catch (SizeException &e) { fp@1837: delete [] ioctl.data; fp@1837: throwCommandException(e.what()); fp@1837: } catch (ios::failure &e) { fp@1837: delete [] ioctl.data; fp@1968: err << "Invalid value argument '" << args[valueArgIndex] fp@1837: << "' for type '" << dataType->name << "'!"; fp@1837: throwInvalidUsageException(err); fp@1837: } fp@1837: fp@1837: try { fp@1837: m.writeSoe(&ioctl); fp@1837: } catch (MasterDeviceSoeException &e) { fp@1837: delete [] ioctl.data; fp@1877: err << "SoE write command failed with code " << errorMsg(e.errorCode); fp@1837: throwCommandException(err); fp@1837: } catch (MasterDeviceException &e) { fp@1837: delete [] ioctl.data; fp@1837: throw e; fp@1837: } fp@1837: fp@1837: delete [] ioctl.data; fp@1837: } fp@1837: fp@1837: /*****************************************************************************/