Commit ab279ee878a6f77e72fd6c7aa9f967d4d16bd3be
1 parent
ea10dfe9
Setting up new structure
Showing
9 changed files
with
42 additions
and
240 deletions
include/modbus.h renamed to include/imodbus.h
src/CMakeLists.txt
| @@ -6,13 +6,14 @@ project_header(modbus-cpp) | @@ -6,13 +6,14 @@ project_header(modbus-cpp) | ||
| 6 | 6 | ||
| 7 | include(compiler) | 7 | include(compiler) |
| 8 | 8 | ||
| 9 | +include_directories( | ||
| 10 | + ${CMAKE_SOURCE_DIR}/include | ||
| 11 | +) | ||
| 12 | + | ||
| 9 | set(SRC_LIST | 13 | set(SRC_LIST |
| 10 | - ${CMAKE_CURRENT_SOURCE_DIR}/modbusbase.h | ||
| 11 | - ${CMAKE_CURRENT_SOURCE_DIR}/modbusbase.cpp | ||
| 12 | - ${CMAKE_CURRENT_SOURCE_DIR}/modbustcp.h | ||
| 13 | - ${CMAKE_CURRENT_SOURCE_DIR}/modbustcp.cpp | ||
| 14 | - #${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.h | ||
| 15 | - #${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.cpp | 14 | + ${CMAKE_CURRENT_SOURCE_DIR}/imodbusport.h |
| 15 | + ${CMAKE_CURRENT_SOURCE_DIR}/modbus.h | ||
| 16 | + ${CMAKE_CURRENT_SOURCE_DIR}/modbus.cpp | ||
| 16 | ${CMAKE_CURRENT_SOURCE_DIR}/connectionconfig.h | 17 | ${CMAKE_CURRENT_SOURCE_DIR}/connectionconfig.h |
| 17 | ${CMAKE_CURRENT_SOURCE_DIR}/connectionconfig.cpp | 18 | ${CMAKE_CURRENT_SOURCE_DIR}/connectionconfig.cpp |
| 18 | ) | 19 | ) |
src/imodbusport.h
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright (c) 2022 Peter M. Groen | ||
| 3 | + * | ||
| 4 | + * This source code is licensed under the MIT license found in the | ||
| 5 | + * LICENSE file in the root directory of this source tree. | ||
| 6 | + */ | ||
| 7 | +#pragma once | ||
| 8 | + | ||
| 9 | +#include "connectionconfig.h" | ||
| 10 | +#include <vector> | ||
| 11 | + | ||
| 12 | +namespace osdev { | ||
| 13 | +namespace components { | ||
| 14 | +namespace modbus { | ||
| 15 | + | ||
| 16 | +class IModbusPort | ||
| 17 | +{ | ||
| 18 | +public: | ||
| 19 | + IModbusPort(const ConnectionConfig &con_config); | ||
| 20 | + virtual ~IModbusPort() {} | ||
| 21 | + | ||
| 22 | + // The standard device interface. | ||
| 23 | + virtual bool Open() const = 0; | ||
| 24 | + virtual bool Close() const = 0; | ||
| 25 | + virtual int Read(uint8_t *buffer) const = 0; | ||
| 26 | + virtual int Write(uint8_t *buffer, size_t length) const = 0; | ||
| 27 | +}; | ||
| 28 | + | ||
| 29 | +} /* End namespace modbus */ | ||
| 30 | +} /* End namespace components */ | ||
| 31 | +} /* End namespace osdev */ |
src/modbusbase.cpp renamed to src/modbus.cpp
| @@ -5,7 +5,7 @@ | @@ -5,7 +5,7 @@ | ||
| 5 | * LICENSE file in the root directory of this source tree. | 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | -#include "modbusbase.h" | 8 | +#include "modbus.h" |
| 9 | 9 | ||
| 10 | using namespace osdev::components::modbus; | 10 | using namespace osdev::components::modbus; |
| 11 | 11 |
src/modbusbase.h renamed to src/modbus.h
| @@ -50,11 +50,11 @@ namespace components { | @@ -50,11 +50,11 @@ namespace components { | ||
| 50 | namespace modbus { | 50 | namespace modbus { |
| 51 | 51 | ||
| 52 | // Modbus base class. Providing all Modbus PDUs without transport implementation | 52 | // Modbus base class. Providing all Modbus PDUs without transport implementation |
| 53 | -class ModbusBase | 53 | +class Modbus |
| 54 | { | 54 | { |
| 55 | public: | 55 | public: |
| 56 | - ModbusBase(); | ||
| 57 | - virtual ~ModbusBase(); | 56 | + Modbus(); |
| 57 | + virtual ~Modbus(); | ||
| 58 | 58 | ||
| 59 | // Pure virtuals. Override when inherited. | 59 | // Pure virtuals. Override when inherited. |
| 60 | virtual bool Connect() = 0; | 60 | virtual bool Connect() = 0; |
src/modbusrtu.cpp
| 1 | -/* | ||
| 2 | - * Copyright (c) 2022 Peter M. Groen | ||
| 3 | - * | ||
| 4 | - * This source code is licensed under the MIT license found in the | ||
| 5 | - * LICENSE file in the root directory of this source tree. | ||
| 6 | - */ | ||
| 7 | -#include "modbusrtu.h" | ||
| 8 | - | ||
| 9 | -using namespace osdev::components::modbus; | ||
| 10 | - | ||
| 11 | -ModbusRTU::ModbusRTU() | ||
| 12 | -{ | ||
| 13 | - | ||
| 14 | -} | ||
| 15 | - | ||
| 16 | -bool ModbusRTU::Connect() | ||
| 17 | -{ | ||
| 18 | - | ||
| 19 | -} | ||
| 20 | - | ||
| 21 | -void ModbusRTU::Close() | ||
| 22 | -{ | ||
| 23 | - | ||
| 24 | -} | ||
| 25 | - | ||
| 26 | -ssize_t ModbusRTU::modbusSend(uint8_t *to_send, size_t length) | ||
| 27 | -{ | ||
| 28 | - | ||
| 29 | -} | ||
| 30 | - | ||
| 31 | -ssize_t ModbusRTU::modbusReceive(uint8_t *buffer) const | ||
| 32 | -{ | ||
| 33 | - | ||
| 34 | -} |
src/modbusrtu.h
| 1 | -/* | ||
| 2 | - * Copyright (c) 2022 Peter M. Groen | ||
| 3 | - * | ||
| 4 | - * This source code is licensed under the MIT license found in the | ||
| 5 | - * LICENSE file in the root directory of this source tree. | ||
| 6 | - */ | ||
| 7 | -#pragma once | ||
| 8 | - | ||
| 9 | -#include "modbusbase.h" | ||
| 10 | - | ||
| 11 | -namespace osdev { | ||
| 12 | -namespace components { | ||
| 13 | -namespace modbus { | ||
| 14 | - | ||
| 15 | -class ModbusRTU : public ModbusBase | ||
| 16 | -{ | ||
| 17 | -public: | ||
| 18 | - ModbusRTU(); | ||
| 19 | - | ||
| 20 | - // Pure virtuals. Override when inherited. | ||
| 21 | - /*! | ||
| 22 | - * \brief Connect | ||
| 23 | - * \return | ||
| 24 | - */ | ||
| 25 | - virtual bool Connect() override; | ||
| 26 | - | ||
| 27 | - /*! | ||
| 28 | - * \brief Close | ||
| 29 | - */ | ||
| 30 | - virtual void Close() override; | ||
| 31 | - | ||
| 32 | -private: | ||
| 33 | - /*! | ||
| 34 | - * \brief modbusSend | ||
| 35 | - * \param to_send | ||
| 36 | - * \param length | ||
| 37 | - * \return | ||
| 38 | - */ | ||
| 39 | - virtual ssize_t modbusSend(uint8_t *to_send, size_t length) override; | ||
| 40 | - | ||
| 41 | - /*! | ||
| 42 | - * \brief modbusReceive | ||
| 43 | - * \param buffer | ||
| 44 | - * \return | ||
| 45 | - */ | ||
| 46 | - virtual ssize_t modbusReceive(uint8_t *buffer) const override; | ||
| 47 | -}; | ||
| 48 | - | ||
| 49 | -} // End namespace modbus | ||
| 50 | -} // End namespace components | ||
| 51 | -} // End namespace osdev |
src/modbustcp.cpp
| 1 | -/* | ||
| 2 | - * Copyright (c) 2022 Peter M. Groen | ||
| 3 | - * | ||
| 4 | - * This source code is licensed under the MIT license found in the | ||
| 5 | - * LICENSE file in the root directory of this source tree. | ||
| 6 | - */ | ||
| 7 | -#include "modbustcp.h" | ||
| 8 | - | ||
| 9 | -using namespace osdev::components::modbus; | ||
| 10 | - | ||
| 11 | -ModbusTCP::ModbusTCP(const ConnectionConfig &port_configuration) | ||
| 12 | -{ | ||
| 13 | - setMaxMessageLength(260); // Maximum Messagelength over TCP | ||
| 14 | -} | ||
| 15 | - | ||
| 16 | -bool ModbusTCP::Connect() | ||
| 17 | -{ | ||
| 18 | - if(m_host.empty() || m_port == 0) | ||
| 19 | - { | ||
| 20 | - LOG("Missing host and port"); | ||
| 21 | - return false; | ||
| 22 | - } | ||
| 23 | - | ||
| 24 | - m_socket = socket(AF_INET, SOCK_STREAM, 0); | ||
| 25 | - if(!X_ISVALIDSOCKET(m_socket)) | ||
| 26 | - { | ||
| 27 | - LOG("Errror Opening Socket"); | ||
| 28 | - return false; | ||
| 29 | - } | ||
| 30 | - | ||
| 31 | - struct timeval timeout {}; | ||
| 32 | - timeout.tv_sec = 20; // After 20 seconds connect() will timeout | ||
| 33 | - timeout.tv_usec = 0; | ||
| 34 | - | ||
| 35 | - setsockopt(m_socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char *>(&timeout), sizeof(timeout)); | ||
| 36 | - setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char *>(&timeout), sizeof(timeout)); | ||
| 37 | - m_server.sin_family = AF_INET; | ||
| 38 | - m_server.sin_addr.s_addr = inet_addr(m_host.c_str()); | ||
| 39 | - m_server.sin_port = htons(m_port); | ||
| 40 | - | ||
| 41 | - if(!X_ISCONNECTSUCCEED(connect(m_socket, reinterpret_cast<SOCKADDR *>(&m_server), sizeof(m_server)))) | ||
| 42 | - { | ||
| 43 | - LOG("Connection Error"); | ||
| 44 | - return false; | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | - ModbusBase::setConnected(true); | ||
| 48 | - return true; | ||
| 49 | -} | ||
| 50 | - | ||
| 51 | -void ModbusTCP::Close() | ||
| 52 | -{ | ||
| 53 | - X_CLOSE_SOCKET(m_socket); | ||
| 54 | -} | ||
| 55 | - | ||
| 56 | -ssize_t ModbusTCP::modbusSend(uint8_t *to_send, size_t length) | ||
| 57 | -{ | ||
| 58 | - uint32_t message_id = getMessageId(); | ||
| 59 | - setMessageId(message_id++); | ||
| 60 | - | ||
| 61 | - return send(m_socket, reinterpret_cast<const char *>(to_send), static_cast<size_t>(length), 0); | ||
| 62 | -} | ||
| 63 | - | ||
| 64 | -ssize_t ModbusTCP::modbusReceive(uint8_t *buffer) const | ||
| 65 | -{ | ||
| 66 | - return recv(m_socket, reinterpret_cast<char *>(buffer), getMaxMessageLength(), 0); | ||
| 67 | -} |
src/modbustcp.h
| 1 | -/* | ||
| 2 | - * Copyright (c) 2022 Peter M. Groen | ||
| 3 | - * | ||
| 4 | - * This source code is licensed under the MIT license found in the | ||
| 5 | - * LICENSE file in the root directory of this source tree. | ||
| 6 | - */ | ||
| 7 | -#pragma once | ||
| 8 | - | ||
| 9 | -#include "modbusbase.h" | ||
| 10 | -#include "connectionconfig.h" | ||
| 11 | - | ||
| 12 | -#include <unistd.h> | ||
| 13 | -#include <sys/socket.h> | ||
| 14 | -#include <netinet/in.h> | ||
| 15 | -#include <arpa/inet.h> | ||
| 16 | - | ||
| 17 | -namespace osdev { | ||
| 18 | -namespace components { | ||
| 19 | -namespace modbus { | ||
| 20 | - | ||
| 21 | -using X_SOCKET = int; | ||
| 22 | -using SOCKADDR = struct sockaddr; | ||
| 23 | -using SOCKADDR_IN = struct sockaddr_in; | ||
| 24 | - | ||
| 25 | -#define X_ISVALIDSOCKET(s) ((s) >= 0) | ||
| 26 | -#define X_CLOSE_SOCKET(s) close(s) | ||
| 27 | -#define X_ISCONNECTSUCCEED(s) ((s) >= 0) | ||
| 28 | - | ||
| 29 | -/*! | ||
| 30 | - * \brief The ModbusTCP class | ||
| 31 | - */ | ||
| 32 | -class ModbusTCP : public ModbusBase | ||
| 33 | -{ | ||
| 34 | -public: | ||
| 35 | - /*! | ||
| 36 | - * \brief ModbusTCP | ||
| 37 | - * \param port_configuration | ||
| 38 | - */ | ||
| 39 | - ModbusTCP(const ConnectionConfig &port_configuration); | ||
| 40 | - | ||
| 41 | - // Pure virtuals. Override when inherited. | ||
| 42 | - /*! | ||
| 43 | - * \brief Connect | ||
| 44 | - * \return | ||
| 45 | - */ | ||
| 46 | - virtual bool Connect() override; | ||
| 47 | - | ||
| 48 | - /*! | ||
| 49 | - * \brief Close | ||
| 50 | - */ | ||
| 51 | - virtual void Close() override; | ||
| 52 | - | ||
| 53 | -private: // Methods | ||
| 54 | - /*! | ||
| 55 | - * \brief modbusSend | ||
| 56 | - * \param to_send | ||
| 57 | - * \param length | ||
| 58 | - * \return | ||
| 59 | - */ | ||
| 60 | - virtual ssize_t modbusSend(uint8_t *to_send, size_t length) override; | ||
| 61 | - | ||
| 62 | - /*! | ||
| 63 | - * \brief modbusReceive | ||
| 64 | - * \param buffer | ||
| 65 | - * \return | ||
| 66 | - */ | ||
| 67 | - virtual ssize_t modbusReceive(uint8_t *buffer) const override; | ||
| 68 | - | ||
| 69 | -private: // Members | ||
| 70 | - uint16_t m_port {}; | ||
| 71 | - std::string m_host{}; | ||
| 72 | - X_SOCKET m_socket{}; | ||
| 73 | - SOCKADDR_IN m_server{}; | ||
| 74 | -}; | ||
| 75 | - | ||
| 76 | -} // End namespace modbus | ||
| 77 | -} // End namespace components | ||
| 78 | -} // End namespace osdev |