fp@1831: /***************************************************************************** fp@1831: * fp@1831: * $Id$ fp@1831: * fp@1831: * Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH fp@1831: * fp@1831: * This file is part of the IgH EtherCAT Master. fp@1831: * fp@1831: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1831: * modify it under the terms of the GNU General Public License version 2, as fp@1831: * published by the Free Software Foundation. fp@1831: * fp@1831: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1831: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1831: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1831: * Public License for more details. fp@1831: * fp@1831: * You should have received a copy of the GNU General Public License along fp@1831: * with the IgH EtherCAT Master; if not, write to the Free Software fp@1831: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1831: * fp@1831: * --- fp@1831: * fp@1831: * The license mentioned above concerns the source code only. Using the fp@1831: * EtherCAT technology and brand is only permitted in compliance with the fp@1831: * industrial property and similar rights of Beckhoff Automation GmbH. fp@1831: * fp@1831: ****************************************************************************/ fp@1831: fp@1831: #include fp@1968: #include fp@1831: using namespace std; fp@1831: fp@1831: #include "CommandSoeRead.h" fp@1831: #include "MasterDevice.h" fp@1831: fp@1831: /*****************************************************************************/ fp@1831: fp@1831: CommandSoeRead::CommandSoeRead(): fp@1966: Command("soe_read", "Read an SoE IDN from a slave.") fp@1831: { fp@1831: } fp@1831: fp@1831: /*****************************************************************************/ fp@1831: fp@1968: string CommandSoeRead::helpString(const string &binaryBaseName) const fp@1831: { fp@1831: stringstream str; fp@1831: fp@1968: str << binaryBaseName << " " << getName() fp@1968: << " [OPTIONS] " << endl fp@1968: << binaryBaseName << " " << getName() fp@1968: << " [OPTIONS] " << endl fp@1831: << endl fp@1831: << getBriefDescription() << endl fp@1831: << endl fp@1831: << "This command requires a single slave to be selected." << endl fp@1831: << endl fp@1831: << "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@1868: << endl fp@1872: << "Data of the given IDN are read and displayed according to" << endl fp@1872: << "the given datatype, or as raw hex bytes." << endl fp@1872: << endl fp@1868: << typeInfo() fp@1831: << endl fp@1831: << "Command-specific options:" << endl fp@1831: << " --alias -a " << endl fp@1831: << " --position -p Slave selection. See the help of" << endl fp@1831: << " the 'slaves' command." << endl fp@1868: << " --type -t Data type (see above)." << endl fp@1831: << endl fp@1831: << numericInfo(); fp@1831: fp@1831: return str.str(); fp@1831: } fp@1831: fp@1831: /****************************************************************************/ fp@1831: fp@1831: void CommandSoeRead::execute(const StringVector &args) fp@1831: { fp@1831: SlaveList slaves; fp@1868: stringstream err; fp@1835: const DataType *dataType = NULL; fp@1837: ec_ioctl_slave_soe_read_t ioctl; fp@1968: int driveArgIndex = -1, idnArgIndex = -1; fp@1831: fp@1968: if (args.size() == 1) { fp@1968: idnArgIndex = 0; fp@1968: } else if (args.size() == 2) { fp@1968: driveArgIndex = 0; fp@1968: idnArgIndex = 1; fp@1968: } else { fp@1968: err << "'" << getName() << "' takes eiter 1 or 2 arguments!"; fp@1831: throwInvalidUsageException(err); fp@1831: } fp@1831: 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@1831: throwInvalidUsageException(err); fp@1831: } fp@1831: fp@1870: MasterDevice m(getSingleMasterIndex()); fp@1831: m.open(MasterDevice::Read); fp@1831: slaves = selectedSlaves(m); fp@1831: if (slaves.size() != 1) { fp@1831: throwSingleSlaveRequired(slaves.size()); fp@1831: } fp@1837: ioctl.slave_position = slaves.front().position; fp@1831: fp@1835: if (getDataType().empty()) { fp@1835: dataType = findDataType("raw"); // FIXME fp@1835: } else { // no data type specified fp@1835: if (!(dataType = findDataType(getDataType()))) { fp@1835: err << "Invalid data type '" << getDataType() << "'!"; fp@1835: throwInvalidUsageException(err); fp@1835: } fp@1835: } fp@1835: fp@1835: if (dataType->byteSize) { fp@1837: ioctl.mem_size = dataType->byteSize; fp@1835: } else { fp@1837: ioctl.mem_size = 1024; fp@1835: } fp@1835: fp@1837: ioctl.data = new uint8_t[ioctl.mem_size + 1]; fp@1831: fp@1831: try { fp@1837: m.readSoe(&ioctl); fp@1831: } catch (MasterDeviceSoeException &e) { fp@1837: delete [] ioctl.data; fp@1877: err << "SoE read command failed with code " << errorMsg(e.errorCode); fp@1831: throwCommandException(err); fp@1831: } catch (MasterDeviceException &e) { fp@1837: delete [] ioctl.data; fp@1831: throw e; fp@1831: } fp@1831: fp@1831: m.close(); fp@1831: fp@1835: try { fp@1837: outputData(cout, dataType, ioctl.data, ioctl.data_size); fp@1835: } catch (SizeException &e) { fp@1837: delete [] ioctl.data; fp@1835: throwCommandException(e.what()); fp@1835: } fp@1831: fp@1837: delete [] ioctl.data; fp@1831: } fp@1831: fp@1831: /*****************************************************************************/