KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
listdevices.cpp
1 /*
2  * Copyright (c) 2015-2016, Thomas Keh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <thread>
33 #include <chrono>
34 #include <cstdint>
35 
36 #include "master.h"
37 #include "logger.h"
38 
39 // This example lists all connected devices and prints their manufacturer device name.
40 
41 int main() {
42 
43  // Set the name of your CAN bus. "slcan0" is a common bus name
44  // for the first SocketCAN device on a Linux system.
45  const std::string busname = "slcan0";
46 
47  // Set the baudrate of your CAN bus. Most drivers support the values
48  // "1M", "500K", "125K", "100K", "50K", "20K", "10K" and "5K".
49  const std::string baudrate = "500K";
50 
51  PRINT("List devices");
52 
53  kaco::Master master;
54 
55  if (!master.start(busname, baudrate)) {
56  ERROR("Starting master failed.");
57  return EXIT_FAILURE;
58  }
59 
60  std::this_thread::sleep_for(std::chrono::seconds(1));
61  const size_t num_devices = master.num_devices();
62 
63  DUMP(num_devices);
64 
65  for (size_t i=0; i<num_devices; ++i) {
66 
67  kaco::Device& device = master.get_device(i);
68  PRINT("Found device: " << device.get_node_id());
69 
70  PRINT("Starting");
71  device.start();
72 
73  PRINT("Loading EDS from library...")
74  const std::string loaded_eds_file = device.load_dictionary_from_library();
75  PRINT("Loaded the following EDS file from the library: " << loaded_eds_file);
76 
77  PRINT("Dictionary:");
78  device.read_complete_dictionary();
79  device.print_dictionary();
80 
81  }
82 
83 }
Device & get_device(size_t index) const
Returns a reference to a slave device object.
Definition: master.cpp:84
void start()
Starts the node via NMT protocol and loads mandatory entries, operations, and constants.
Definition: device.cpp:54
size_t num_devices() const
Returns the number of slave devices in the network.
Definition: master.cpp:80
bool start(const std::string busname, const std::string &baudrate)
Starts master and creates Core.
Definition: master.cpp:51
This class represents a master node. It listens for new slaves and provides access to them via get_sl...
Definition: master.h:47
uint8_t get_node_id() const
Returns the node ID of the device.
Definition: device.cpp:74
This class represents a CanOpen slave device in the network.
Definition: device.h:83