tool/CommandSoeRead.cpp
changeset 2011 04fdb4abf120
parent 1968 4f682084c643
equal deleted inserted replaced
2010:87de63b19e4c 2011:04fdb4abf120
    26  *  industrial property and similar rights of Beckhoff Automation GmbH.
    26  *  industrial property and similar rights of Beckhoff Automation GmbH.
    27  *
    27  *
    28  ****************************************************************************/
    28  ****************************************************************************/
    29 
    29 
    30 #include <iostream>
    30 #include <iostream>
       
    31 #include <iomanip>
    31 using namespace std;
    32 using namespace std;
    32 
    33 
    33 #include "CommandSoeRead.h"
    34 #include "CommandSoeRead.h"
    34 #include "MasterDevice.h"
    35 #include "MasterDevice.h"
    35 
    36 
    36 /*****************************************************************************/
    37 /*****************************************************************************/
    37 
    38 
    38 CommandSoeRead::CommandSoeRead():
    39 CommandSoeRead::CommandSoeRead():
    39     SoeCommand("soe_read", "Read an SoE IDN from a slave.")
    40     Command("soe_read", "Read an SoE IDN from a slave.")
    40 {
    41 {
    41 }
    42 }
    42 
    43 
    43 /*****************************************************************************/
    44 /*****************************************************************************/
    44 
    45 
    45 string CommandSoeRead::helpString() const
    46 string CommandSoeRead::helpString(const string &binaryBaseName) const
    46 {
    47 {
    47     stringstream str;
    48     stringstream str;
    48 
    49 
    49     str << getName() << " [OPTIONS] <IDN>" << endl
    50     str << binaryBaseName << " " << getName()
       
    51         << " [OPTIONS] <IDN>" << endl
       
    52         << binaryBaseName << " " << getName()
       
    53         << " [OPTIONS] <DRIVE> <IDN>" << endl
    50         << endl
    54         << endl
    51         << getBriefDescription() << endl
    55         << getBriefDescription() << endl
    52         << endl
    56         << endl
    53         << "This command requires a single slave to be selected." << endl
    57         << "This command requires a single slave to be selected." << endl
    54         << endl
    58         << endl
    55         << "Arguments:" << endl
    59         << "Arguments:" << endl
       
    60         << "  DRIVE    is the drive number (0 - 7). If omitted, 0 is assumed."
       
    61         << endl
    56         << "  IDN      is the IDN and must be either an unsigned" << endl
    62         << "  IDN      is the IDN and must be either an unsigned" << endl
    57         << "           16 bit number acc. to IEC 61800-7-204:" << endl
    63         << "           16 bit number acc. to IEC 61800-7-204:" << endl
    58         << "             Bit 15: (0) Standard data, (1) Product data" << endl
    64         << "             Bit 15: (0) Standard data, (1) Product data" << endl
    59         << "             Bit 14 - 12: Parameter set (0 - 7)" << endl
    65         << "             Bit 14 - 12: Parameter set (0 - 7)" << endl
    60         << "             Bit 11 - 0: Data block number" << endl
    66         << "             Bit 11 - 0: Data block number" << endl
    82 {
    88 {
    83     SlaveList slaves;
    89     SlaveList slaves;
    84     stringstream err;
    90     stringstream err;
    85     const DataType *dataType = NULL;
    91     const DataType *dataType = NULL;
    86     ec_ioctl_slave_soe_read_t ioctl;
    92     ec_ioctl_slave_soe_read_t ioctl;
       
    93     int driveArgIndex = -1, idnArgIndex = -1;
    87 
    94 
    88     if (args.size() != 1) {
    95     if (args.size() == 1) {
    89         err << "'" << getName() << "' takes one argument!";
    96         idnArgIndex = 0;
       
    97     } else if (args.size() == 2) {
       
    98         driveArgIndex = 0;
       
    99         idnArgIndex = 1;
       
   100     } else {
       
   101         err << "'" << getName() << "' takes eiter 1 or 2 arguments!";
    90         throwInvalidUsageException(err);
   102         throwInvalidUsageException(err);
    91     }
   103     }
    92 
   104 
    93     ioctl.drive_no = 0; // FIXME
   105     if (driveArgIndex >= 0) {
       
   106         stringstream str;
       
   107         unsigned int number;
       
   108         str << args[driveArgIndex];
       
   109         str
       
   110             >> resetiosflags(ios::basefield) // guess base from prefix
       
   111             >> number;
       
   112         if (str.fail() || number > 7) {
       
   113             err << "Invalid drive number '" << args[driveArgIndex] << "'!";
       
   114             throwInvalidUsageException(err);
       
   115         }
       
   116         ioctl.drive_no = number;
       
   117     } else {
       
   118         ioctl.drive_no = 0;
       
   119     }
    94 
   120 
    95     try {
   121     try {
    96         ioctl.idn = parseIdn(args[0]);
   122         ioctl.idn = parseIdn(args[idnArgIndex]);
    97     } catch (runtime_error &e) {
   123     } catch (runtime_error &e) {
    98         err << "Invalid IDN '" << args[0] << "': " << e.what();
   124         err << "Invalid IDN '" << args[idnArgIndex] << "': " << e.what();
    99         throwInvalidUsageException(err);
   125         throwInvalidUsageException(err);
   100     }
   126     }
   101 
   127 
   102     MasterDevice m(getSingleMasterIndex());
   128     MasterDevice m(getSingleMasterIndex());
   103     m.open(MasterDevice::Read);
   129     m.open(MasterDevice::Read);