45 const uint8_t node_id = 2;
49 const std::string busname =
"slcan0";
53 const std::string baudrate =
"500K";
57 const uint16_t index = 0x6200;
64 const uint8_t subindex = 0x01;
70 const std::vector<uint8_t> data { 0x7F };
79 std::cout <<
"This is an example which shows the usage of the Core library." << std::endl;
85 bool found_node =
false;
87 std::cout <<
"Registering a callback which is called when a device is detected via NMT..." << std::endl;
89 std::cout <<
"Device says it's alive! ID = " << (unsigned) new_node_id << std::endl;
91 if (new_node_id == node_id) {
96 std::cout <<
"Starting Core (connect to the driver and start the receiver thread)..." << std::endl;
97 if (!core.
start(busname, baudrate)) {
98 std::cout <<
"Starting core failed." << std::endl;
102 std::cout <<
"Asking all devices to reset. You don't need to do that, but it makes"
103 <<
" sure all slaves are in a reproducible state." << std::endl;
110 std::cout <<
"Giving the devices one second time to respond..." << std::endl;
111 std::this_thread::sleep_for(std::chrono::seconds(1));
115 std::cout <<
"Node with ID " << (unsigned) node_id <<
" has not been found."<< std::endl;
119 std::cout <<
"Asking the device to start up..." << std::endl;
122 std::cout <<
"Giving the devices one second time to boot up..." << std::endl;
123 std::this_thread::sleep_for(std::chrono::seconds(1));
129 std::cout <<
"Writing to a dictionary entry (CANopen speech: \"download\")..." << std::endl;
130 core.
sdo.
download(node_id, index, subindex, data.size(), data);
132 std::cout <<
"Reading the device type (\"upload\" 0x1000)... Little-endian!" << std::endl;
133 std::vector<uint8_t> device_type = core.
sdo.
upload(node_id,0x1000,0x0);
134 for (uint8_t device_type_byte : device_type) {
135 std::cout <<
" byte 0x" << std::hex << (unsigned) device_type_byte << std::endl;
138 std::cout <<
"Reading the device name (\"upload\" 0x1008 - usually using segmented transfer)..." << std::endl;
139 std::vector<uint8_t> device_name = core.
sdo.
upload(node_id,0x1008,0x0);
140 std::string result(reinterpret_cast<char const*>(device_name.data()), device_name.size());
141 std::cout <<
" " << result << std::endl;
143 std::cout <<
"Finished." << std::endl;
void send_nmt_message(uint8_t node_id, Command cmd)
Sends a NMT message to a given device.
NMT nmt
The NMT sub-protocol.
void register_device_alive_callback(const DeviceAliveCallback &callback)
Registers a callback which will be called when a slave sends it's state via NMT and the state indicat...
void download(uint8_t node_id, uint16_t index, uint8_t subindex, uint32_t size, const std::vector< uint8_t > &bytes)
SDO download: Write value into remote device's object dictionary.
This class implements the Core of KaCanOpen It communicates with the CAN driver, sends CAN messages a...
SDO sdo
The SDO sub-protocol.
bool start(const std::string busname, const std::string &baudrate)
Opens CAN driver and starts CAN message receive loop.
std::vector< uint8_t > upload(uint8_t node_id, uint16_t index, uint8_t subindex)
SDO download: Get value from remote device's object dictionary.
void reset_all_nodes()
Resets all nodes in the network.