Commit cc331f808836f008f363d7a82ff3d642de53d262
1 parent
7cdff23b
Added modbusstack test
Showing
6 changed files
with
79 additions
and
6 deletions
CMakeLists.txt
include/request.h
| ... | ... | @@ -129,7 +129,7 @@ private: |
| 129 | 129 | std::vector<uint8_t> m_dataBuffer = {}; |
| 130 | 130 | |
| 131 | 131 | /// @return Callbacks registered to call when new data is available. |
| 132 | - /// @note Functionn pointer returns False if the device state is OFFLINE. True, otherwise. | |
| 132 | + /// @note Function pointer returns False if the device state is OFFLINE. True, otherwise. | |
| 133 | 133 | std::vector<std::function<bool(Response)>> callbacks = {}; |
| 134 | 134 | }; |
| 135 | 135 | ... | ... |
src/modbus.cpp
| ... | ... | @@ -53,32 +53,77 @@ bool ModBus::Close() |
| 53 | 53 | |
| 54 | 54 | Response ModBus::SendRequest(const Request &request) |
| 55 | 55 | { |
| 56 | + Response oResponse = {}; | |
| 57 | + std::vector<uint8_t> uint8Buffer(static_cast<size_t>(request.getNumberOfRegisters())); | |
| 58 | + std::vector<uint16_t> uint16Buffer(static_cast<size_t>(request.getNumberOfRegisters())); | |
| 59 | + | |
| 56 | 60 | |
| 57 | 61 | switch(request.getFunctionCode()) |
| 58 | 62 | { |
| 59 | 63 | case Request::FunctionCode::FC_UNKNOWN: |
| 64 | + { | |
| 60 | 65 | break; |
| 66 | + } | |
| 61 | 67 | case Request::FunctionCode::FC_READ_COILS: |
| 68 | + { | |
| 69 | + // oResponse.resultValue = m_modbus->readCoils(request.getStartAddress(),request.getNumberOfRegisters(), boolBuffer.data()); | |
| 62 | 70 | break; |
| 71 | + } | |
| 63 | 72 | case Request::FunctionCode::FC_READ_DISCRETE_INPUTS: |
| 73 | + { | |
| 74 | + // oResponse.resultValue = m_modbus->readInputBits(request.getStartAddress(), request.getNumberOfRegisters(), boolBuffer.data()); | |
| 64 | 75 | break; |
| 76 | + } | |
| 65 | 77 | case Request::FunctionCode::FC_READ_HOLDING_REGISTERS: |
| 78 | + { | |
| 79 | + oResponse.resultValue = m_modbus->readHoldingRegisters(request.getStartAddress(), request.getNumberOfRegisters(), uint16Buffer.data()); | |
| 80 | + if (oResponse.resultValue == request.getNumberOfRegisters()) | |
| 81 | + { | |
| 82 | + for(int index = 0; index < oResponse.resultValue; ++index) | |
| 83 | + { | |
| 84 | + oResponse.responseBuffer.emplace_back(static_cast<uint16_t>(uint16Buffer[index])); | |
| 85 | + } | |
| 86 | + } | |
| 66 | 87 | break; |
| 88 | + } | |
| 67 | 89 | case Request::FunctionCode::FC_READ_INPUT_REGISTERS: |
| 90 | + { | |
| 91 | + oResponse.resultValue = m_modbus->readInputRegisters(request.getStartAddress(), request.getNumberOfRegisters(), uint16Buffer.data()); | |
| 92 | + if (oResponse.resultValue == request.getNumberOfRegisters()) | |
| 93 | + { | |
| 94 | + for(int index = 0; index < oResponse.resultValue; ++index) | |
| 95 | + { | |
| 96 | + oResponse.responseBuffer.emplace_back(static_cast<uint16_t>(uint16Buffer[index])); | |
| 97 | + } | |
| 98 | + } | |
| 68 | 99 | break; |
| 100 | + } | |
| 69 | 101 | case Request::FunctionCode::FC_WRITE_SINGLE_COIL: |
| 70 | - break; | |
| 71 | 102 | case Request::FunctionCode::FC_WRITE_SINGLE_REGISTER: |
| 103 | + { | |
| 104 | + oResponse.resultValue = m_modbus->writeCoil(request.getStartAddress(), request.getDataBuffer()[0]); | |
| 72 | 105 | break; |
| 106 | + } | |
| 73 | 107 | case Request::FunctionCode::FC_WRITE_MULTIPLE_COILS: |
| 108 | + { | |
| 109 | + // const auto buffer = std | |
| 74 | 110 | break; |
| 111 | + } | |
| 75 | 112 | case Request::FunctionCode::FC_WRITE_MULTIPLE_REGISTERS: |
| 113 | + { | |
| 76 | 114 | break; |
| 115 | + } | |
| 77 | 116 | case Request::FunctionCode::FC_READ_FILE_RECORD: |
| 117 | + { | |
| 118 | + // Not implemented (yet) | |
| 78 | 119 | break; |
| 120 | + } | |
| 79 | 121 | case Request::FunctionCode::FC_WRITE_FILE_RECORD: |
| 122 | + { | |
| 123 | + // Not implemented (yet) | |
| 80 | 124 | break; |
| 125 | + } | |
| 81 | 126 | } |
| 82 | 127 | |
| 83 | - return Response; | |
| 128 | + return Response(); | |
| 84 | 129 | } | ... | ... |
src/modbusbase.cpp
| ... | ... | @@ -297,7 +297,7 @@ int ModbusBase::modbusWrite(uint16_t address, uint16_t amount, int function_code |
| 297 | 297 | // Declare as pure virtual and implement in the transport-specific class? |
| 298 | 298 | // For now we focus on TCP as it is easier to implement. |
| 299 | 299 | int status = 0; |
| 300 | - uint8_t *to_send; | |
| 300 | + uint8_t *to_send = nullptr; | |
| 301 | 301 | |
| 302 | 302 | switch (function_code) |
| 303 | 303 | { | ... | ... |
tests/CMakeLists.txt
| 1 | 1 | # **************************************************************** |
| 2 | 2 | # Copyright (c)2022 Peter M. Groen |
| 3 | -# This file is licensed under the MIT license found in the | |
| 3 | +# This file is licensed under the MIT license found in the | |
| 4 | 4 | # LICENSE file in the root directory of this source tree. |
| 5 | 5 | # **************************************************************** |
| 6 | 6 | add_executable(modbustest |
| 7 | 7 | connectionconfigtest.cpp |
| 8 | 8 | requesttest.cpp |
| 9 | + modbusstacktest.cpp | |
| 9 | 10 | ) |
| 10 | 11 | |
| 11 | 12 | target_include_directories(modbustest PRIVATE | ... | ... |
tests/modbusstacktest.cpp
0 → 100644
| 1 | +/**************************************************************** | |
| 2 | + * Copyright (c)2022 Peter M. Groen | |
| 3 | + * This file is licensed under the MIT license found in the | |
| 4 | + * LICENSE file in the root directory of this source tree. | |
| 5 | + ****************************************************************/ | |
| 6 | +#include <gmock/gmock.h> | |
| 7 | +#include <gtest/gtest.h> | |
| 8 | + | |
| 9 | +#include "connectionconfig.h" | |
| 10 | + | |
| 11 | +using namespace osdev::components::modbus; | |
| 12 | + | |
| 13 | +TEST(ModbusStackTest, StackConnectRTU) | |
| 14 | +{ | |
| 15 | + // Setup the connection configuration | |
| 16 | + ConnectionConfig oConfig; | |
| 17 | + oConfig.setBaudRate(B9600); | |
| 18 | + oConfig.setConnectionType(ConnectionConfig::E_CONNECTIONTYPE::SERIAL); | |
| 19 | + oConfig.setDataBits(8); | |
| 20 | + oConfig.setStopBits(1); | |
| 21 | + oConfig.setFrameTimeout(10); | |
| 22 | + oConfig.setParity(ConnectionConfig::E_PARITY::NONE); | |
| 23 | + oConfig.setPortName("/dev/ttyUSB0"); | |
| 24 | + | |
| 25 | + // Create the modbus-stack.. | |
| 26 | + | |
| 27 | +} | ... | ... |