Main function
In this section, you'll learn how to apply all the code in this chapter to main.cpp
. If everything is done correctly your KINGSTAR Subsystem should be started and stopped properly. To help you review what you have learned, we add comments into the code to describe the meaning of the variables and functions.
- In
main.cpp
, add the following code beforeint _tmain
to include RT_Project_01.h and SystemInitialization.h. - In
int _tmain
, add the following code to start the KINGSTAR Subsystem. This function is written inSystemInitialization.cpp
. - Under the block of
StartKingstar();
, add the following code to get the state of EtherCAT network and check whether the KINGSTAR Subsystem is started. - Under
RtPrintf("\n");
, add the following code to get the information of an EtherCAT slave, the Vendor ID of an axis, and the input and output length of an I/O module. - Under
RtPrintf("\n");
, add the following code to stop the KINGSTAR Subsystem.
Copy
#include "RT_Project_01.h"
#include "SystemInitialization.h"
NOTE: The if (!StartKingstar())
code block will be used repeatedly in the following chapters.
Copy
//Start the KINGSTAR Subsystem.
if (!StartKingstar())
{
RtPrintf("The KINGSTAR Subsystem is not started.\n");
return -1;
}
else
RtPrintf("The KINGSTAR Subsystem is started.\n");
RtPrintf("\n");
Copy
//Create an instance of the SubsystemStatus structure and get the state from it.
SubsystemStatus Subsystem = { ecatOffline, ecatOffline, 0, 0, 0, {ecatOffline}, {ecatOffline}, {axisOffline} };
GetStatus(&Subsystem, NULL);
//Display the details of the EtherCAT network.
RtPrintf("Number of Devices found: %d\n", Subsystem.SlaveCount);
RtPrintf("Number of Axes found: %d\n", Subsystem.AxesCount);
RtPrintf("Number of I/O found: %d\n", Subsystem.IOCount);
RtPrintf("\n");
Copy
int nRet = 0;
int resolution = 0;
SlaveStatus Sts = { 0 };
//Get the detailed information from a specified slave using an ID.
nRet = GetSlaveById(0, &Sts);
RtPrintf("%s cycle time: %d\n", Sts.Name, Sts.CycleTime);
//Get information from an axis.
nRet = GetAxisByIndex(2, &Sts, &resolution, NULL, NULL);
RtPrintf("%s Vendor ID: %d\n", Sts.Name, Sts.VendorId);
//Get information from an I/O module.
nRet = GetIOByIndex(3, &Sts);
RtPrintf("%s input length: %d\n", Sts.Name, Sts.InputLength);
RtPrintf("%s output length: %d\n", Sts.Name, Sts.OutputLength);
RtPrintf("\n");
NOTE: The if (!StopKingstar())
code block will be used repeatedly in the following chapters.
Copy
//Stop the KINGSTAR Subsystem.
if (!StopKingstar())
{
RtPrintf("The KINGSTAR Subsystem is not stopped.\n");
return -1;
}
else
RtPrintf("The KINGSTAR Subsystem is stopped.\n");
Complete code
In conclusion, in main.cpp
, your code should be as follows:
Copy
#include "RT_Project_01.h"
#include "SystemInitialization.h"
int _tmain(int argc, _TCHAR * argv[])
{
//Start the KINGSTAR Subsystem.
if (!StartKingstar())
{
RtPrintf("The KINGSTAR Subsystem is not started.\n");
return -1;
}
else
RtPrintf("The KINGSTAR Subsystem is started.\n");
RtPrintf("\n");
//Create an instance of the SubsystemStatus structure and get the state from it.
SubsystemStatus Subsystem = { ecatOffline, ecatOffline, 0, 0, 0, {ecatOffline}, {ecatOffline}, {axisOffline} };
GetStatus(&Subsystem, NULL);
//Display the details of the EtherCAT network.
RtPrintf("Number of Devices found: %d\n", Subsystem.SlaveCount);
RtPrintf("Number of Axes found: %d\n", Subsystem.AxesCount);
RtPrintf("Number of I/O found: %d\n", Subsystem.IOCount);
RtPrintf("\n");
int nRet = 0;
int resolution = 0;
SlaveStatus Sts = { 0 };
//Get the detailed information from a specified slave using an ID.
nRet = GetSlaveById(0, &Sts);
RtPrintf("%s cycle time: %d\n", Sts.Name, Sts.CycleTime);
//Get information from an axis.
nRet = GetAxisByIndex(2, &Sts, &resolution, NULL, NULL);
RtPrintf("%s Vendor ID: %d\n", Sts.Name, Sts.VendorId);
//Get information from an I/O module.
nRet = GetIOByIndex(3, &Sts);
RtPrintf("%s input length: %d\n", Sts.Name, Sts.InputLength);
RtPrintf("%s output length: %d\n", Sts.Name, Sts.OutputLength);
RtPrintf("\n");
//Stop the KINGSTAR Subsystem.
if (!StopKingstar())
{
RtPrintf("The KINGSTAR Subsystem is not stopped.\n");
return -1;
}
else
RtPrintf("The KINGSTAR Subsystem is stopped.\n");
return 0;
}
Output: