tool/CommandRegRead.cpp
changeset 2589 2b9c78543663
parent 1968 4f682084c643
equal deleted inserted replaced
2415:af21f0bdc7c9 2589:2b9c78543663
     1 /*****************************************************************************
     1 /*****************************************************************************
     2  *
     2  *
     3  *  $Id$
     3  *  $Id$
     4  *
     4  *
     5  *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
     5  *  Copyright (C) 2006-2012  Florian Pose, Ingenieurgemeinschaft IgH
     6  *
     6  *
     7  *  This file is part of the IgH EtherCAT Master.
     7  *  This file is part of the IgH EtherCAT Master.
     8  *
     8  *
     9  *  The IgH EtherCAT Master is free software; you can redistribute it and/or
     9  *  The IgH EtherCAT Master is free software; you can redistribute it and/or
    10  *  modify it under the terms of the GNU General Public License version 2, as
    10  *  modify it under the terms of the GNU General Public License version 2, as
    46 string CommandRegRead::helpString(const string &binaryBaseName) const
    46 string CommandRegRead::helpString(const string &binaryBaseName) const
    47 {
    47 {
    48     stringstream str;
    48     stringstream str;
    49 
    49 
    50     str << binaryBaseName << " " << getName()
    50     str << binaryBaseName << " " << getName()
    51         << " [OPTIONS] <OFFSET> [LENGTH]" << endl
    51         << " [OPTIONS] <ADDRESS> [SIZE]" << endl
    52         << endl
    52         << endl
    53         << getBriefDescription() << endl
    53         << getBriefDescription() << endl
    54         << endl
    54         << endl
    55         << "This command requires a single slave to be selected." << endl
    55         << "This command requires a single slave to be selected." << endl
    56         << endl
    56         << endl
    57         << "Arguments:" << endl
    57         << "Arguments:" << endl
    58         << "  OFFSET is the register address. Must" << endl
    58         << "  ADDRESS is the register address. Must" << endl
    59         << "         be an unsigned 16 bit number." << endl
    59         << "          be an unsigned 16 bit number." << endl
    60         << "  LENGTH is the number of bytes to read and must also be" << endl
    60         << "  SIZE    is the number of bytes to read and must also be" << endl
    61         << "         an unsigned 16 bit number. OFFSET plus LENGTH" << endl
    61         << "          an unsigned 16 bit number. ADDRESS plus SIZE" << endl
    62         << "         may not exceed 64k. The length is ignored (and" << endl
    62         << "          may not exceed 64k. The size is ignored (and" << endl
    63         << "         can be omitted), if a selected data type" << endl
    63         << "          can be omitted), if a selected data type" << endl
    64         << "         implies a length." << endl
    64         << "          implies a size." << endl
    65         << endl
    65         << endl
    66         << typeInfo()
    66         << typeInfo()
    67         << endl
    67         << endl
    68         << "Command-specific options:" << endl
    68         << "Command-specific options:" << endl
    69         << "  --alias    -a <alias>" << endl
    69         << "  --alias    -a <alias>" << endl
    79 /****************************************************************************/
    79 /****************************************************************************/
    80 
    80 
    81 void CommandRegRead::execute(const StringVector &args)
    81 void CommandRegRead::execute(const StringVector &args)
    82 {
    82 {
    83     SlaveList slaves;
    83     SlaveList slaves;
    84     ec_ioctl_slave_reg_t data;
    84     ec_ioctl_slave_reg_t io;
    85     stringstream strOffset, err;
    85     stringstream strOffset, err;
    86     const DataType *dataType = NULL;
    86     const DataType *dataType = NULL;
    87 
    87 
    88     if (args.size() < 1 || args.size() > 2) {
    88     if (args.size() < 1 || args.size() > 2) {
    89         err << "'" << getName() << "' takes one or two arguments!";
    89         err << "'" << getName() << "' takes one or two arguments!";
    91     }
    91     }
    92 
    92 
    93     strOffset << args[0];
    93     strOffset << args[0];
    94     strOffset
    94     strOffset
    95         >> resetiosflags(ios::basefield) // guess base from prefix
    95         >> resetiosflags(ios::basefield) // guess base from prefix
    96         >> data.offset;
    96         >> io.address;
    97     if (strOffset.fail()) {
    97     if (strOffset.fail()) {
    98         err << "Invalid offset '" << args[0] << "'!";
    98         err << "Invalid address '" << args[0] << "'!";
    99         throwInvalidUsageException(err);
    99         throwInvalidUsageException(err);
   100     }
   100     }
   101 
   101 
   102     if (args.size() > 1) {
   102     if (args.size() > 1) {
   103         stringstream strLength;
   103         stringstream strLength;
   104         strLength << args[1];
   104         strLength << args[1];
   105         strLength
   105         strLength
   106             >> resetiosflags(ios::basefield) // guess base from prefix
   106             >> resetiosflags(ios::basefield) // guess base from prefix
   107             >> data.length;
   107             >> io.size;
   108         if (strLength.fail()) {
   108         if (strLength.fail()) {
   109             err << "Invalid length '" << args[1] << "'!";
   109             err << "Invalid size '" << args[1] << "'!";
   110             throwInvalidUsageException(err);
   110             throwInvalidUsageException(err);
   111         }
   111         }
   112 
   112 
   113         if (!data.length) {
   113         if (!io.size) {
   114             err << "Length may not be zero!";
   114             err << "Length may not be zero!";
   115             throwInvalidUsageException(err);
   115             throwInvalidUsageException(err);
   116         }
   116         }
   117     } else { // no length argument given
   117     } else { // no size argument given
   118         data.length = 0;
   118         io.size = 0;
   119     }
   119     }
   120 
   120 
   121     if (!getDataType().empty()) {
   121     if (!getDataType().empty()) {
   122         if (!(dataType = findDataType(getDataType()))) {
   122         if (!(dataType = findDataType(getDataType()))) {
   123             err << "Invalid data type '" << getDataType() << "'!";
   123             err << "Invalid data type '" << getDataType() << "'!";
   124             throwInvalidUsageException(err);
   124             throwInvalidUsageException(err);
   125         }
   125         }
   126 
   126 
   127         if (dataType->byteSize) {
   127         if (dataType->byteSize) {
   128             // override length argument
   128             // override size argument
   129             data.length = dataType->byteSize;
   129             io.size = dataType->byteSize;
   130         }
   130         }
   131     }
   131     }
   132 
   132 
   133     if (!data.length) {
   133     if (!io.size) {
   134         err << "The length argument is mandatory, if no datatype is " << endl
   134         err << "The size argument is mandatory, if no datatype is " << endl
   135             << "specified, or the datatype does not imply a length!";
   135             << "specified, or the datatype does not imply a size!";
   136         throwInvalidUsageException(err);
   136         throwInvalidUsageException(err);
   137     }
   137     }
   138 
   138 
   139     if ((uint32_t) data.offset + data.length > 0xffff) {
   139     if ((uint32_t) io.address + io.size > 0xffff) {
   140         err << "Offset and length exceeding 64k!";
   140         err << "Address and size exceeding 64k!";
   141         throwInvalidUsageException(err);
   141         throwInvalidUsageException(err);
   142     }
   142     }
   143     
   143 
   144     MasterDevice m(getSingleMasterIndex());
   144     MasterDevice m(getSingleMasterIndex());
   145     m.open(MasterDevice::Read);
   145     m.open(MasterDevice::Read);
   146     slaves = selectedSlaves(m);
   146     slaves = selectedSlaves(m);
   147 
   147 
   148     if (slaves.size() != 1) {
   148     if (slaves.size() != 1) {
   149         throwSingleSlaveRequired(slaves.size());
   149         throwSingleSlaveRequired(slaves.size());
   150     }
   150     }
   151     data.slave_position = slaves.front().position;
   151     io.slave_position = slaves.front().position;
       
   152     io.emergency = false;
   152 
   153 
   153     data.data = new uint8_t[data.length];
   154     io.data = new uint8_t[io.size];
   154 
   155 
   155     try {
   156     try {
   156         m.readReg(&data);
   157         m.readReg(&io);
   157     } catch (MasterDeviceException &e) {
   158     } catch (MasterDeviceException &e) {
   158         delete [] data.data;
   159         delete [] io.data;
   159         throw e;
   160         throw e;
   160     }
   161     }
   161 
   162 
   162     try {
   163     try {
   163         outputData(cout, dataType, data.data, data.length);
   164         outputData(cout, dataType, io.data, io.size);
   164     } catch (SizeException &e) {
   165     } catch (SizeException &e) {
   165         delete [] data.data;
   166         delete [] io.data;
   166         throwCommandException(e.what());
   167         throwCommandException(e.what());
   167     }
   168     }
   168 
   169 
   169     delete [] data.data;
   170     delete [] io.data;
   170 }
   171 }
   171 
   172 
   172 /*****************************************************************************/
   173 /*****************************************************************************/