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
MotionController *controller = MotionController::Create(&params);
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;
}
NetworkNode * NetworkNodeGet(int32_t nodeNumber)
NetworkNodeGet returns a pointer to a RapidCodeNetworkNode object using its node number and initializ...
RSINetworkState NetworkStateGet()
void Delete(void)
Delete the MotionController and all its objects.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:796
const char *const NetworkOutputNameGet(int32_t index)
Get the name of a PDO output.
int32_t NetworkOutputBitSizeGet(int32_t index)
Get the size (in bits) of a PDO output.
int32_t NetworkInputCountGet()
Get the number of PDO inputs found on the network.
int32_t NetworkInputBitSizeGet(int32_t index)
Get the size (in bits) of a network input.
RSINetworkType NetworkTypeGet()
Returns network type. (Currently only STRING type is supported.)
const char *const NetworkInputNameGet(int32_t index)
Get the name of a PDO network input.
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:857