fp@1200: /***************************************************************************** fp@1200: * 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@1200: * fp@1200: ****************************************************************************/ fp@1200: fp@1200: #include fp@1200: #include fp@1200: using namespace std; fp@1200: fp@1388: #include "CommandRegRead.h" fp@1200: fp@1200: /*****************************************************************************/ fp@1200: fp@1388: CommandRegRead::CommandRegRead(): fp@1389: CommandReg("reg_read", "Output a slave's register contents.") fp@1200: { fp@1200: } fp@1200: fp@1200: /*****************************************************************************/ fp@1200: fp@1388: string CommandRegRead::helpString() const fp@1200: { fp@1200: stringstream str; fp@1200: fp@1387: str << getName() << " [OPTIONS] [LENGTH]" << endl fp@1200: << endl fp@1200: << getBriefDescription() << endl fp@1200: << endl fp@1200: << "This command requires a single slave to be selected." << endl fp@1200: << endl fp@1200: << "Arguments:" << endl fp@1388: << " OFFSET is the register address. Must" << endl fp@1200: << " be an unsigned 16 bit number." << endl fp@1200: << " LENGTH is the number of bytes to read and must also be" << endl fp@1200: << " an unsigned 16 bit number. OFFSET plus LENGTH" << endl fp@1387: << " may not exceed 64k. The length is ignored (and" << endl fp@1387: << " can be omitted), if a selected data type" << endl fp@1387: << " implies a length." << endl fp@1387: << endl fp@1387: << "These are the valid data types:" << endl fp@1387: << " int8, int16, int32, int64, uint8, uint16, uint32," << endl fp@1389: << " uint64, string, raw." << endl fp@1200: << endl fp@1200: << "Command-specific options:" << endl fp@1200: << " --alias -a " << endl fp@1200: << " --position -p Slave selection. See the help of" << endl fp@1200: << " the 'slaves' command." << endl fp@1387: << " --type -t Data type (see above)." << endl fp@1200: << endl fp@1200: << numericInfo(); fp@1200: fp@1200: return str.str(); fp@1200: } fp@1200: fp@1200: /****************************************************************************/ fp@1200: fp@1388: void CommandRegRead::execute(MasterDevice &m, const StringVector &args) fp@1200: { fp@1200: SlaveList slaves; fp@1388: ec_ioctl_slave_reg_t data; fp@1387: stringstream strOffset, err; fp@1387: const DataType *dataType = NULL; fp@1387: fp@1387: if (args.size() < 1 || args.size() > 2) { fp@1387: err << "'" << getName() << "' takes one or two arguments!"; fp@1200: throwInvalidUsageException(err); fp@1200: } fp@1200: fp@1200: strOffset << args[0]; fp@1200: strOffset fp@1200: >> resetiosflags(ios::basefield) // guess base from prefix fp@1200: >> data.offset; fp@1200: if (strOffset.fail()) { fp@1200: err << "Invalid offset '" << args[0] << "'!"; fp@1200: throwInvalidUsageException(err); fp@1200: } fp@1200: fp@1387: if (args.size() > 1) { fp@1387: stringstream strLength; fp@1387: strLength << args[1]; fp@1387: strLength fp@1387: >> resetiosflags(ios::basefield) // guess base from prefix fp@1387: >> data.length; fp@1387: if (strLength.fail()) { fp@1387: err << "Invalid length '" << args[1] << "'!"; fp@1387: throwInvalidUsageException(err); fp@1387: } fp@1387: fp@1387: if (!data.length) { fp@1387: err << "Length may not be zero!"; fp@1387: throwInvalidUsageException(err); fp@1387: } fp@1387: } else { // no length argument given fp@1387: data.length = 0; fp@1387: } fp@1387: fp@1387: if (!getDataType().empty()) { fp@1387: if (!(dataType = findDataType(getDataType()))) { fp@1387: err << "Invalid data type '" << getDataType() << "'!"; fp@1387: throwInvalidUsageException(err); fp@1387: } fp@1387: fp@1387: if (dataType->byteSize) { fp@1387: // override length argument fp@1387: data.length = dataType->byteSize; fp@1387: } fp@1200: } fp@1200: fp@1200: if (!data.length) { fp@1387: err << "The length argument is mandatory, if no datatype is " << endl fp@1387: << "specified, or the datatype does not imply a length!"; fp@1387: throwInvalidUsageException(err); fp@1200: } fp@1200: fp@1200: if ((uint32_t) data.offset + data.length > 0xffff) { fp@1200: err << "Offset and length exceeding 64k!"; fp@1200: throwInvalidUsageException(err); fp@1200: } fp@1200: fp@1200: m.open(MasterDevice::Read); fp@1200: slaves = selectedSlaves(m); fp@1200: fp@1200: if (slaves.size() != 1) { fp@1200: throwSingleSlaveRequired(slaves.size()); fp@1200: } fp@1200: data.slave_position = slaves.front().position; fp@1200: fp@1200: data.data = new uint8_t[data.length]; fp@1200: fp@1200: try { fp@1388: m.readReg(&data); fp@1200: } catch (MasterDeviceException &e) { fp@1200: delete [] data.data; fp@1200: throw e; fp@1200: } fp@1200: fp@1387: cout << setfill('0'); fp@1431: if (!dataType || string(dataType->name) == "string") { fp@1387: uint16_t i; fp@1387: for (i = 0; i < data.length; i++) { fp@1387: cout << data.data[i]; fp@1387: } fp@1389: if (dataType) fp@1389: cout << endl; fp@1431: } else if (string(dataType->name) == "int8") { fp@1393: int val = (int) *data.data; fp@1393: cout << "0x" << hex << setw(2) << val << " " << dec << val << endl; fp@1431: } else if (string(dataType->name) == "int16") { fp@1393: int16_t val = le16_to_cpup(data.data); fp@1393: cout << "0x" << hex << setw(4) << val << " " << dec << val << endl; fp@1431: } else if (string(dataType->name) == "int32") { fp@1393: int32_t val = le32_to_cpup(data.data); fp@1393: cout << "0x" << hex << setw(8) << val << " " << dec << val << endl; fp@1431: } else if (string(dataType->name) == "int64") { fp@1393: int64_t val = le64_to_cpup(data.data); fp@1393: cout << "0x" << hex << setw(16) << val << " " << dec << val << endl; fp@1431: } else if (string(dataType->name) == "uint8") { fp@1393: unsigned int val = (unsigned int) *data.data; fp@1393: cout << "0x" << hex << setw(2) << val << " " << dec << val << endl; fp@1431: } else if (string(dataType->name) == "uint16") { fp@1393: uint16_t val = le16_to_cpup(data.data); fp@1393: cout << "0x" << hex << setw(4) << val << " " << dec << val << endl; fp@1431: } else if (string(dataType->name) == "uint32") { fp@1393: uint32_t val = le32_to_cpup(data.data); fp@1393: cout << "0x" << hex << setw(8) << val << " " << dec << val << endl; fp@1431: } else if (string(dataType->name) == "uint64") { fp@1393: uint64_t val = le64_to_cpup(data.data); fp@1393: cout << "0x" << hex << setw(16) << val << " " << dec << val << endl; fp@1389: } else { // raw fp@1387: uint8_t *d = data.data; fp@1387: unsigned int size = data.length; fp@1387: fp@1387: cout << hex << setfill('0'); fp@1387: while (size--) { fp@1387: cout << "0x" << setw(2) << (unsigned int) *d++; fp@1387: if (size) fp@1387: cout << " "; fp@1387: } fp@1387: cout << endl; fp@1200: } fp@1200: fp@1200: delete [] data.data; fp@1200: } fp@1200: fp@1200: /*****************************************************************************/