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@1837: #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@1868: SoeCommand("soe_write", "Write an SoE IDN to a slave.") fp@1837: { fp@1837: } fp@1837: fp@1837: /*****************************************************************************/ fp@1837: fp@1837: string CommandSoeWrite::helpString() const fp@1837: { fp@1837: stringstream str; fp@1837: fp@1868: str << getName() << " [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@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@1837: << " VALUE is the value to write and is interpreted" << endl fp@1837: << " as the given datatype (see above)." << endl fp@1868: << 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@1837: stringstream strIdn, 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@1837: fp@1837: if (args.size() != 2) { fp@1837: err << "'" << getName() << "' takes 2 arguments!"; fp@1837: throwInvalidUsageException(err); fp@1837: } fp@1837: fp@1868: try { fp@1868: ioctl.idn = parseIdn(args[0]); fp@1868: } catch (runtime_error &e) { fp@1868: err << "Invalid IDN '" << args[0] << "': " << 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@1867: memSize = args[1].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@1837: dataType, args[1], 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@1837: err << "Invalid value argument '" << args[1] 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@1837: err << "SoE write command aborted with code 0x" fp@1837: << setfill('0') << hex << setw(4) << 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: /*****************************************************************************/