tools/main.cpp
changeset 935 b954e9d91ea5
parent 922 fede1d8f5b71
child 936 30fddfce8db6
equal deleted inserted replaced
934:96241b092fac 935:b954e9d91ea5
    17 #define DEFAULT_MASTER 0
    17 #define DEFAULT_MASTER 0
    18 #define DEFAULT_COMMAND "slaves"
    18 #define DEFAULT_COMMAND "slaves"
    19 #define DEFAULT_SLAVESPEC ""
    19 #define DEFAULT_SLAVESPEC ""
    20 
    20 
    21 static unsigned int masterIndex = DEFAULT_MASTER;
    21 static unsigned int masterIndex = DEFAULT_MASTER;
    22 static string slaveSpec = DEFAULT_SLAVESPEC;
    22 static int slavePosition = -1;
    23 static string command = DEFAULT_COMMAND;
    23 static string command = DEFAULT_COMMAND;
    24 
    24 
    25 /*****************************************************************************/
    25 /*****************************************************************************/
    26 
    26 
    27 void printUsage()
    27 void printUsage()
    28 {
    28 {
    29     cerr
    29     cerr
    30         << "Usage: ethercat <COMMAND> [OPTIONS]" << endl
    30         << "Usage: ethercat <COMMAND> [OPTIONS]" << endl
    31 		<< "Commands:" << endl
    31 		<< "Commands:" << endl
    32         << "  list (ls, slaves)  List all slaves (former 'lsec')." << endl
    32         << "  list (ls, slaves)  List all slaves (former 'lsec')." << endl
       
    33         << "  pdos               List Pdo mapping of given slaves." << endl
    33 		<< "Global options:" << endl
    34 		<< "Global options:" << endl
    34         << "  --master  -m <master>  Index of the master to use. Default: "
    35         << "  --master  -m <master>  Index of the master to use. Default: "
    35 		<< DEFAULT_MASTER	<< endl
    36 		<< DEFAULT_MASTER	<< endl
    36         << "  --slave   -s <slave>   Slave specification. Default: All "
    37         << "  --slave   -s <slave>   Numerical ring position. Default: All "
    37         "slaves." << endl
    38         "slaves." << endl
    38         << "  --help    -h           Show this help." << endl;
    39         << "  --help    -h           Show this help." << endl;
    39 }
    40 }
    40 
    41 
    41 /*****************************************************************************/
    42 /*****************************************************************************/
    66                 }
    67                 }
    67 				masterIndex = number;
    68 				masterIndex = number;
    68                 break;
    69                 break;
    69 
    70 
    70             case 's':
    71             case 's':
    71 				slaveSpec = optarg;
    72                 number = strtoul(optarg, &remainder, 0);
       
    73                 if (remainder == optarg || *remainder
       
    74                         || number < 0 || number > 0xFFFF) {
       
    75                     cerr << "Invalid slave position " << optarg << "!" << endl;
       
    76                     printUsage();
       
    77                     exit(1);
       
    78                 }
       
    79 				slavePosition = number;
    72                 break;
    80                 break;
    73 
    81 
    74             case 'h':
    82             case 'h':
    75             case '?':
    83             case '?':
    76                 printUsage();
    84                 printUsage();
    99 {
   107 {
   100     Master master;
   108     Master master;
   101     
   109     
   102 	getOptions(argc, argv);
   110 	getOptions(argc, argv);
   103 
   111 
   104     master.open(masterIndex);
   112     try {
       
   113         master.open(masterIndex);
   105 
   114 
   106     if (command == "list" || command == "ls" || command == "slaves") {
   115         if (command == "list" || command == "ls" || command == "slaves") {
   107         master.listSlaves();
   116             master.listSlaves();
   108     } else {
   117 
   109         cerr << "Unknown command " << command << "!" << endl;
   118         } else if (command == "pdos") {
   110         printUsage();
   119             master.listPdos(slavePosition);
       
   120         } else {
       
   121             cerr << "Unknown command " << command << "!" << endl;
       
   122             printUsage();
       
   123             exit(1);
       
   124         }
       
   125     } catch (MasterException &e) {
       
   126         cerr << e.what() << endl;
   111         exit(1);
   127         exit(1);
   112     }
   128     }
   113 
   129 
   114 	return 0;
   130 	return 0;
   115 }
   131 }