Commit 86fed624d1b71db343893ef83b94726266c74253
1 parent
a576ff0b
Add TCP connection
Showing
4 changed files
with
42 additions
and
45 deletions
src/CMakeLists.txt
| ... | ... | @@ -12,8 +12,8 @@ include_directories( |
| 12 | 12 | |
| 13 | 13 | set(SRC_LIST |
| 14 | 14 | ${CMAKE_CURRENT_SOURCE_DIR}/imodbusport.h |
| 15 | - ${CMAKE_CURRENT_SOURCE_DIR}/modbus.h | |
| 16 | - ${CMAKE_CURRENT_SOURCE_DIR}/modbus.cpp | |
| 15 | + ${CMAKE_CURRENT_SOURCE_DIR}/modbusbase.h | |
| 16 | + ${CMAKE_CURRENT_SOURCE_DIR}/modbusbase.cpp | |
| 17 | 17 | ${CMAKE_CURRENT_SOURCE_DIR}/modbustcp.h |
| 18 | 18 | ${CMAKE_CURRENT_SOURCE_DIR}/modbustcp.cpp |
| 19 | 19 | ${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.h | ... | ... |
src/modbus.cpp renamed to src/modbusbase.cpp
| ... | ... | @@ -5,7 +5,7 @@ |
| 5 | 5 | * LICENSE file in the root directory of this source tree. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -#include "modbus.h" | |
| 8 | +#include "modbusbase.h" | |
| 9 | 9 | |
| 10 | 10 | using namespace osdev::components::modbus; |
| 11 | 11 | |
| ... | ... | @@ -352,21 +352,6 @@ int ModbusBase::modbusWrite(uint16_t address, uint16_t amount, int function_code |
| 352 | 352 | return status; |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | -ssize_t ModbusBase::modbusSend(uint8_t *to_send, size_t length) | |
| 356 | -{ | |
| 357 | - (void)to_send; | |
| 358 | - (void)length; | |
| 359 | - | |
| 360 | - return 0; | |
| 361 | -} | |
| 362 | - | |
| 363 | -ssize_t ModbusBase::modbusReceive(uint8_t *buffer) const | |
| 364 | -{ | |
| 365 | - (void)buffer; | |
| 366 | - | |
| 367 | - return 0; | |
| 368 | -} | |
| 369 | - | |
| 370 | 355 | void ModbusBase::modbusErrorHandle(const uint8_t *msg, int function_code) |
| 371 | 356 | { |
| 372 | 357 | (void)msg; | ... | ... |
src/modbus.h renamed to src/modbusbase.h
| ... | ... | @@ -50,15 +50,30 @@ namespace components { |
| 50 | 50 | namespace modbus { |
| 51 | 51 | |
| 52 | 52 | // Modbus base class. Providing all Modbus PDUs without transport implementation |
| 53 | -class Modbus | |
| 53 | +class ModbusBase | |
| 54 | 54 | { |
| 55 | 55 | public: |
| 56 | - Modbus(); | |
| 57 | - virtual ~Modbus(); | |
| 56 | + ModbusBase(); | |
| 57 | + virtual ~ModbusBase(); | |
| 58 | 58 | |
| 59 | 59 | // Pure virtuals. Override when inherited. |
| 60 | 60 | virtual bool Connect() = 0; |
| 61 | - virtual void Close() = 0; | |
| 61 | + virtual bool Close() = 0; | |
| 62 | + | |
| 63 | + /*! | |
| 64 | + * \brief modbusSend | |
| 65 | + * \param to_send | |
| 66 | + * \param length | |
| 67 | + * \return | |
| 68 | + */ | |
| 69 | + virtual ssize_t modbusSend(uint8_t *to_send, size_t length) = 0; | |
| 70 | + | |
| 71 | + /*! | |
| 72 | + * \brief modbusReceive | |
| 73 | + * \param buffer | |
| 74 | + * \return | |
| 75 | + */ | |
| 76 | + virtual ssize_t modbusReceive(uint8_t *buffer) = 0; | |
| 62 | 77 | |
| 63 | 78 | // Modbus implementation(s) |
| 64 | 79 | /*! |
| ... | ... | @@ -173,21 +188,6 @@ private: // Methods |
| 173 | 188 | int modbusWrite(uint16_t address, uint16_t amount, int function_code, const uint16_t *value); |
| 174 | 189 | |
| 175 | 190 | /*! |
| 176 | - * \brief modbusSend | |
| 177 | - * \param to_send | |
| 178 | - * \param length | |
| 179 | - * \return | |
| 180 | - */ | |
| 181 | - virtual ssize_t modbusSend(uint8_t *to_send, size_t length) = 0; | |
| 182 | - | |
| 183 | - /*! | |
| 184 | - * \brief modbusReceive | |
| 185 | - * \param buffer | |
| 186 | - * \return | |
| 187 | - */ | |
| 188 | - virtual ssize_t modbusReceive(uint8_t *buffer) const = 0; | |
| 189 | - | |
| 190 | - /*! | |
| 191 | 191 | * \brief modbusErrorHandle |
| 192 | 192 | * \param msg |
| 193 | 193 | * \param function_code | ... | ... |
src/modbustcp.h
| ... | ... | @@ -6,7 +6,8 @@ |
| 6 | 6 | */ |
| 7 | 7 | #pragma once |
| 8 | 8 | |
| 9 | -#include "imodbusport.h" | |
| 9 | +#include "modbusbase.h" | |
| 10 | +#include "connectionconfig.h" | |
| 10 | 11 | |
| 11 | 12 | #include <unistd.h> |
| 12 | 13 | #include <sys/socket.h> |
| ... | ... | @@ -21,18 +22,29 @@ using X_SOCKET = int; |
| 21 | 22 | using SOCKADDR = struct sockaddr; |
| 22 | 23 | using SOCKADDR_IN = struct sockaddr_in; |
| 23 | 24 | |
| 24 | -class ModbusTcp : public IModbusPort | |
| 25 | +class ModbusTcp : public ModbusBase | |
| 25 | 26 | { |
| 26 | 27 | public: |
| 27 | 28 | explicit ModbusTcp(const ConnectionConfig &con_config); |
| 28 | 29 | virtual ~ModbusTcp() {} |
| 29 | 30 | |
| 30 | - // The standard device interface. | |
| 31 | - // Implementation of IModbusPort | |
| 32 | - virtual bool Open() const override; | |
| 33 | - virtual bool Close() const override; | |
| 34 | - virtual int Read(uint8_t *buffer) const override; | |
| 35 | - virtual int Write(uint8_t *buffer, size_t length) const override; | |
| 31 | + virtual bool Connect() override; | |
| 32 | + virtual bool Close() override; | |
| 33 | + | |
| 34 | + /*! | |
| 35 | + * \brief modbusSend | |
| 36 | + * \param to_send | |
| 37 | + * \param length | |
| 38 | + * \return | |
| 39 | + */ | |
| 40 | + virtual ssize_t mmodbusSend(uint8_t *to_send, size_t length) override; | |
| 41 | + | |
| 42 | + /*! | |
| 43 | + * \brief modbusReceive | |
| 44 | + * \param buffer | |
| 45 | + * \return | |
| 46 | + */ | |
| 47 | + virtual ssize_t modbusReceive(uint8_t *buffer) const override; | |
| 36 | 48 | |
| 37 | 49 | private: |
| 38 | 50 | uint16_t m_port {502}; | ... | ... |