Commit 5a6186d4880820fde80858135ef21bc49f6aa1b3
1 parent
cc331f80
Added RTU Test
Showing
6 changed files
with
27 additions
and
8 deletions
include/request.h
| @@ -49,14 +49,15 @@ public: | @@ -49,14 +49,15 @@ public: | ||
| 49 | FC_READ_FILE_RECORD, | 49 | FC_READ_FILE_RECORD, |
| 50 | FC_WRITE_FILE_RECORD, | 50 | FC_WRITE_FILE_RECORD, |
| 51 | }; | 51 | }; |
| 52 | - | ||
| 53 | - explicit Request() | 52 | + |
| 53 | + | ||
| 54 | + Request() | ||
| 54 | : m_functionCode(Request::FunctionCode::FC_UNKNOWN) | 55 | : m_functionCode(Request::FunctionCode::FC_UNKNOWN) |
| 55 | , m_slaveId(0) | 56 | , m_slaveId(0) |
| 56 | , m_startAddress(0x00) | 57 | , m_startAddress(0x00) |
| 57 | , m_numberOfRegisters(0x00) | 58 | , m_numberOfRegisters(0x00) |
| 58 | {} | 59 | {} |
| 59 | - | 60 | + |
| 60 | /// Constructor taking all necessary information to create a request. | 61 | /// Constructor taking all necessary information to create a request. |
| 61 | /// | 62 | /// |
| 62 | /// @param functionCode - The code of the action this request wants to perform. | 63 | /// @param functionCode - The code of the action this request wants to perform. |
src/modbus.cpp
| @@ -34,7 +34,7 @@ bool ModBus::Open(const ConnectionConfig &connection_config) | @@ -34,7 +34,7 @@ bool ModBus::Open(const ConnectionConfig &connection_config) | ||
| 34 | } | 34 | } |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | - if(m_modbus->getConnected()) | 37 | + if(m_modbus->isConnected()) |
| 38 | { | 38 | { |
| 39 | m_modbus->Close(); | 39 | m_modbus->Close(); |
| 40 | } | 40 | } |
src/modbusbase.h
| @@ -152,7 +152,7 @@ public: | @@ -152,7 +152,7 @@ public: | ||
| 152 | 152 | ||
| 153 | // Getters and Setters. | 153 | // Getters and Setters. |
| 154 | void setConnected(bool connected = false){ m_connected = connected;} | 154 | void setConnected(bool connected = false){ m_connected = connected;} |
| 155 | - bool getConnected() const { return m_connected; } | 155 | + bool isConnected() const { return m_connected; } |
| 156 | void setMessageId( uint32_t message_id ) { m_msg_id = message_id; } | 156 | void setMessageId( uint32_t message_id ) { m_msg_id = message_id; } |
| 157 | uint32_t getMessageId() const { return m_msg_id; } | 157 | uint32_t getMessageId() const { return m_msg_id; } |
| 158 | void setSlaveId(int slave_id){ m_slaveId = slave_id; } | 158 | void setSlaveId(int slave_id){ m_slaveId = slave_id; } |
src/modbustcp.cpp
| @@ -65,7 +65,7 @@ bool ModbusTcp::Connect() | @@ -65,7 +65,7 @@ bool ModbusTcp::Connect() | ||
| 65 | 65 | ||
| 66 | LOG("Connected"); | 66 | LOG("Connected"); |
| 67 | ModbusBase::setConnected( true ); | 67 | ModbusBase::setConnected( true ); |
| 68 | - return true; | 68 | + return ModbusBase::isConnected(); |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | bool ModbusTcp::Close() | 71 | bool ModbusTcp::Close() |
| @@ -73,7 +73,7 @@ bool ModbusTcp::Close() | @@ -73,7 +73,7 @@ bool ModbusTcp::Close() | ||
| 73 | X_CLOSE_SOCKET(m_socket); | 73 | X_CLOSE_SOCKET(m_socket); |
| 74 | LOG("Socket Closed"); | 74 | LOG("Socket Closed"); |
| 75 | ModbusBase::setConnected(false); | 75 | ModbusBase::setConnected(false); |
| 76 | - return ModbusBase::getConnected(); | 76 | + return ModbusBase::isConnected(); |
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | int ModbusTcp::modbusReceive(uint8_t *buffer) | 79 | int ModbusTcp::modbusReceive(uint8_t *buffer) |
tests/CMakeLists.txt
| @@ -27,4 +27,4 @@ add_test(NAME modbustest COMMAND modbustest) | @@ -27,4 +27,4 @@ add_test(NAME modbustest COMMAND modbustest) | ||
| 27 | 27 | ||
| 28 | set_tests_properties(modbustest PROPERTIES | 28 | set_tests_properties(modbustest PROPERTIES |
| 29 | WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" | 29 | WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" |
| 30 | -) | 30 | -) |
| 31 | +) | ||
| 31 | \ No newline at end of file | 32 | \ No newline at end of file |
tests/modbusstacktest.cpp
| @@ -5,8 +5,11 @@ | @@ -5,8 +5,11 @@ | ||
| 5 | ****************************************************************/ | 5 | ****************************************************************/ |
| 6 | #include <gmock/gmock.h> | 6 | #include <gmock/gmock.h> |
| 7 | #include <gtest/gtest.h> | 7 | #include <gtest/gtest.h> |
| 8 | +#include <iostream> | ||
| 8 | 9 | ||
| 9 | #include "connectionconfig.h" | 10 | #include "connectionconfig.h" |
| 11 | +#include "modbus.h" | ||
| 12 | +#include "request.h" | ||
| 10 | 13 | ||
| 11 | using namespace osdev::components::modbus; | 14 | using namespace osdev::components::modbus; |
| 12 | 15 | ||
| @@ -23,5 +26,20 @@ TEST(ModbusStackTest, StackConnectRTU) | @@ -23,5 +26,20 @@ TEST(ModbusStackTest, StackConnectRTU) | ||
| 23 | oConfig.setPortName("/dev/ttyUSB0"); | 26 | oConfig.setPortName("/dev/ttyUSB0"); |
| 24 | 27 | ||
| 25 | // Create the modbus-stack.. | 28 | // Create the modbus-stack.. |
| 29 | + ModBus oModbus; | ||
| 30 | + EXPECT_EQ(oModbus.Open(oConfig), true); | ||
| 31 | + | ||
| 32 | + // Create a request to read the Temperature from a physical device XY-MD02 | ||
| 33 | + Request request(Request::FunctionCode::FC_READ_HOLDING_REGISTERS, | ||
| 34 | + 1, | ||
| 35 | + 0x0001, | ||
| 36 | + 2, | ||
| 37 | + [](Response response) mutable | ||
| 38 | + { | ||
| 39 | + std::cout << "Result: " << response.resultValue << std::endl; | ||
| 40 | + return true; | ||
| 41 | + }); | ||
| 26 | 42 | ||
| 43 | + // Send the request | ||
| 44 | + Response response = oModbus.SendRequest(request); | ||
| 27 | } | 45 | } |