diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d796c6..be2d0d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.20) project(modbus-cpp) LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/submodules/cmake) diff --git a/include/request.h b/include/request.h index cecd0dd..538d88e 100644 --- a/include/request.h +++ b/include/request.h @@ -129,7 +129,7 @@ private: std::vector m_dataBuffer = {}; /// @return Callbacks registered to call when new data is available. - /// @note Functionn pointer returns False if the device state is OFFLINE. True, otherwise. + /// @note Function pointer returns False if the device state is OFFLINE. True, otherwise. std::vector> callbacks = {}; }; diff --git a/src/modbus.cpp b/src/modbus.cpp index cacf094..782d1d1 100644 --- a/src/modbus.cpp +++ b/src/modbus.cpp @@ -53,32 +53,77 @@ bool ModBus::Close() Response ModBus::SendRequest(const Request &request) { + Response oResponse = {}; + std::vector uint8Buffer(static_cast(request.getNumberOfRegisters())); + std::vector uint16Buffer(static_cast(request.getNumberOfRegisters())); + switch(request.getFunctionCode()) { case Request::FunctionCode::FC_UNKNOWN: + { break; + } case Request::FunctionCode::FC_READ_COILS: + { + // oResponse.resultValue = m_modbus->readCoils(request.getStartAddress(),request.getNumberOfRegisters(), boolBuffer.data()); break; + } case Request::FunctionCode::FC_READ_DISCRETE_INPUTS: + { + // oResponse.resultValue = m_modbus->readInputBits(request.getStartAddress(), request.getNumberOfRegisters(), boolBuffer.data()); break; + } case Request::FunctionCode::FC_READ_HOLDING_REGISTERS: + { + oResponse.resultValue = m_modbus->readHoldingRegisters(request.getStartAddress(), request.getNumberOfRegisters(), uint16Buffer.data()); + if (oResponse.resultValue == request.getNumberOfRegisters()) + { + for(int index = 0; index < oResponse.resultValue; ++index) + { + oResponse.responseBuffer.emplace_back(static_cast(uint16Buffer[index])); + } + } break; + } case Request::FunctionCode::FC_READ_INPUT_REGISTERS: + { + oResponse.resultValue = m_modbus->readInputRegisters(request.getStartAddress(), request.getNumberOfRegisters(), uint16Buffer.data()); + if (oResponse.resultValue == request.getNumberOfRegisters()) + { + for(int index = 0; index < oResponse.resultValue; ++index) + { + oResponse.responseBuffer.emplace_back(static_cast(uint16Buffer[index])); + } + } break; + } case Request::FunctionCode::FC_WRITE_SINGLE_COIL: - break; case Request::FunctionCode::FC_WRITE_SINGLE_REGISTER: + { + oResponse.resultValue = m_modbus->writeCoil(request.getStartAddress(), request.getDataBuffer()[0]); break; + } case Request::FunctionCode::FC_WRITE_MULTIPLE_COILS: + { + // const auto buffer = std break; + } case Request::FunctionCode::FC_WRITE_MULTIPLE_REGISTERS: + { break; + } case Request::FunctionCode::FC_READ_FILE_RECORD: + { + // Not implemented (yet) break; + } case Request::FunctionCode::FC_WRITE_FILE_RECORD: + { + // Not implemented (yet) break; + } } - return Response; + return Response(); } diff --git a/src/modbusbase.cpp b/src/modbusbase.cpp index b88bba3..3640298 100644 --- a/src/modbusbase.cpp +++ b/src/modbusbase.cpp @@ -297,7 +297,7 @@ int ModbusBase::modbusWrite(uint16_t address, uint16_t amount, int function_code // Declare as pure virtual and implement in the transport-specific class? // For now we focus on TCP as it is easier to implement. int status = 0; - uint8_t *to_send; + uint8_t *to_send = nullptr; switch (function_code) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bb6c95c..ba6cd08 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,11 +1,12 @@ # **************************************************************** # Copyright (c)2022 Peter M. Groen -# This file is licensed under the MIT license found in the +# This file is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. # **************************************************************** add_executable(modbustest connectionconfigtest.cpp requesttest.cpp + modbusstacktest.cpp ) target_include_directories(modbustest PRIVATE diff --git a/tests/modbusstacktest.cpp b/tests/modbusstacktest.cpp new file mode 100644 index 0000000..f355454 --- /dev/null +++ b/tests/modbusstacktest.cpp @@ -0,0 +1,27 @@ +/**************************************************************** + * Copyright (c)2022 Peter M. Groen + * This file is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + ****************************************************************/ +#include +#include + +#include "connectionconfig.h" + +using namespace osdev::components::modbus; + +TEST(ModbusStackTest, StackConnectRTU) +{ + // Setup the connection configuration + ConnectionConfig oConfig; + oConfig.setBaudRate(B9600); + oConfig.setConnectionType(ConnectionConfig::E_CONNECTIONTYPE::SERIAL); + oConfig.setDataBits(8); + oConfig.setStopBits(1); + oConfig.setFrameTimeout(10); + oConfig.setParity(ConnectionConfig::E_PARITY::NONE); + oConfig.setPortName("/dev/ttyUSB0"); + + // Create the modbus-stack.. + +}