From e8956d72e340b0197f3fed5929eb8e8885e1ef7f Mon Sep 17 00:00:00 2001 From: Nikita Date: Thu, 13 Jul 2023 16:56:09 +0300 Subject: [PATCH] Added a function to check the received data and get measurements. --- wrappers/Cpp/rf60Xsdk/CMakeLists.txt | 116 ++--- wrappers/Cpp/rf60Xsdk/rf60Xsdk.cpp | 590 ++++++++++++++++-------- wrappers/Cpp/rf60Xsdk/rf60Xsdk.h | 102 ++-- wrappers/Cpp/rf60Xsdk/serialmanager.cpp | 206 ++++----- wrappers/Cpp/rf60Xsdk/serialmanager.h | 99 ++-- 5 files changed, 651 insertions(+), 462 deletions(-) diff --git a/wrappers/Cpp/rf60Xsdk/CMakeLists.txt b/wrappers/Cpp/rf60Xsdk/CMakeLists.txt index d169072..9e1e24b 100644 --- a/wrappers/Cpp/rf60Xsdk/CMakeLists.txt +++ b/wrappers/Cpp/rf60Xsdk/CMakeLists.txt @@ -1,18 +1,16 @@ cmake_minimum_required(VERSION 3.5) - - - -## -## PROJECT -## name and version -## -project(rf60Xsdk VERSION 1.0.1 LANGUAGES CXX) - - -## -## CONFIGURATION -## +# +# PROJECT name and version +# +project( + rf60Xsdk + VERSION 1.0.1 + LANGUAGES CXX) + +# +# CONFIGURATION +# set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -25,15 +23,12 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BINARY_DIR}/bin") set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${BINARY_DIR}/lib") set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${BINARY_DIR}/lib") - - -set (FETCHCONTENT_FULLY_DISCONNECTED "ON") +set(FETCHCONTENT_FULLY_DISCONNECTED "ON") include(FetchContent) FetchContent_Declare( asio GIT_REPOSITORY https://github.com/chriskohlhoff/asio.git - GIT_TAG d6b95c0188e0359a8cdbdb6571f0cbacf11a538c -) + GIT_TAG d6b95c0188e0359a8cdbdb6571f0cbacf11a538c) FetchContent_MakeAvailable(asio) option(BUILD_SHARED_LIBS "Build using shared libraries" ON) @@ -45,66 +40,57 @@ set(RF60XCORE_DIR "../../../rf60Xcore") add_subdirectory(${RF60XCORE_DIR} CMAKE_CURRENT_SOURCE_DIR) # create glob files for *.h, *.cpp -file (GLOB H_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.h) -file (GLOB CPP_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) +file(GLOB H_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.h) +file(GLOB CPP_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) # concatenate the results (glob files) to variable -set (SOURCES ${CPP_FILES} ${H_FILES}) +set(SOURCES ${CPP_FILES} ${H_FILES}) -## -## TARGET -## create target and add include path -## +# +# TARGET create target and add include path +# add_library(${PROJECT_NAME} ${SOURCES}) -set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") +set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") -target_include_directories(${PROJECT_NAME} PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/ - ) - -target_include_directories(${PROJECT_NAME} PUBLIC ${asio_SOURCE_DIR}/asio/include) -target_include_directories(${PROJECT_NAME} PUBLIC ${asio_SOURCE_DIR}/asio/include/asio/detail) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/) +target_include_directories(${PROJECT_NAME} + PUBLIC ${asio_SOURCE_DIR}/asio/include) +target_include_directories(${PROJECT_NAME} + PUBLIC ${asio_SOURCE_DIR}/asio/include/asio/detail) target_link_libraries(${PROJECT_NAME} rf60Xcore) if(MINGW) - target_link_libraries(${PROJECT_NAME} ws2_32) - target_link_libraries(${PROJECT_NAME} iphlpapi) + target_link_libraries(${PROJECT_NAME} ws2_32) + target_link_libraries(${PROJECT_NAME} iphlpapi) endif(MINGW) get_filename_component(_vs_bin_path "${CMAKE_LINKER}" DIRECTORY) -## -## INSTALL -## install header files, generate .lib or .dll file. -## +# +# INSTALL install header files, generate .lib or .dll file. +# install( - TARGETS ${PROJECT_NAME} - EXPORT ${PROJECT_NAME} - LIBRARY DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/build/bin/ - RUNTIME DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/build/bin/ - CONFIGURATIONS DEBUG|RELEASE - ) + TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME} + LIBRARY DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/build/bin/ + RUNTIME DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/build/bin/ + CONFIGURATIONS DEBUG|RELEASE) install(FILES ${H_FILES} DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/build/include) - -if (NOT BUILD_SHARED_LIBS) -if(MSVC) - # execute_process( - # COMMAND "${_vs_bin_path}/lib.exe" "/machine:x64" /out:${CMAKE_BINARY_DIR}/lib/rf60XMerge.lib ${CMAKE_BINARY_DIR}/lib/rf60Xsdk.lib ${CMAKE_BINARY_DIR}/lib/rf60Xcore.lib - # WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/lib" - # OUTPUT_VARIABLE OutVar - # ERROR_VARIABLE ErrVar - # RESULT_VARIABLE ExitCode) - -# add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD -# COMMAND "${_vs_bin_path}/lib.exe" "/machine:x64" /out:${CMAKE_BINARY_DIR}/lib/rf60XMerge.lib ${CMAKE_BINARY_DIR}/lib/rf60Xsdk.lib ${CMAKE_BINARY_DIR}/lib/rf60Xcore.lib -# WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/lib" -# VERBATIM -# COMMENT "Running merge..." -# ) - - -endif(MSVC) +if(NOT BUILD_SHARED_LIBS) + if(MSVC) + # execute_process( COMMAND "${_vs_bin_path}/lib.exe" "/machine:x64" + # /out:${CMAKE_BINARY_DIR}/lib/rf60XMerge.lib + # ${CMAKE_BINARY_DIR}/lib/rf60Xsdk.lib ${CMAKE_BINARY_DIR}/lib/rf60Xcore.lib + # WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/lib" OUTPUT_VARIABLE OutVar + # ERROR_VARIABLE ErrVar RESULT_VARIABLE ExitCode) + + # add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND + # "${_vs_bin_path}/lib.exe" "/machine:x64" + # /out:${CMAKE_BINARY_DIR}/lib/rf60XMerge.lib + # ${CMAKE_BINARY_DIR}/lib/rf60Xsdk.lib ${CMAKE_BINARY_DIR}/lib/rf60Xcore.lib + # WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/lib" VERBATIM COMMENT "Running + # merge..." ) + + endif(MSVC) endif(BUILD_SHARED_LIBS) - - diff --git a/wrappers/Cpp/rf60Xsdk/rf60Xsdk.cpp b/wrappers/Cpp/rf60Xsdk/rf60Xsdk.cpp index c0f3e96..7704c0f 100644 --- a/wrappers/Cpp/rf60Xsdk/rf60Xsdk.cpp +++ b/wrappers/Cpp/rf60Xsdk/rf60Xsdk.cpp @@ -97,6 +97,31 @@ static unsigned char auchCRCHi[] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40}; +int checkLowByteConsistency(const unsigned char byte, rfBool reset) { + static unsigned char lowByte = 0; + + // if (lowByte==0 || reset == 0) { + // // Если это первый байт, сохраняем его как младший байт для + // последующих сравнений lowByte = byte & 0xF0;; + // } + + // if ((byte & 0xF0) != lowByte) { + // lowByte = 0; // Сбрасываем младший байт при обнаружении + // несоответствия return 0; + // } + + // return 1; + + if (reset == 0) { + lowByte = byte & 0xF0; + } else if (lowByte == 0 || (byte & 0xF0) != lowByte) { + lowByte = 0; + return 0; + } + + return 1; +} + uint16_t CRC16(unsigned char *puchMsg, uint16_t usDataLen) { unsigned char uchCRCHi = 0xFF; unsigned char uchCRCLo = 0xFF; @@ -135,7 +160,7 @@ bool rf60x::connect() { result = connect_to_scanner_udp((scanner_base_t *)scanner_base, - kRIFTEK_BIN_PROTOCOL); // FIXME ошибка конвертации + kRIFTEK_BIN_PROTOCOL); // FIXME ошибка конвертации return result; } @@ -151,37 +176,37 @@ bool rf60x::get_measure_udp(void *measure) { rf60x_network_udp_measure_t *measure_from_scanner = get_measure_from_scanner_udp( (scanner_base_t *)scanner_base, - kRIFTEK_BIN_PROTOCOL); // FIXME исправить для различных сканеров + kRIFTEK_BIN_PROTOCOL); // FIXME исправить для различных сканеров if (measure_from_scanner == NULL) { return false; } switch (((scanner_base_t *)scanner_base)->type) { - case 1: { - memcpy((udp_measure_t *)measure, measure_from_scanner->rf60x_udp_measure, - sizeof(udp_measure_t)); + case 1: { + memcpy((udp_measure_t *)measure, measure_from_scanner->rf60x_udp_measure, + sizeof(udp_measure_t)); - memory_platform.rf_free(measure_from_scanner->rf60x_udp_measure); - memory_platform.rf_free(measure_from_scanner); - break; - } - case 2: { - memcpy(measure, measure_from_scanner->rf60xB_udp_measure, - sizeof(rf60xB_udp_measure_t)); + memory_platform.rf_free(measure_from_scanner->rf60x_udp_measure); + memory_platform.rf_free(measure_from_scanner); + break; + } + case 2: { + memcpy(measure, measure_from_scanner->rf60xB_udp_measure, + sizeof(rf60xB_udp_measure_t)); - memory_platform.rf_free(measure_from_scanner->rf60xB_udp_measure); - memory_platform.rf_free(measure_from_scanner); - break; - } - case 3: { - memcpy(measure, measure_from_scanner->rf60xB_udp_measure, - sizeof(rf60xB_udp_measure_t)); + memory_platform.rf_free(measure_from_scanner->rf60xB_udp_measure); + memory_platform.rf_free(measure_from_scanner); + break; + } + case 3: { + memcpy(measure, measure_from_scanner->rf60xB_udp_measure, + sizeof(rf60xB_udp_measure_t)); - memory_platform.rf_free(measure_from_scanner->rf60xB_udp_measure); - memory_platform.rf_free(measure_from_scanner); - break; - } + memory_platform.rf_free(measure_from_scanner->rf60xB_udp_measure); + memory_platform.rf_free(measure_from_scanner); + break; + } } return true; @@ -199,7 +224,8 @@ uart_hello_t rf60x::hello_msg_uart() { rf60x_uart_hello_t *tempReturnValue = ::hello_msg_uart((scanner_base_t *)scanner_base); - if (tempReturnValue == NULL) return tempHello; + if (tempReturnValue == NULL) + return tempHello; memory_platform.rf_memcpy(&tempHello, tempReturnValue, sizeof(uart_hello_t)); @@ -229,130 +255,129 @@ bool rf60x::get_measure_uart(void *measure, PROTOCOL_MEASURE_UART type) { // START_STREAM_MODIFIED = 0x0D, switch (type) { - case PROTOCOL_MEASURE_UART::UART_STREAM_MEASURE_T: { - rfUint8 tempCNT = (measure_from_scanner_uart_ti[0] & 0x30) >> 4; - - if ((measure_from_scanner_uart_ti[1] & 0x30) >> 4 != tempCNT || - (measure_from_scanner_uart_ti[2] & 0x30) >> 4 != tempCNT || - (measure_from_scanner_uart_ti[3] & 0x30) >> 4 != tempCNT) - return false; - - reinterpret_cast(measure)->count = tempCNT; - - reinterpret_cast(measure)->value = - (measure_from_scanner_uart_ti[0] & 0x0F) | - (measure_from_scanner_uart_ti[1] & 0x0F) << 4 | - (measure_from_scanner_uart_ti[2] & 0x0F) << 8 | - (measure_from_scanner_uart_ti[3] & 0x0F) << 12; - reinterpret_cast(measure)->status = - (measure_from_scanner_uart_ti[0] & 0x40) >> 6; - - memory_platform.rf_free(measure_from_scanner_uart_ti); - break; - } - case PROTOCOL_MEASURE_UART::UART_RESULT_WITH_ENCODER_T: { - reinterpret_cast(measure)->value = - (measure_from_scanner_uart_ti[0] & 0x0F) | - (measure_from_scanner_uart_ti[1] & 0x0F) << 4 | - (measure_from_scanner_uart_ti[2] & 0x0F) << 8 | - (measure_from_scanner_uart_ti[3] & 0x0F) << 12; - reinterpret_cast(measure) - ->rotationMarks = (measure_from_scanner_uart_ti[4] & 0x0F) | - (measure_from_scanner_uart_ti[5] & 0x0F) << 4 | - (measure_from_scanner_uart_ti[6] & 0x0F) << 8 | - (measure_from_scanner_uart_ti[7] & 0x0F) << 12; - reinterpret_cast(measure) - ->encoderValue = (measure_from_scanner_uart_ti[8] & 0x0F) | - (measure_from_scanner_uart_ti[9] & 0x0F) << 4 | - (measure_from_scanner_uart_ti[10] & 0x0F) << 8 | - (measure_from_scanner_uart_ti[11] & 0x0F) << 12; - - memory_platform.rf_free(measure_from_scanner_uart_ti); - break; - } - case PROTOCOL_MEASURE_UART::UART_STREAM_ADVANCED_MEASURE_T: { - rfUint8 tempCNT = (measure_from_scanner_uart_ti[0] & 0x30) >> 4; - - // printf("%d %d %d %d",(measure_from_scanner_uart_ti[0] & 0x30) >> - // 4,(measure_from_scanner_uart_ti[1] & 0x30) >> - // 4,(measure_from_scanner_uart_ti[2] & 0x30) >> - // 4,(measure_from_scanner_uart_ti[3] & 0x30) >> 4); - if ((measure_from_scanner_uart_ti[1] & 0x30) >> 4 != tempCNT || - (measure_from_scanner_uart_ti[2] & 0x30) >> 4 != tempCNT || - (measure_from_scanner_uart_ti[3] & 0x30) >> 4 != tempCNT) - return false; - - reinterpret_cast(measure)->cnt = - tempCNT; - - reinterpret_cast(measure)->value = - (measure_from_scanner_uart_ti[0] & 0x0F) | - (measure_from_scanner_uart_ti[1] & 0x0F) << 4 | - (measure_from_scanner_uart_ti[2] & 0x0F) << 8 | - (measure_from_scanner_uart_ti[3] & 0x0F) << 12; - reinterpret_cast(measure)->status = - (measure_from_scanner_uart_ti[0] & 0x40) >> 6; - - uint8_t advanced = (measure_from_scanner_uart_ti[4] & 0x0F) | - (measure_from_scanner_uart_ti[5] & 0x0F) << 4; - - reinterpret_cast(measure)->status = - (advanced >> 0) & 1; - - reinterpret_cast(measure)->dir = - (advanced >> 1) & 1; - - reinterpret_cast(measure)->cnt = - (advanced >> 2) & 1; - - memory_platform.rf_free(measure_from_scanner_uart_ti); - break; - } - - case PROTOCOL_MEASURE_UART::UART_STREAM_MODIFIED_MEASURE_T: { - rfUint8 tempCNT = (measure_from_scanner_uart_ti[0] & 0x30) >> 4; - - // printf("%d %d %d %d",(measure_from_scanner_uart_ti[0] & 0x30) >> - // 4,(measure_from_scanner_uart_ti[1] & 0x30) >> - // 4,(measure_from_scanner_uart_ti[2] & 0x30) >> - // 4,(measure_from_scanner_uart_ti[3] & 0x30) >> 4); - if ((measure_from_scanner_uart_ti[1] & 0x30) >> 4 != tempCNT || - (measure_from_scanner_uart_ti[2] & 0x30) >> 4 != tempCNT || - (measure_from_scanner_uart_ti[3] & 0x30) >> 4 != tempCNT) - return false; - - // reinterpret_cast(measure)->cnt = - // tempCNT; - - reinterpret_cast(measure)->value = - (measure_from_scanner_uart_ti[0] & 0x0F) | - (measure_from_scanner_uart_ti[1] & 0x0F) << 4 | - (measure_from_scanner_uart_ti[2] & 0x0F) << 8 | - (measure_from_scanner_uart_ti[3] & 0x0F) << 12; - reinterpret_cast(measure)->status = - (measure_from_scanner_uart_ti[0] & 0x40) >> 6; - - uint8_t advanced = (measure_from_scanner_uart_ti[4] & 0x0F) | - (measure_from_scanner_uart_ti[1] & 0x0F) << 4; - - reinterpret_cast(measure)->status = - (advanced >> 0) & 1; - - reinterpret_cast(measure)->dir = - (advanced >> 1) & 1; - - reinterpret_cast(measure)->cnt = - (advanced >> 2) & 1; - - reinterpret_cast(measure)->cnt_stream = - (measure_from_scanner_uart_ti[6] & 0x0F) | - (measure_from_scanner_uart_ti[7] & 0x0F) << 4 | - (measure_from_scanner_uart_ti[8] & 0x0F) << 8 | - (measure_from_scanner_uart_ti[9] & 0x0F) << 12; - - memory_platform.rf_free(measure_from_scanner_uart_ti); - break; - } + case PROTOCOL_MEASURE_UART::UART_STREAM_MEASURE_T: { + rfUint8 tempCNT = (measure_from_scanner_uart_ti[0] & 0x30) >> 4; + + if ((measure_from_scanner_uart_ti[1] & 0x30) >> 4 != tempCNT || + (measure_from_scanner_uart_ti[2] & 0x30) >> 4 != tempCNT || + (measure_from_scanner_uart_ti[3] & 0x30) >> 4 != tempCNT) + return false; + + reinterpret_cast(measure)->count = tempCNT; + + reinterpret_cast(measure)->value = + (measure_from_scanner_uart_ti[0] & 0x0F) | + (measure_from_scanner_uart_ti[1] & 0x0F) << 4 | + (measure_from_scanner_uart_ti[2] & 0x0F) << 8 | + (measure_from_scanner_uart_ti[3] & 0x0F) << 12; + reinterpret_cast(measure)->status = + (measure_from_scanner_uart_ti[0] & 0x40) >> 6; + + memory_platform.rf_free(measure_from_scanner_uart_ti); + break; + } + case PROTOCOL_MEASURE_UART::UART_RESULT_WITH_ENCODER_T: { + reinterpret_cast(measure)->value = + (measure_from_scanner_uart_ti[0] & 0x0F) | + (measure_from_scanner_uart_ti[1] & 0x0F) << 4 | + (measure_from_scanner_uart_ti[2] & 0x0F) << 8 | + (measure_from_scanner_uart_ti[3] & 0x0F) << 12; + reinterpret_cast(measure) + ->rotationMarks = (measure_from_scanner_uart_ti[4] & 0x0F) | + (measure_from_scanner_uart_ti[5] & 0x0F) << 4 | + (measure_from_scanner_uart_ti[6] & 0x0F) << 8 | + (measure_from_scanner_uart_ti[7] & 0x0F) << 12; + reinterpret_cast(measure) + ->encoderValue = (measure_from_scanner_uart_ti[8] & 0x0F) | + (measure_from_scanner_uart_ti[9] & 0x0F) << 4 | + (measure_from_scanner_uart_ti[10] & 0x0F) << 8 | + (measure_from_scanner_uart_ti[11] & 0x0F) << 12; + + memory_platform.rf_free(measure_from_scanner_uart_ti); + break; + } + case PROTOCOL_MEASURE_UART::UART_STREAM_ADVANCED_MEASURE_T: { + rfUint8 tempCNT = (measure_from_scanner_uart_ti[0] & 0x30) >> 4; + + // printf("%d %d %d %d",(measure_from_scanner_uart_ti[0] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[1] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[2] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[3] & 0x30) >> 4); + if ((measure_from_scanner_uart_ti[1] & 0x30) >> 4 != tempCNT || + (measure_from_scanner_uart_ti[2] & 0x30) >> 4 != tempCNT || + (measure_from_scanner_uart_ti[3] & 0x30) >> 4 != tempCNT) + return false; + + reinterpret_cast(measure)->cnt = tempCNT; + + reinterpret_cast(measure)->value = + (measure_from_scanner_uart_ti[0] & 0x0F) | + (measure_from_scanner_uart_ti[1] & 0x0F) << 4 | + (measure_from_scanner_uart_ti[2] & 0x0F) << 8 | + (measure_from_scanner_uart_ti[3] & 0x0F) << 12; + reinterpret_cast(measure)->status = + (measure_from_scanner_uart_ti[0] & 0x40) >> 6; + + uint8_t advanced = (measure_from_scanner_uart_ti[4] & 0x0F) | + (measure_from_scanner_uart_ti[5] & 0x0F) << 4; + + reinterpret_cast(measure)->status = + (advanced >> 0) & 1; + + reinterpret_cast(measure)->dir = + (advanced >> 1) & 1; + + reinterpret_cast(measure)->cnt = + (advanced >> 2) & 1; + + memory_platform.rf_free(measure_from_scanner_uart_ti); + break; + } + + case PROTOCOL_MEASURE_UART::UART_STREAM_MODIFIED_MEASURE_T: { + rfUint8 tempCNT = (measure_from_scanner_uart_ti[0] & 0x30) >> 4; + + // printf("%d %d %d %d",(measure_from_scanner_uart_ti[0] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[1] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[2] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[3] & 0x30) >> 4); + if ((measure_from_scanner_uart_ti[1] & 0x30) >> 4 != tempCNT || + (measure_from_scanner_uart_ti[2] & 0x30) >> 4 != tempCNT || + (measure_from_scanner_uart_ti[3] & 0x30) >> 4 != tempCNT) + return false; + + // reinterpret_cast(measure)->cnt = + // tempCNT; + + reinterpret_cast(measure)->value = + (measure_from_scanner_uart_ti[0] & 0x0F) | + (measure_from_scanner_uart_ti[1] & 0x0F) << 4 | + (measure_from_scanner_uart_ti[2] & 0x0F) << 8 | + (measure_from_scanner_uart_ti[3] & 0x0F) << 12; + reinterpret_cast(measure)->status = + (measure_from_scanner_uart_ti[0] & 0x40) >> 6; + + uint8_t advanced = (measure_from_scanner_uart_ti[4] & 0x0F) | + (measure_from_scanner_uart_ti[1] & 0x0F) << 4; + + reinterpret_cast(measure)->status = + (advanced >> 0) & 1; + + reinterpret_cast(measure)->dir = + (advanced >> 1) & 1; + + reinterpret_cast(measure)->cnt = + (advanced >> 2) & 1; + + reinterpret_cast(measure)->cnt_stream = + (measure_from_scanner_uart_ti[6] & 0x0F) | + (measure_from_scanner_uart_ti[7] & 0x0F) << 4 | + (measure_from_scanner_uart_ti[8] & 0x0F) << 8 | + (measure_from_scanner_uart_ti[9] & 0x0F) << 12; + + memory_platform.rf_free(measure_from_scanner_uart_ti); + break; + } } return true; @@ -367,12 +392,14 @@ bool rf60x::get_single_measure(uint8_t address, void *measure) { scanner_base_ti->rf60x->user_params.network_address_uart = address; bool result = this->send_command(COMMAND_UART::GETRESULT); - if (!result) return FALSE; + if (!result) + return FALSE; result = this->get_measure_uart(measure, PROTOCOL_MEASURE_UART::UART_STREAM_MEASURE_T); - if (!result) return FALSE; + if (!result) + return FALSE; scanner_base_ti->rf60x->user_params.network_address_uart = tempAddress; return result; @@ -1340,54 +1367,247 @@ void rf60x::test_close_serial_port() { serial_manager->close_serial_port(); } bool rf60x::test_get_single_measure() { return true; } -bool rf60x::test_send_custom_command(uint8_t command, uint8_t size, - void *value){ +bool rf60x::test_read_custom_command(uint8_t command, uint8_t size, + std::vector &vec_ref) { - serial_manager->set_timeout(std::chrono::milliseconds(500)); - return serial_manager->read_command(static_cast(value),size); + serial_manager->set_timeout( + std::chrono::milliseconds(500)); // TODO move to method -} + char tempByteBuffer = 0; + + // bool resultReturn=0; + + // for(uint32_t i=0; iread_command(&tempByteBuffer,sizeof(size)); + + // if(resultReturn<=0){ + // break; + // } -bool rf60x::test_write_custom_command(uint8_t command, uint8_t size, char *data) -{ + // if(checkLowByteConsistency(tempByteBuffer,1)){ - char ucBuffer[4]; - ucBuffer[0] =(1 & 0xFF); - ucBuffer[1] = 0x80 | 0x02; // - ///////////////////////////////////////////////////////////////////////////////////////////// - ucBuffer[2] = 0x80 | (command & 0x0F); - ucBuffer[3] = 0x80| ((command >> 4) & 0x0F); + // vec_ref.emplace_back(tempByteBuffer); + // }else{ + // vec_ref.clear(); + // if(checkLowByteConsistency(tempByteBuffer,0)){ + // vec_ref.emplace_back(tempByteBuffer); + // } - return serial_manager->write_command(ucBuffer,size); + // } + + // } + + while (vec_ref.size() != size) { + + try { + + if (!serial_manager->read_command(&tempByteBuffer, sizeof(size))) { + return false; + } + + bool isConsistent = checkLowByteConsistency(tempByteBuffer, 1); + if (isConsistent || checkLowByteConsistency(tempByteBuffer, 0)) { + vec_ref.emplace_back(tempByteBuffer); + } else { + vec_ref.clear(); + vec_ref.emplace_back(tempByteBuffer); + } + } catch (std::exception &e) { + return false; + } + } + + return true; } -bool rf60x::test_get_ip_address() -{ - uint8_t ip_values[4]; - int8_t result=0; - char ucBuffer[4]; +bool rf60x::test_write_custom_command(uint8_t command, uint8_t size, + char *data) { + char ucBuffer[4]; + ucBuffer[0] = (1 & 0xFF); + ucBuffer[1] = 0x80 | (command & 0x0F); // + ///////////////////////////////////////////////////////////////////////////////////////////// + // ucBuffer[2] = 0x80 | (command & 0x0F); + // ucBuffer[3] = 0x80 | ((command >> 4) & 0x0F); + return serial_manager->write_command(ucBuffer, size); +} - try { - if(!test_write_custom_command(0x78,4,ucBuffer)){ +bool rf60x::test_get_measure_uart(void *measure, PROTOCOL_MEASURE_UART type) { - return false; - } + char tempByteBuffer = 0; + size_t sizeType = static_cast(type); + size_t sequenceLength = 0; - result = test_send_custom_command( - CODE::PARAM_NAME_KEY::ZERO_BYTE_OF_SOURCE_IP_ADDRESS, 2, &ip_values[0]); - if (result == -1) { - return false; + char tempButeBufferArray[12]; + + while (sequenceLength != sizeType) { + + try { + + if (!serial_manager->read_command(&tempByteBuffer, 1)) { + return false; + } + + bool isConsistent = checkLowByteConsistency(tempByteBuffer, 1); + if (isConsistent || checkLowByteConsistency(tempByteBuffer, 0)) { + tempButeBufferArray[sequenceLength++] = tempByteBuffer; + } else { + + std::fill_n(tempButeBufferArray, sizeType, 0); + sequenceLength = 0; + tempButeBufferArray[sequenceLength++] = tempByteBuffer; + } + } catch (std::exception &e) { + + std::cout << "Error: " << e.what() << std::endl; + return false; + } + } + + switch (type) { + case PROTOCOL_MEASURE_UART::UART_STREAM_MEASURE_T: { + uint8_t tempCNT = (tempButeBufferArray[0] & 0x30) >> 4; + + if ((tempButeBufferArray[1] & 0x30) >> 4 != tempCNT || + (tempButeBufferArray[2] & 0x30) >> 4 != tempCNT || + (tempButeBufferArray[3] & 0x30) >> 4 != tempCNT) + return false; + + reinterpret_cast(measure)->count = tempCNT; + + reinterpret_cast(measure)->value = + (tempButeBufferArray[0] & 0x0F) | (tempButeBufferArray[1] & 0x0F) << 4 | + (tempButeBufferArray[2] & 0x0F) << 8 | + (tempButeBufferArray[3] & 0x0F) << 12; + reinterpret_cast(measure)->status = + (tempButeBufferArray[0] & 0x40) >> 6; + + break; + } + case PROTOCOL_MEASURE_UART::UART_RESULT_WITH_ENCODER_T: { + reinterpret_cast(measure)->value = + (tempButeBufferArray[0] & 0x0F) | (tempButeBufferArray[1] & 0x0F) << 4 | + (tempButeBufferArray[2] & 0x0F) << 8 | + (tempButeBufferArray[3] & 0x0F) << 12; + reinterpret_cast(measure) + ->rotationMarks = (tempButeBufferArray[4] & 0x0F) | + (tempButeBufferArray[5] & 0x0F) << 4 | + (tempButeBufferArray[6] & 0x0F) << 8 | + (tempButeBufferArray[7] & 0x0F) << 12; + reinterpret_cast(measure) + ->encoderValue = (tempButeBufferArray[8] & 0x0F) | + (tempButeBufferArray[9] & 0x0F) << 4 | + (tempButeBufferArray[10] & 0x0F) << 8 | + (tempButeBufferArray[11] & 0x0F) << 12; + break; + } + case PROTOCOL_MEASURE_UART::UART_STREAM_ADVANCED_MEASURE_T: { + rfUint8 tempCNT = (tempButeBufferArray[0] & 0x30) >> 4; + + // printf("%d %d %d %d",(measure_from_scanner_uart_ti[0] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[1] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[2] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[3] & 0x30) >> 4); + if ((tempButeBufferArray[1] & 0x30) >> 4 != tempCNT || + (tempButeBufferArray[2] & 0x30) >> 4 != tempCNT || + (tempButeBufferArray[3] & 0x30) >> 4 != tempCNT) + return false; + + reinterpret_cast(measure)->cnt = tempCNT; + + reinterpret_cast(measure)->value = + (tempButeBufferArray[0] & 0x0F) | (tempButeBufferArray[1] & 0x0F) << 4 | + (tempButeBufferArray[2] & 0x0F) << 8 | + (tempButeBufferArray[3] & 0x0F) << 12; + reinterpret_cast(measure)->status = + (tempButeBufferArray[0] & 0x40) >> 6; + + uint8_t advanced = + (tempButeBufferArray[4] & 0x0F) | (tempButeBufferArray[5] & 0x0F) << 4; + + reinterpret_cast(measure)->status = + (advanced >> 0) & 1; + + reinterpret_cast(measure)->dir = + (advanced >> 1) & 1; + + reinterpret_cast(measure)->cnt = + (advanced >> 2) & 1; + + break; + } + + case PROTOCOL_MEASURE_UART::UART_STREAM_MODIFIED_MEASURE_T: { + rfUint8 tempCNT = (tempButeBufferArray[0] & 0x30) >> 4; + + // printf("%d %d %d %d",(measure_from_scanner_uart_ti[0] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[1] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[2] & 0x30) >> + // 4,(measure_from_scanner_uart_ti[3] & 0x30) >> 4); + if ((tempButeBufferArray[1] & 0x30) >> 4 != tempCNT || + (tempButeBufferArray[2] & 0x30) >> 4 != tempCNT || + (tempButeBufferArray[3] & 0x30) >> 4 != tempCNT) + return false; + + // reinterpret_cast(measure)->cnt = + // tempCNT; + + reinterpret_cast(measure)->value = + (tempButeBufferArray[0] & 0x0F) | (tempButeBufferArray[1] & 0x0F) << 4 | + (tempButeBufferArray[2] & 0x0F) << 8 | + (tempButeBufferArray[3] & 0x0F) << 12; + reinterpret_cast(measure)->status = + (tempButeBufferArray[0] & 0x40) >> 6; + + uint8_t advanced = + (tempButeBufferArray[4] & 0x0F) | (tempButeBufferArray[1] & 0x0F) << 4; + + reinterpret_cast(measure)->status = + (advanced >> 0) & 1; + + reinterpret_cast(measure)->dir = + (advanced >> 1) & 1; + + reinterpret_cast(measure)->cnt = + (advanced >> 2) & 1; + + reinterpret_cast(measure)->cnt_stream = + (tempButeBufferArray[6] & 0x0F) | (tempButeBufferArray[7] & 0x0F) << 4 | + (tempButeBufferArray[8] & 0x0F) << 8 | + (tempButeBufferArray[9] & 0x0F) << 12; + + break; + } } - std::cout<<"IP ADDRESS : "<<+ip_values[0]< ip_values; + bool result = 0; + char ucBuffer[4]; + + try { + if (!test_write_custom_command(0x78, 4, ucBuffer)) { + + return false; + } + + if (!test_read_custom_command( + CODE::PARAM_NAME_KEY::ZERO_BYTE_OF_SOURCE_IP_ADDRESS, 2, + ip_values)) { + return false; + } + + std::cout << "IP ADDRESS : " << +ip_values[0] << std::endl; + return true; + } catch (std::exception &e) { + std::cout << "Error: " << e.what() << std::endl; + return false; + } } param_t *rf60x::get_param(std::string param_name) { @@ -1505,6 +1725,6 @@ rf60x::~rf60x() { _flushall(); } -} // namespace RF60X -} // namespace SCANNERS -} // namespace SDK +} // namespace RF60X +} // namespace SCANNERS +} // namespace SDK diff --git a/wrappers/Cpp/rf60Xsdk/rf60Xsdk.h b/wrappers/Cpp/rf60Xsdk/rf60Xsdk.h index 9085c3b..67f1de2 100644 --- a/wrappers/Cpp/rf60Xsdk/rf60Xsdk.h +++ b/wrappers/Cpp/rf60Xsdk/rf60Xsdk.h @@ -36,12 +36,12 @@ API_EXPORT bool sdk_init(); API_EXPORT void sdk_cleanup(); class API_EXPORT rf60x { - public: +public: bool connect(); bool disconnect(); - bool get_measure_udp(void* measure); + bool get_measure_udp(void *measure); bool open_port_uart(); @@ -51,26 +51,27 @@ class API_EXPORT rf60x { bool close_port_uart(); - bool get_measure_uart(void* measure, PROTOCOL_MEASURE_UART type); + bool get_measure_uart(void *measure, PROTOCOL_MEASURE_UART type); - bool get_single_measure(uint8_t address, void* measure); + bool get_single_measure(uint8_t address, void *measure); - bool send_command_modbus(modbus_rtu_command& command); + bool send_command_modbus(modbus_rtu_command &command); - bool receive_data_modbus(uint16_t* buffer); + bool receive_data_modbus(uint16_t *buffer); bool read_params_uart(); bool read_params_by_custom_command_uart(uint8_t command, uint8_t size, - void* value); + void *value); bool write_params_uart(); - bool write_params_code_of_parameter_uart( - CODE::PARAM_NAME_KEY code_of_parameter, uint16_t value); + bool + write_params_code_of_parameter_uart(CODE::PARAM_NAME_KEY code_of_parameter, + uint16_t value); bool write_params_by_custom_command_uart(uint8_t command, uint8_t size, - void* value); + void *value); bool send_command(COMMAND_UART CMD); @@ -126,57 +127,57 @@ class API_EXPORT rf60x { bool set_protol_type(uint8_t value); - std::pair get_sensor(); - std::pair get_analog_out(); + std::pair get_sensor(); + std::pair get_analog_out(); // bool get_AL_mode();//TODO надо проработать биты // bool get_averaging_mode(); // bool get_analog_ouput_mode(); // bool get_sampling_mode(); - std::pair get_network_address(); + std::pair get_network_address(); - std::pair get_baute_rate(); + std::pair get_baute_rate(); - std::pair get_number_of_averaged_values(); + std::pair get_number_of_averaged_values(); - std::pair get_sampling_period(); + std::pair get_sampling_period(); - std::pair get_maximum_integration_time(); + std::pair get_maximum_integration_time(); - std::pair get_begin_analog_range(); + std::pair get_begin_analog_range(); - std::pair get_end_analog_range(); + std::pair get_end_analog_range(); - std::pair get_time_lock_of_result(); + std::pair get_time_lock_of_result(); - std::pair get_zero_point(); + std::pair get_zero_point(); - std::pair get_CAN_interface_speed(); + std::pair get_CAN_interface_speed(); - std::pair get_standard_identifier(); + std::pair get_standard_identifier(); - std::pair get_extend_identifier(); + std::pair get_extend_identifier(); - std::pair get_CAN_interface_identifier(); + std::pair get_CAN_interface_identifier(); - std::pair get_CAN_interface(); + std::pair get_CAN_interface(); - std::pair get_destination_ip_address(); + std::pair get_destination_ip_address(); - std::pair get_gateway_ip_address(); + std::pair get_gateway_ip_address(); - std::pair get_subnet_mask(); + std::pair get_subnet_mask(); - std::pair get_source_ip_address(); + std::pair get_source_ip_address(); - std::pair get_number_of_measurements_in_the_packet(); + std::pair get_number_of_measurements_in_the_packet(); - std::pair get_ethernet_inteface(); + std::pair get_ethernet_inteface(); - std::pair get_autostart_of_measurement_stream(); + std::pair get_autostart_of_measurement_stream(); - std::pair get_protol_type(); + std::pair get_protol_type(); bool test_open_serial_port(std::string devName); @@ -184,33 +185,36 @@ class API_EXPORT rf60x { bool test_get_single_measure(); - bool test_send_custom_command(uint8_t command, uint8_t size,void* value); + bool test_read_custom_command(uint8_t command, uint8_t size, + std::vector &vec_ref); bool test_write_custom_command(uint8_t command, uint8_t size, char *data); + bool test_get_measure_uart(void *measure, PROTOCOL_MEASURE_UART type); + bool test_get_ip_address(); - param_t* get_param(std::string param_name); - param_t* get_param(PARAM_NAME_KEY param_name); + param_t *get_param(std::string param_name); + param_t *get_param(PARAM_NAME_KEY param_name); - bool set_param(param_t* param); - bool set_param(const char* param_name, ...); + bool set_param(param_t *param); + bool set_param(const char *param_name, ...); bool set_param(int param_id, ...); - rf60x(const config_base_information_rf60x_t& config); - rf60x(const rf60x&& obj) = delete; - rf60x(const rf60x& obj) = delete; - rf60x& operator=(const rf60x& x) = delete; - rf60x& operator=(rf60x&&) = delete; + rf60x(const config_base_information_rf60x_t &config); + rf60x(const rf60x &&obj) = delete; + rf60x(const rf60x &obj) = delete; + rf60x &operator=(const rf60x &x) = delete; + rf60x &operator=(rf60x &&) = delete; ~rf60x(); - private: - void* scanner_base = nullptr; +private: + void *scanner_base = nullptr; uint8_t m_count_modbus; config_base_information_rf60x_t current_config; std::unique_ptr serial_manager; }; -} // namespace RF60X -} // namespace SCANNERS -} // namespace SDK +} // namespace RF60X +} // namespace SCANNERS +} // namespace SDK diff --git a/wrappers/Cpp/rf60Xsdk/serialmanager.cpp b/wrappers/Cpp/rf60Xsdk/serialmanager.cpp index 7891c56..2dbf5d4 100644 --- a/wrappers/Cpp/rf60Xsdk/serialmanager.cpp +++ b/wrappers/Cpp/rf60Xsdk/serialmanager.cpp @@ -1,29 +1,30 @@ #include "serialmanager.h" -bool SerialManager::open_serial_port(std::string devName) -{ - - - if(port.is_open()) return false; - port.open(devName); - port.set_option(asio::serial_port_base::baud_rate(static_cast(baud_rate))); - port.set_option(asio::serial_port_base::parity(asio::serial_port_base::parity::even)); - port.set_option(asio::serial_port_base::character_size(8)); - port.set_option(asio::serial_port_base::flow_control(asio::serial_port_base::flow_control::none)); - port.set_option(asio::serial_port_base::stop_bits(asio::serial_port_base::stop_bits::one)); - return true; - +bool SerialManager::open_serial_port(std::string devName) { + + if (port.is_open()) + return false; + port.open(devName); + port.set_option( + asio::serial_port_base::baud_rate(static_cast(baud_rate))); + port.set_option( + asio::serial_port_base::parity(asio::serial_port_base::parity::even)); + port.set_option(asio::serial_port_base::character_size(8)); + port.set_option(asio::serial_port_base::flow_control( + asio::serial_port_base::flow_control::none)); + port.set_option(asio::serial_port_base::stop_bits( + asio::serial_port_base::stop_bits::one)); + return true; } -void SerialManager::close_serial_port() -{ - if(port.is_open()==false) return; - port.close(); +void SerialManager::close_serial_port() { + if (port.is_open() == false) + return; + port.close(); } -bool SerialManager::read_command(char *data, size_t size) -{ - if(readData.size()>0)//If there is some data from a previous read +bool SerialManager::read_command(char *data, size_t size) { + /* if(readData.size()>0)//If there is some data from a previous read { std::istream is(&readData); size_t toRead=std::min(readData.size(),size);//How many bytes to read? @@ -31,37 +32,37 @@ bool SerialManager::read_command(char *data, size_t size) data+=toRead; size-=toRead; if(size==0) return false;//If read data was enough, just return - } - - - asio::async_read(port,asio::buffer(data,size),std::bind(&SerialManager::read_completed,this,std::placeholders::_1, - std::placeholders::_2)); + }*/ - //For this code to work, there should always be a timeout, so the - //request for no timeout is translated into a very long timeout + io.reset(); + asio::async_read(port, asio::buffer(data, size), + std::bind(&SerialManager::read_completed, this, + std::placeholders::_1, std::placeholders::_2)); - if (timeout != std::chrono::seconds(0)) { - timer.expires_from_now(timeout); - } else { - timer.expires_from_now(asio::chrono::hours(100000)); - } - + // For this code to work, there should always be a timeout, so the + // request for no timeout is translated into a very long timeout -// auto future= std::async(std::launch::async, [=]() { -// std::this_thread::sleep_for(timer); -// timeout_expired(std::make_error_code(std::errc::timed_out)); // Вызов callback функции -// }); + if (timeout != std::chrono::seconds(0)) { + timer.expires_from_now(timeout); + } else { + timer.expires_from_now(asio::chrono::hours(100000)); + } + // auto future= std::async(std::launch::async, [=]() { + // std::this_thread::sleep_for(timer); + // timeout_expired(std::make_error_code(std::errc::timed_out)); // + // Вызов callback функции + // }); - timer.async_wait(std::bind(&SerialManager::timeout_expired,this, - std::placeholders::_1)); + timer.async_wait( + std::bind(&SerialManager::timeout_expired, this, std::placeholders::_1)); /* timer.async_wait(std::bind(&Serial::timeoutExpired,this, std::placeholders::error));*/ - - /* auto callback = std::bind(&SerialManager::timeout_expired, this, std::placeholders::_1); + /* auto callback = std::bind(&SerialManager::timeout_expired, this, + std::placeholders::_1); // Пример длительности ожидания: 1000 миллисекунд (1 секунда) auto asyncTask = std::async(std::launch::async, [=]() { @@ -69,88 +70,75 @@ bool SerialManager::read_command(char *data, size_t size) callback(error_code); });*/ - - - result=resultInProgress; - bytesTransferred=0; - for(;;) - { - io.run_one(); - switch(result) - { - case resultSuccess: - timer.cancel(); - return true; - case resultTimeoutExpired: - port.cancel(); - throw(timeout_exception("Timeout expired")); - case resultError: - timer.cancel(); - port.cancel(); - throw(std::system_error(std::error_code(), - "Error while reading")); - //if resultInProgress remain in the loop - case resultInProgress: - break; - } + result = resultInProgress; + bytesTransferred = 0; + for (;;) { + io.run_one(); + switch (result) { + case resultSuccess: + timer.cancel(); + return true; + case resultTimeoutExpired: + port.cancel(); + throw(timeout_exception("Timeout expired")); + case resultError: + timer.cancel(); + port.cancel(); + throw(std::system_error(std::error_code(), "Error while reading")); + // if resultInProgress remain in the loop + case resultInProgress: + break; } - + } } -void SerialManager::timeout_expired(const std::error_code& error) -{ - if(!error && result==resultInProgress){ - result=resultTimeoutExpired; - } - - +void SerialManager::timeout_expired(const std::error_code &error) { + if (!error && result == resultInProgress) { + result = resultTimeoutExpired; + } } -void SerialManager::read_completed(const std::error_code &error, const size_t bytesTransferred) -{ - if(!error) - { - result=resultSuccess; - this->bytesTransferred=bytesTransferred; - return; - } +void SerialManager::read_completed(const std::error_code &error, + const size_t bytesTransferred) { + if (!error) { + result = resultSuccess; + this->bytesTransferred = bytesTransferred; + return; + } - //In case a asynchronous operation is cancelled due to a timeout, - //each OS seems to have its way to react. + // In case a asynchronous operation is cancelled due to a timeout, + // each OS seems to have its way to react. #ifdef _WIN32 - if(error.value()==995) return; //Windows spits out error 995 + if (error.value() == 995) + return; // Windows spits out error 995 #elif defined(__APPLE__) - if(error.value()==45) - { - //Bug on OS X, it might be necessary to repeat the setup - //http://osdir.com/ml/lib.boost.asio.user/2008-08/msg00004.html - performReadSetup(setupParameters); - return; - } -#else //Linux - if(error.value()==125) return; //Linux outputs error 125 + if (error.value() == 45) { + // Bug on OS X, it might be necessary to repeat the setup + // http://osdir.com/ml/lib.boost.asio.user/2008-08/msg00004.html + performReadSetup(setupParameters); + return; + } +#else // Linux + if (error.value() == 125) + return; // Linux outputs error 125 #endif - result=resultError; + result = resultError; } +bool SerialManager::write_command(char *data, size_t size) { + size_t countByte = asio::write(port, asio::buffer(data, size)); + if (countByte != size) { + return false; + } - -bool SerialManager::write_command(char *data, size_t size) -{ - size_t countByte=asio::write(port,asio::buffer(data,size)); - if(countByte!=size){ - return false; - } - - return true; + return true; } -void SerialManager::set_timeout(const std::chrono::duration &t) -{ - timeout=t; +void SerialManager::set_timeout( + const std::chrono::duration &t) { + timeout = t; } -void SerialManager::setBaud_rate(NEW_WRAP::BAUR_RATE_UART newBaud_rate) -{ - baud_rate = newBaud_rate; +void SerialManager::setBaud_rate(NEW_WRAP::BAUR_RATE_UART newBaud_rate) { + baud_rate = newBaud_rate; } diff --git a/wrappers/Cpp/rf60Xsdk/serialmanager.h b/wrappers/Cpp/rf60Xsdk/serialmanager.h index 6914b34..c7dd324 100644 --- a/wrappers/Cpp/rf60Xsdk/serialmanager.h +++ b/wrappers/Cpp/rf60Xsdk/serialmanager.h @@ -1,84 +1,75 @@ #ifndef SERIALMANAGER_H #define SERIALMANAGER_H - //#include "communicationinterface.h" #include "asio.hpp" #include "asio/placeholders.hpp" #include "asio/ts/timer.hpp" #include "rf60Xtypes.h" -#include #include - +#include /** * @brief timeout_exception - This class is responsible * for exception handling. */ -class timeout_exception: public std::runtime_error -{ +class timeout_exception : public std::runtime_error { public: - timeout_exception(const std::string& arg): runtime_error(arg) {} + timeout_exception(const std::string &arg) : runtime_error(arg) {} }; - /** * @brief SerialManager - This class provides methods for * controlling the scanner via the serial port. */ -class SerialManager -{ +class SerialManager { public: - SerialManager():io(), port(io), timer(io), timeout(0) {}; + SerialManager() : io(), port(io), timer(io), timeout(0){}; - // CommunicationInterface interface + // CommunicationInterface interface public: - - /** - * Opens a serial device. - * \param address serial device name, example "/dev/ttyS0" or "COM1" - * \throws boost::system::system_error if cannot open the - * serial device - */ - - bool open_serial_port(std::string devName); - void close_serial_port(); - bool read_command(char *data, size_t size); - bool write_command(char *data, size_t size); - - void set_timeout(const std::chrono::duration& t); - void setBaud_rate(NEW_WRAP::BAUR_RATE_UART newBaud_rate); - void timeout_expired(const std::error_code& error); - - /** - * Possible outcome of a read. Set by callbacks, read from main code - */ - enum ReadResult - { - resultInProgress, - resultSuccess, - resultError, - resultTimeoutExpired - }; - - - void read_completed(const std::error_code& error, const size_t bytesTransferred); - - + /** + * Opens a serial device. + * \param address serial device name, example "/dev/ttyS0" or "COM1" + * \throws boost::system::system_error if cannot open the + * serial device + */ + + bool open_serial_port(std::string devName); + void close_serial_port(); + bool read_command(char *data, size_t size); + bool write_command(char *data, size_t size); + + void set_timeout(const std::chrono::duration &t); + void setBaud_rate(NEW_WRAP::BAUR_RATE_UART newBaud_rate); + void timeout_expired(const std::error_code &error); + + /** + * Possible outcome of a read. Set by callbacks, read from main code + */ + enum ReadResult { + resultInProgress, + resultSuccess, + resultError, + resultTimeoutExpired + }; + + void read_completed(const std::error_code &error, + const size_t bytesTransferred); private: - NEW_WRAP::BAUR_RATE_UART baud_rate; - asio::io_service io; ///< Io service object - asio::serial_port port; ///< Serial port object - asio::high_resolution_timer timer; ///< Timer for timeout - std::chrono::duration timeout; ///< Read/write timeout - asio::streambuf readData; ///< Holds eventual read but not consumed - enum ReadResult result; ///< Used by read with timeout - size_t bytesTransferred; ///< Used by async read callback - std::error_code error_code; -// ReadSetupParameters setupParameters; ///< Global because used in the OSX fix - + NEW_WRAP::BAUR_RATE_UART baud_rate; + asio::io_service io; ///< Io service object + asio::serial_port port; ///< Serial port object + asio::high_resolution_timer timer; ///< Timer for timeout + std::chrono::duration timeout; ///< Read/write timeout + asio::streambuf readData; ///< Holds eventual read but not consumed + enum ReadResult result; ///< Used by read with timeout + size_t bytesTransferred; ///< Used by async read callback + std::error_code error_code; + // ReadSetupParameters setupParameters; ///< Global because used in the OSX + // fix }; #endif // SERIALMANAGER_H -- GitLab