Refactor
This commit is contained in:
20
experiments/can/CMakeLists.txt
Normal file
20
experiments/can/CMakeLists.txt
Normal file
@@ -0,0 +1,20 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
cmake_policy(SET CMP0020 NEW)
|
||||
|
||||
find_package(PIP REQUIRED)
|
||||
|
||||
if(WIN32)
|
||||
add_custom_target(copy_dependencies
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${PCAN_LIB} ${CMAKE_CURRENT_BINARY_DIR}/PCANBasic${CMAKE_SHARED_LIBRARY_SUFFIX}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${VSCAN_LIB} ${CMAKE_CURRENT_BINARY_DIR}/vs_can_api${CMAKE_SHARED_LIBRARY_SUFFIX})
|
||||
|
||||
add_executable(can_send_multithread can_send_multithread.cpp)
|
||||
target_include_directories(can_send_multithread PUBLIC ${PIP_INCLUDES} ${CAN_INCLUDES})
|
||||
target_link_libraries(can_send_multithread can)
|
||||
add_dependencies(can_send_multithread copy_dependencies)
|
||||
|
||||
add_executable(can_send can_send.cpp)
|
||||
target_include_directories(can_send PUBLIC ${PIP_INCLUDES} ${CAN_INCLUDES})
|
||||
target_link_libraries(can_send can)
|
||||
add_dependencies(can_send copy_dependencies)
|
||||
endif()
|
||||
16
experiments/can/can_send.cpp
Normal file
16
experiments/can/can_send.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "can_send.h"
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
auto time1 = std::async(std::launch::deferred, [] { return test_send(PCAN_USBBUS1); });
|
||||
auto time2 = std::async(std::launch::deferred, [] { return test_send(PCAN_USBBUS2); });
|
||||
|
||||
time1.wait();
|
||||
std::cout << "measurements for PCAN_USBBUS1: " << time1.get() / 1000.f << " ms" << std::endl;
|
||||
|
||||
time2.wait();
|
||||
std::cout << "measurements for PCAN_USBBUS2: " << time2.get() / 1000.f << " ms" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
experiments/can/can_send.h
Normal file
23
experiments/can/can_send.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef MULTITHREAD_EXPERIMENTS_CAN_SEND_H
|
||||
#define MULTITHREAD_EXPERIMENTS_CAN_SEND_H
|
||||
|
||||
#include "pcan/peakcandevice.h"
|
||||
#include <chrono>
|
||||
|
||||
float test_send(int device_id) {
|
||||
PeakCANDevice canDevice(PeakCANDevice::CAN_SPEED_500K, device_id);
|
||||
if (!canDevice.open()) {
|
||||
return 0.f;
|
||||
}
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
CAN_Raw msg = { .Id = 0x72, .Size = 8, .Data = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 } };
|
||||
for (int i = 0; i < 30 * 1000; ++i) {
|
||||
canDevice.send(msg);
|
||||
}
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.f;
|
||||
}
|
||||
|
||||
#endif //MULTITHREAD_EXPERIMENTS_CAN_SEND_H
|
||||
13
experiments/can/can_send_multithread.cpp
Normal file
13
experiments/can/can_send_multithread.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "can_send.h"
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
auto time1 = std::async(std::launch::async, [] { return test_send(PCAN_USBBUS1); });
|
||||
auto time2 = std::async(std::launch::async, [] { return test_send(PCAN_USBBUS2); });
|
||||
|
||||
std::cout << "measurements for PCAN_USBBUS1: " << time1.get() / 1000.f << " ms" << std::endl;
|
||||
std::cout << "measurements for PCAN_USBBUS2: " << time2.get() / 1000.f << " ms" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user