tool/CommandSoeRead.cpp
changeset 1831 1875b9fea0ba
child 1832 fb6a307daf31
equal deleted inserted replaced
1830:ef09f0ea0c4c 1831:1875b9fea0ba
       
     1 /*****************************************************************************
       
     2  *
       
     3  *  $Id$
       
     4  *
       
     5  *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
       
     6  *
       
     7  *  This file is part of the IgH EtherCAT Master.
       
     8  *
       
     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
       
    11  *  published by the Free Software Foundation.
       
    12  *
       
    13  *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
       
    14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
       
    16  *  Public License for more details.
       
    17  *
       
    18  *  You should have received a copy of the GNU General Public License along
       
    19  *  with the IgH EtherCAT Master; if not, write to the Free Software
       
    20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    21  *
       
    22  *  ---
       
    23  *
       
    24  *  The license mentioned above concerns the source code only. Using the
       
    25  *  EtherCAT technology and brand is only permitted in compliance with the
       
    26  *  industrial property and similar rights of Beckhoff Automation GmbH.
       
    27  *
       
    28  ****************************************************************************/
       
    29 
       
    30 #include <iostream>
       
    31 #include <iomanip>
       
    32 using namespace std;
       
    33 
       
    34 #include "CommandSoeRead.h"
       
    35 #include "MasterDevice.h"
       
    36 
       
    37 /*****************************************************************************/
       
    38 
       
    39 CommandSoeRead::CommandSoeRead():
       
    40     Command("soe_read", "Read an SoE IDN from a slave.")
       
    41 {
       
    42 }
       
    43 
       
    44 /*****************************************************************************/
       
    45 
       
    46 string CommandSoeRead::helpString() const
       
    47 {
       
    48     stringstream str;
       
    49 
       
    50     str << getName() << " [OPTIONS] <INDEX> <SUBINDEX>" << endl
       
    51         << endl
       
    52         << getBriefDescription() << endl
       
    53         << endl
       
    54         << "This command requires a single slave to be selected." << endl
       
    55         << endl
       
    56         << "Arguments:" << endl
       
    57         << "  IDN      is the IDN and must be an unsigned" << endl
       
    58         << "           16 bit number." << endl
       
    59         << endl
       
    60         << "Command-specific options:" << endl
       
    61         << "  --alias    -a <alias>" << endl
       
    62         << "  --position -p <pos>    Slave selection. See the help of" << endl
       
    63         << "                         the 'slaves' command." << endl
       
    64         << endl
       
    65         << numericInfo();
       
    66 
       
    67     return str.str();
       
    68 }
       
    69 
       
    70 /****************************************************************************/
       
    71 
       
    72 void CommandSoeRead::execute(const StringVector &args)
       
    73 {
       
    74     SlaveList slaves;
       
    75     stringstream err, strIdn;
       
    76     ec_ioctl_slave_soe_t data;
       
    77 
       
    78     if (args.size() != 1) {
       
    79         err << "'" << getName() << "' takes one argument!";
       
    80         throwInvalidUsageException(err);
       
    81     }
       
    82 
       
    83     strIdn << args[0];
       
    84     strIdn
       
    85         >> resetiosflags(ios::basefield) // guess base from prefix
       
    86         >> data.idn;
       
    87     if (strIdn.fail()) {
       
    88         err << "Invalid IDN '" << args[0] << "'!";
       
    89         throwInvalidUsageException(err);
       
    90     }
       
    91 
       
    92     if (getMasterIndices().size() != 1) {
       
    93         err << getName() << " requires to select a single master!";
       
    94         throwInvalidUsageException(err);
       
    95     }
       
    96     MasterDevice m(getMasterIndices().front());
       
    97     m.open(MasterDevice::Read);
       
    98     slaves = selectedSlaves(m);
       
    99     if (slaves.size() != 1) {
       
   100         throwSingleSlaveRequired(slaves.size());
       
   101     }
       
   102     data.slave_position = slaves.front().position;
       
   103 
       
   104 	data.mem_size = 1024;
       
   105     data.data = new uint8_t[data.mem_size + 1];
       
   106 
       
   107     try {
       
   108         m.readSoe(&data);
       
   109     } catch (MasterDeviceSoeException &e) {
       
   110         delete [] data.data;
       
   111         err << "CoE read command aborted with code 0x"
       
   112             << setfill('0') << hex << setw(4) << e.errorCode;
       
   113         throwCommandException(err);
       
   114     } catch (MasterDeviceException &e) {
       
   115         delete [] data.data;
       
   116         throw e;
       
   117     }
       
   118 
       
   119     m.close();
       
   120 
       
   121 	printRawData(data.data, data.data_size);
       
   122 
       
   123     delete [] data.data;
       
   124 }
       
   125 
       
   126 /****************************************************************************/
       
   127 
       
   128 void CommandSoeRead::printRawData(
       
   129         const uint8_t *data,
       
   130         unsigned int size
       
   131         )
       
   132 {
       
   133     cout << hex << setfill('0');
       
   134     while (size--) {
       
   135         cout << "0x" << setw(2) << (unsigned int) *data++;
       
   136         if (size)
       
   137             cout << " ";
       
   138     }
       
   139     cout << endl;
       
   140 }
       
   141 
       
   142 /*****************************************************************************/