tool/CommandCrc.cpp
author Florian Pose <fp@igh-essen.com>
Fri, 03 Mar 2017 13:33:17 +0100
changeset 2669 878a657d920a
child 2667 0d7936701e60
permissions -rw-r--r--
New tool command 'ethercat crc'.
2669
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  Copyright (C) 2006-2017  Florian Pose, Ingenieurgemeinschaft IgH
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  This file is part of the IgH EtherCAT Master.
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *  modify it under the terms of the GNU General Public License version 2, as
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  published by the Free Software Foundation.
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  Public License for more details.
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  You should have received a copy of the GNU General Public License along
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  with the IgH EtherCAT Master; if not, write to the Free Software
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  ---
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *  The license mentioned above concerns the source code only. Using the
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *  EtherCAT technology and brand is only permitted in compliance with the
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  industrial property and similar rights of Beckhoff Automation GmbH.
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 ****************************************************************************/
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
#include <iostream>
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
#include <iomanip>
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
using namespace std;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
#include "CommandCrc.h"
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
#include "MasterDevice.h"
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
/*****************************************************************************/
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
CommandCrc::CommandCrc():
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
    Command("crc", "CRC error register diagnosis.")
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
{
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
}
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
/*****************************************************************************/
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
string CommandCrc::helpString(const string &binaryBaseName) const
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
{
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
    stringstream str;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
    str << binaryBaseName << " " << getName()
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
        << " [OPTIONS]" << endl
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
        << endl
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
        << getBriefDescription() << endl
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
        << endl
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
        << "CRC - CRC Error Counter                0x300, 0x302, 0x304, 0x306"
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
        << endl
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
        << "PHY - Physical Interface Error Counter 0x301, 0x303, 0x305, 0x307"
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
        << endl
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
        << "FWD - Forwarded RX Error Counter       0x308, 0x309, 0x30a, 0x30b"
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
        << endl
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
        << "LNK - Lost Link Counter                0x310, 0x311, 0x312, 0x313"
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
        << endl
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
        << endl;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
    return str.str();
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
}
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
/****************************************************************************/
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
#define NUM_PORTS (4)
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
void CommandCrc::execute(const StringVector &args)
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
{
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
    MasterDevice m(getSingleMasterIndex());
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
    m.open(MasterDevice::Read);
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
    ec_ioctl_master_t master;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
    m.getMaster(&master);
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
    cout << "   |";
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
    for (unsigned int port = 0; port < NUM_PORTS; port++) {
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
        cout << "Port " << port << "         |";
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
    }
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
    cout << endl;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
    cout << "   |";
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
    for (unsigned int port = 0; port < NUM_PORTS; port++) {
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
        cout << "CRC PHY FWD LNK|";
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
    }
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
    cout << endl;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
    ec_ioctl_slave_reg_t io;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
    io.emergency = 0;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
    io.address = 0x0300;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
    io.size = 20;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
    io.data = new uint8_t[20];
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
    for (unsigned int i = 0; i < master.slave_count; i++) {
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
        io.slave_position = i;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
        try {
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
            m.readReg(&io);
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
        } catch (MasterDeviceException &e) {
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
            delete [] io.data;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
            throw e;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
        }
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
        cout << setw(3) << i << "|";
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
        for (int port = 0; port < 4; port++) {
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
            cout << setw(3) << (unsigned int) io.data[ 0 + port * 2]; // CRC
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
            cout << setw(4) << (unsigned int) io.data[ 1 + port * 2]; // PHY
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
            cout << setw(4) << (unsigned int) io.data[ 8 + port]; // FWD
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
            cout << setw(4) << (unsigned int) io.data[16 + port]; // LNK
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
            cout << "|";
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
        }
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
        ec_ioctl_slave_t slave;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
        m.getSlave(&slave, i);
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
        std::string slaveName(slave.name);
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
        slaveName = slaveName.substr(0, 11);
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
        cout << slaveName << endl;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
    }
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
    delete [] io.data;
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
}
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
878a657d920a New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
/*****************************************************************************/