diff --git a/include/modbus.h b/include/imodbus.h index e69de29..e69de29 100644 --- a/include/modbus.h +++ b/include/imodbus.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7aac9df..1dfe0a6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,13 +6,14 @@ project_header(modbus-cpp) include(compiler) +include_directories( + ${CMAKE_SOURCE_DIR}/include +) + set(SRC_LIST - ${CMAKE_CURRENT_SOURCE_DIR}/modbusbase.h - ${CMAKE_CURRENT_SOURCE_DIR}/modbusbase.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/modbustcp.h - ${CMAKE_CURRENT_SOURCE_DIR}/modbustcp.cpp - #${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.h - #${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/imodbusport.h + ${CMAKE_CURRENT_SOURCE_DIR}/modbus.h + ${CMAKE_CURRENT_SOURCE_DIR}/modbus.cpp ${CMAKE_CURRENT_SOURCE_DIR}/connectionconfig.h ${CMAKE_CURRENT_SOURCE_DIR}/connectionconfig.cpp ) diff --git a/src/imodbusport.h b/src/imodbusport.h new file mode 100644 index 0000000..b54f8e4 --- /dev/null +++ b/src/imodbusport.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Peter M. Groen + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#pragma once + +#include "connectionconfig.h" +#include + +namespace osdev { +namespace components { +namespace modbus { + +class IModbusPort +{ +public: + IModbusPort(const ConnectionConfig &con_config); + virtual ~IModbusPort() {} + + // The standard device interface. + virtual bool Open() const = 0; + virtual bool Close() const = 0; + virtual int Read(uint8_t *buffer) const = 0; + virtual int Write(uint8_t *buffer, size_t length) const = 0; +}; + +} /* End namespace modbus */ +} /* End namespace components */ +} /* End namespace osdev */ diff --git a/src/modbusbase.cpp b/src/modbus.cpp index e680b58..2565a3e 100644 --- a/src/modbusbase.cpp +++ b/src/modbus.cpp @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -#include "modbusbase.h" +#include "modbus.h" using namespace osdev::components::modbus; diff --git a/src/modbusbase.h b/src/modbus.h index 29b72cd..9514a98 100644 --- a/src/modbusbase.h +++ b/src/modbus.h @@ -50,11 +50,11 @@ namespace components { namespace modbus { // Modbus base class. Providing all Modbus PDUs without transport implementation -class ModbusBase +class Modbus { public: - ModbusBase(); - virtual ~ModbusBase(); + Modbus(); + virtual ~Modbus(); // Pure virtuals. Override when inherited. virtual bool Connect() = 0; diff --git a/src/modbusrtu.cpp b/src/modbusrtu.cpp index 2656c1d..e69de29 100644 --- a/src/modbusrtu.cpp +++ b/src/modbusrtu.cpp @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2022 Peter M. Groen - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -#include "modbusrtu.h" - -using namespace osdev::components::modbus; - -ModbusRTU::ModbusRTU() -{ - -} - -bool ModbusRTU::Connect() -{ - -} - -void ModbusRTU::Close() -{ - -} - -ssize_t ModbusRTU::modbusSend(uint8_t *to_send, size_t length) -{ - -} - -ssize_t ModbusRTU::modbusReceive(uint8_t *buffer) const -{ - -} diff --git a/src/modbusrtu.h b/src/modbusrtu.h index 393098a..e69de29 100644 --- a/src/modbusrtu.h +++ b/src/modbusrtu.h @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2022 Peter M. Groen - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -#pragma once - -#include "modbusbase.h" - -namespace osdev { -namespace components { -namespace modbus { - -class ModbusRTU : public ModbusBase -{ -public: - ModbusRTU(); - - // Pure virtuals. Override when inherited. - /*! - * \brief Connect - * \return - */ - virtual bool Connect() override; - - /*! - * \brief Close - */ - virtual void Close() override; - -private: - /*! - * \brief modbusSend - * \param to_send - * \param length - * \return - */ - virtual ssize_t modbusSend(uint8_t *to_send, size_t length) override; - - /*! - * \brief modbusReceive - * \param buffer - * \return - */ - virtual ssize_t modbusReceive(uint8_t *buffer) const override; -}; - -} // End namespace modbus -} // End namespace components -} // End namespace osdev diff --git a/src/modbustcp.cpp b/src/modbustcp.cpp index 0af8167..e69de29 100644 --- a/src/modbustcp.cpp +++ b/src/modbustcp.cpp @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2022 Peter M. Groen - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -#include "modbustcp.h" - -using namespace osdev::components::modbus; - -ModbusTCP::ModbusTCP(const ConnectionConfig &port_configuration) -{ - setMaxMessageLength(260); // Maximum Messagelength over TCP -} - -bool ModbusTCP::Connect() -{ - if(m_host.empty() || m_port == 0) - { - LOG("Missing host and port"); - return false; - } - - m_socket = socket(AF_INET, SOCK_STREAM, 0); - if(!X_ISVALIDSOCKET(m_socket)) - { - LOG("Errror Opening Socket"); - return false; - } - - struct timeval timeout {}; - timeout.tv_sec = 20; // After 20 seconds connect() will timeout - timeout.tv_usec = 0; - - setsockopt(m_socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast(&timeout), sizeof(timeout)); - setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast(&timeout), sizeof(timeout)); - m_server.sin_family = AF_INET; - m_server.sin_addr.s_addr = inet_addr(m_host.c_str()); - m_server.sin_port = htons(m_port); - - if(!X_ISCONNECTSUCCEED(connect(m_socket, reinterpret_cast(&m_server), sizeof(m_server)))) - { - LOG("Connection Error"); - return false; - } - - ModbusBase::setConnected(true); - return true; -} - -void ModbusTCP::Close() -{ - X_CLOSE_SOCKET(m_socket); -} - -ssize_t ModbusTCP::modbusSend(uint8_t *to_send, size_t length) -{ - uint32_t message_id = getMessageId(); - setMessageId(message_id++); - - return send(m_socket, reinterpret_cast(to_send), static_cast(length), 0); -} - -ssize_t ModbusTCP::modbusReceive(uint8_t *buffer) const -{ - return recv(m_socket, reinterpret_cast(buffer), getMaxMessageLength(), 0); -} diff --git a/src/modbustcp.h b/src/modbustcp.h index 567a809..e69de29 100644 --- a/src/modbustcp.h +++ b/src/modbustcp.h @@ -1,78 +0,0 @@ -/* - * Copyright (c) 2022 Peter M. Groen - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -#pragma once - -#include "modbusbase.h" -#include "connectionconfig.h" - -#include -#include -#include -#include - -namespace osdev { -namespace components { -namespace modbus { - -using X_SOCKET = int; -using SOCKADDR = struct sockaddr; -using SOCKADDR_IN = struct sockaddr_in; - -#define X_ISVALIDSOCKET(s) ((s) >= 0) -#define X_CLOSE_SOCKET(s) close(s) -#define X_ISCONNECTSUCCEED(s) ((s) >= 0) - -/*! - * \brief The ModbusTCP class - */ -class ModbusTCP : public ModbusBase -{ -public: - /*! - * \brief ModbusTCP - * \param port_configuration - */ - ModbusTCP(const ConnectionConfig &port_configuration); - - // Pure virtuals. Override when inherited. - /*! - * \brief Connect - * \return - */ - virtual bool Connect() override; - - /*! - * \brief Close - */ - virtual void Close() override; - -private: // Methods - /*! - * \brief modbusSend - * \param to_send - * \param length - * \return - */ - virtual ssize_t modbusSend(uint8_t *to_send, size_t length) override; - - /*! - * \brief modbusReceive - * \param buffer - * \return - */ - virtual ssize_t modbusReceive(uint8_t *buffer) const override; - -private: // Members - uint16_t m_port {}; - std::string m_host{}; - X_SOCKET m_socket{}; - SOCKADDR_IN m_server{}; -}; - -} // End namespace modbus -} // End namespace components -} // End namespace osdev