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@1831: #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@1831: Command("soe_read", "Read an SoE IDN from a slave.") fp@1831: { fp@1831: } fp@1831: fp@1831: /*****************************************************************************/ fp@1831: fp@1831: string CommandSoeRead::helpString() const fp@1831: { fp@1831: stringstream str; fp@1831: fp@1831: str << getName() << " [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@1831: << " IDN is the IDN and must be an unsigned" << endl fp@1831: << " 16 bit number." << endl 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@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@1831: stringstream err, strIdn; fp@1835: const DataType *dataType = NULL; fp@1837: ec_ioctl_slave_soe_read_t ioctl; fp@1831: fp@1831: if (args.size() != 1) { fp@1831: err << "'" << getName() << "' takes one argument!"; fp@1831: throwInvalidUsageException(err); fp@1831: } fp@1831: fp@1831: strIdn << args[0]; fp@1831: strIdn fp@1831: >> resetiosflags(ios::basefield) // guess base from prefix fp@1837: >> ioctl.idn; fp@1831: if (strIdn.fail()) { fp@1831: err << "Invalid IDN '" << args[0] << "'!"; fp@1831: throwInvalidUsageException(err); fp@1831: } fp@1831: fp@1831: if (getMasterIndices().size() != 1) { fp@1831: err << getName() << " requires to select a single master!"; fp@1831: throwInvalidUsageException(err); fp@1831: } fp@1831: MasterDevice m(getMasterIndices().front()); 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@1832: err << "SoE read command aborted with code 0x" fp@1831: << setfill('0') << hex << setw(4) << 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: /*****************************************************************************/