APIs, concepts, guides, and more
PrintNetworkTopology.cpp
Attention
See the following Concept pages for a detailed explanation of this sample: RapidCode NetworkNode.
Warning
This is a sample program to assist in the integration of the RMP motion controller with your application. It may not contain all of the logic and safety features that your application requires. We recommend that you wire an external hardware emergency stop (e-stop) button for safety when using our code sample apps. Doing so will help ensure the safety of you and those around you and will prevent potential injury or damage.

The sample apps assume that the system (network, axes, I/O) are configured prior to running the code featured in the sample app. See the Configuration page for more information.
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include "SampleAppsHelper.h" // Import our helper functions.
#include "rsi.h" // Import our RapidCode Library.
using namespace RSI::RapidCode; // Import the RapidCode namespace
// Map of the RSINetworkType enum to string
static const std::map<RSINetworkType, std::string> RSINetworkTypeMap = {
{RSINetworkType::RSINetworkTypeSTRING, "RSINetworkTypeSTRING"},
{RSINetworkType::RSINetworkTypeDUAL_STRING, "RSINetworkTypeDUAL_STRING"},
{RSINetworkType::RSINetworkTypeRING, "RSINetworkTypeRING"}
};
// Map of the RSINetworkState enum to string
static const std::map<RSINetworkState, std::string> RSINetworkStateMap = {
{RSINetworkState::RSINetworkStateUNINITIALIZED, "RSINetworkStateUNINITIALIZED"},
{RSINetworkState::RSINetworkStateDISCOVERING, "RSINetworkStateDISCOVERING"},
{RSINetworkState::RSINetworkStateDISCOVERED, "RSINetworkStateDISCOVERED"},
{RSINetworkState::RSINetworkStatePREOPERATIONAL, "RSINetworkStatePREOPERATIONAL"},
{RSINetworkState::RSINetworkStateOPERATIONAL, "RSINetworkStateOPERATIONAL"},
{RSINetworkState::RSINetworkStateERROR, "RSINetworkStateERROR"},
{RSINetworkState::RSINetworkStateSHUTDOWN, "RSINetworkStateSHUTDOWN"},
{RSINetworkState::RSINetworkStateSTARTING, "RSINetworkStateSTARTING"}
};
// Constants for reading the device type from the Mdp
struct MdpConstants
{
static constexpr int DEVICE_TYPE_INDEX = 0x1000;
static constexpr int DEVICE_TYPE_SUB_INDEX = 0;
static constexpr int DEVICE_TYPE_BYTE_SIZE = 4;
};
// Helper function to get the list of network nodes from the controller
static std::vector<RapidCodeNetworkNode *> GetNodeList(MotionController *controller)
{
std::vector<RapidCodeNetworkNode *> nodes;
for (int i = 0; i < controller->NetworkNodeCountGet(); ++i)
{
RapidCodeNetworkNode *node = controller->NetworkNodeGet(i);
if (node == nullptr)
{
throw std::runtime_error("Error creating the network node, it is null.");
}
if (!node->Exists())
{
throw std::runtime_error("The network node should exist after we get it.");
}
nodes.push_back(node);
}
return nodes;
}
// Helper function to get the device type of a node from the Mdp
static std::string GetDeviceType(RapidCodeNetworkNode *node)
{
std::string deviceType = "";
try
{
uint32_t deviceTypeValue =
node->ServiceChannelRead(MdpConstants::DEVICE_TYPE_INDEX, MdpConstants::DEVICE_TYPE_SUB_INDEX, MdpConstants::DEVICE_TYPE_BYTE_SIZE);
std::ostringstream deviceTypeStream;
deviceTypeStream << "0x" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << deviceTypeValue;
deviceType = deviceTypeStream.str();
}
catch (std::exception err)
{
deviceType = "Exception trying to read device type";
}
return deviceType;
}
// Helper function to read the information of a node into a string
static std::string ReadNodeInfo(RapidCodeNetworkNode *node)
{
std::ostringstream nodeInfo;
nodeInfo << "\n"; // for spacing
nodeInfo << "Node[" << node->NumberGet() << "] - " << node->NameGet() << " ______________________________________________\n";
nodeInfo << " Vendor: " << node->VendorNameGet();
nodeInfo << " Product: " << node->ProductNameGet() << "\n";
nodeInfo << " VendorID: 0x" << std::hex << std::setw(8) << std::setfill('0') << node->VendorIdGet();
nodeInfo << " ProductCode: 0x" << std::setw(8) << node->ProductCodeGet() << "\n";
nodeInfo << " HardwareRev: 0x" << std::setw(8) << node->RevisionGet();
nodeInfo << " SerialNumber: " << node->SerialNumberGet() << "\n";
nodeInfo << " StationAlias: 0x" << std::setw(8) << node->StationAliasGet();
nodeInfo << " AxisCount: " << std::dec << node->AxisCountGet() << "\n";
nodeInfo << " DeviceType: " << GetDeviceType(node);
nodeInfo << " (For Mdp by reading SDO 0x" << std::hex << std::setw(4) << MdpConstants::DEVICE_TYPE_INDEX << ")\n";
nodeInfo << " SegmentCount: " << node->SegmentCountGet() << "\n";
nodeInfo << " DI: " << node->DigitalInCountGet();
nodeInfo << " DO: " << node->DigitalOutCountGet();
nodeInfo << " AI: " << node->AnalogInCountGet();
nodeInfo << " AO: " << node->AnalogOutCountGet() << "\n";
return nodeInfo.str();
}
int main()
{
const std::string SAMPLE_APP_NAME = "Utilities: Print Network Topology";
// Print a start message to indicate that the sample app has started
/* RAPIDCODE INITIALIZATION */
// Create the controller
int exitCode = -1; // Set the exit code to an error value.
try // Ensure that the controller is deleted if an error occurs.
{
/* SAMPLE APP BODY */
// Get the nodes
std::vector<RapidCodeNetworkNode *> nodes = GetNodeList(controller);
// Print the network topology
std::ostringstream topologyInfo;
// Add the overview information
topologyInfo << "EtherCAT: " << std::to_string(nodes.size());
topologyInfo << " Nodes, " << RSINetworkTypeMap.at(controller->NetworkTypeGet());
topologyInfo << " " << RSINetworkStateMap.at(controller->NetworkStateGet()) << "\n";
// Add information for each node
for (RapidCodeNetworkNode *node : nodes)
{
topologyInfo << ReadNodeInfo(node);
}
// Add network input information
topologyInfo << "\nNetworkInputs count: " << controller->NetworkInputCountGet() << " _________________________________\n";
for (int i = 0; i < controller->NetworkInputCountGet(); ++i)
{
topologyInfo << " [" << i << "] - " << std::setw(70) << controller->NetworkInputNameGet(i);
topologyInfo << " Bits: " << controller->NetworkInputBitSizeGet(i) << "\n";
}
// Add network output information
topologyInfo << "NetworkOutputs count: " << controller->NetworkOutputCountGet() << " _________________________________\n";
for (int i = 0; i < controller->NetworkOutputCountGet(); ++i)
{
topologyInfo << " [" << i << "] - " << std::setw(70) << controller->NetworkOutputNameGet(i);
topologyInfo << " Bits: " << controller->NetworkOutputBitSizeGet(i) << "\n";
}
// Print the accumulated string
std::cout << topologyInfo.str() << std::endl;
exitCode = 0;
}
catch (const std::exception &ex)
{
std::cerr << ex.what() << std::endl;
exitCode = -1;
}
// Delete the controller as the program exits to ensure memory is deallocated in the correct order
controller->Delete();
// Print a message to indicate the sample app has finished and if it was successful or not
SampleAppsHelper::PrintFooter(SAMPLE_APP_NAME, exitCode);
return exitCode;
}
static MotionController * Create(CreationParameters *creationParameters)
Initialize and start the RMP EtherCAT controller.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:800
@ RSINetworkStatePREOPERATIONAL
EtherCAT preoperational.
Definition rsienums.h:572
@ RSINetworkStateSHUTDOWN
EtherCAT was shutdown or stopped, must restart.
Definition rsienums.h:575
@ RSINetworkStateDISCOVERED
EtherCAT nodes discovered but not started.
Definition rsienums.h:571
@ RSINetworkStateDISCOVERING
EtherCAT network is in the process of discovering nodes.
Definition rsienums.h:570
@ RSINetworkStateOPERATIONAL
EtherCAT operational, good state.
Definition rsienums.h:573
@ RSINetworkStateSTARTING
EtherCAT is starting.
Definition rsienums.h:576
@ RSINetworkStateUNINITIALIZED
EtherCAT not yet started.
Definition rsienums.h:569
@ RSINetworkTypeRING
A Ring topology is a chain of linked Node(s) going from the Controllers Out port to the In port.
Definition rsienums.h:629
@ RSINetworkTypeSTRING
A String topology is chain of linked Node(s) to the Controller from the In or Out port.
Definition rsienums.h:627
@ RSINetworkTypeDUAL_STRING
A Dual String topology is two chained links of Node(s) to the Controller from the In and Out ports.
Definition rsienums.h:628
static void PrintFooter(std::string sampleAppName, int exitCode)
Print a message to indicate the sample app has finished and if it was successful or not.
static void CheckErrors(RapidCodeObject *rsiObject)
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
static void PrintHeader(std::string sampleAppName)
Print a start message to indicate that the sample app has started.
static MotionController::CreationParameters GetCreationParameters()
Returns a MotionController::CreationParameters object with user-defined parameters.
CreationParameters for MotionController::Create.
Definition rsi.h:866