Commit 0be34396769060a0e880a09c274728b73ada88f4
1 parent
77ba000d
Implmenting ModbusBase class
Showing
7 changed files
with
189 additions
and
15 deletions
src/connectionconfig.cpp
| @@ -4,3 +4,11 @@ | @@ -4,3 +4,11 @@ | ||
| 4 | * This source code is licensed under the MIT license found in the | 4 | * This source code is licensed under the MIT license found in the |
| 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 | +#include "connectionconfig.h" | ||
| 8 | + | ||
| 9 | +using namespace osdev::components::modbus; | ||
| 10 | + | ||
| 11 | +ConnectionConfig::ConnectionConfig() | ||
| 12 | +{ | ||
| 13 | + | ||
| 14 | +} |
src/connectionconfig.h
src/modbusbase.h
| @@ -6,7 +6,9 @@ | @@ -6,7 +6,9 @@ | ||
| 6 | */ | 6 | */ |
| 7 | #pragma once | 7 | #pragma once |
| 8 | 8 | ||
| 9 | +#include <cstring> | ||
| 9 | #include <string> | 10 | #include <string> |
| 11 | +#include <stdint.h> | ||
| 10 | 12 | ||
| 11 | // Create a simple logger for console output during debugging. | 13 | // Create a simple logger for console output during debugging. |
| 12 | // TODO: Replace with a custom logger by using std::function | 14 | // TODO: Replace with a custom logger by using std::function |
| @@ -54,23 +56,12 @@ namespace modbus { | @@ -54,23 +56,12 @@ namespace modbus { | ||
| 54 | class ModbusBase | 56 | class ModbusBase |
| 55 | { | 57 | { |
| 56 | public: | 58 | public: |
| 57 | - bool err{}; | ||
| 58 | - int err_no{}; | ||
| 59 | - std::string m_error_msg; | ||
| 60 | - | ||
| 61 | ModbusBase(); | 59 | ModbusBase(); |
| 62 | virtual ~ModbusBase(); | 60 | virtual ~ModbusBase(); |
| 63 | 61 | ||
| 64 | - /*! | ||
| 65 | - * Set slave id for this context | ||
| 66 | - * \param slave_id - The slave id this system is known under. | ||
| 67 | - */ | ||
| 68 | - void setSlaveId(int slave_id) { m_slaveId = slave_id; } | ||
| 69 | - | ||
| 70 | // Pure virtuals. Override when inherited. | 62 | // Pure virtuals. Override when inherited. |
| 71 | - virtual bool Connect() const = 0; | 63 | + virtual bool Connect() = 0; |
| 72 | virtual void Close() = 0; | 64 | virtual void Close() = 0; |
| 73 | - virtual bool isConnected() const = 0; | ||
| 74 | 65 | ||
| 75 | // Modbus implementation(s) | 66 | // Modbus implementation(s) |
| 76 | /*! | 67 | /*! |
| @@ -143,6 +134,23 @@ public: | @@ -143,6 +134,23 @@ public: | ||
| 143 | */ | 134 | */ |
| 144 | int writeRegisters(uint16_t address, uint16_t amount, const uint16_t *value); | 135 | int writeRegisters(uint16_t address, uint16_t amount, const uint16_t *value); |
| 145 | 136 | ||
| 137 | + // Getters and Setters. | ||
| 138 | + void setConnected(bool connected = false){ m_connected = connected;} | ||
| 139 | + bool getConnected() const { return m_connected; } | ||
| 140 | + void setMessageId( uint32_t message_id ) { m_msg_id = message_id; } | ||
| 141 | + uint32_t getMessageId() const { return m_msg_id; } | ||
| 142 | + void setSlaveId(int slave_id){ m_slaveId = slave_id; } | ||
| 143 | + int getSlaveId() const { return m_slaveId; } | ||
| 144 | + void setError(bool error, int error_number = 0, const std::string &error_message = std::string()) | ||
| 145 | + { | ||
| 146 | + m_error = error; | ||
| 147 | + m_error_number = error_number; | ||
| 148 | + m_error_message = error_message; | ||
| 149 | + } | ||
| 150 | + bool getError() const { return m_error; } | ||
| 151 | + int getErrorNumber() const { return m_error_number; } | ||
| 152 | + std::string getErrorMessage() const { return m_error_message;} | ||
| 153 | + | ||
| 146 | private: // Methods | 154 | private: // Methods |
| 147 | /*! | 155 | /*! |
| 148 | * Modbus Request Builder | 156 | * Modbus Request Builder |
| @@ -201,6 +209,9 @@ private: // Members (Giggity!) | @@ -201,6 +209,9 @@ private: // Members (Giggity!) | ||
| 201 | bool m_connected{}; | 209 | bool m_connected{}; |
| 202 | uint32_t m_msg_id{}; | 210 | uint32_t m_msg_id{}; |
| 203 | int m_slaveId{}; | 211 | int m_slaveId{}; |
| 212 | + bool m_error{}; | ||
| 213 | + int m_error_number{}; | ||
| 214 | + std::string m_error_message; | ||
| 204 | 215 | ||
| 205 | }; | 216 | }; |
| 206 | 217 |
src/modbusrtu.cpp
| @@ -12,3 +12,23 @@ ModbusRTU::ModbusRTU() | @@ -12,3 +12,23 @@ ModbusRTU::ModbusRTU() | ||
| 12 | { | 12 | { |
| 13 | 13 | ||
| 14 | } | 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
| @@ -16,6 +16,34 @@ class ModbusRTU : public ModbusBase | @@ -16,6 +16,34 @@ class ModbusRTU : public ModbusBase | ||
| 16 | { | 16 | { |
| 17 | public: | 17 | public: |
| 18 | ModbusRTU(); | 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; | ||
| 19 | }; | 47 | }; |
| 20 | 48 | ||
| 21 | } // End namespace modbus | 49 | } // End namespace modbus |
src/modbustcp.cpp
| @@ -8,7 +8,62 @@ | @@ -8,7 +8,62 @@ | ||
| 8 | 8 | ||
| 9 | using namespace osdev::components::modbus; | 9 | using namespace osdev::components::modbus; |
| 10 | 10 | ||
| 11 | -ModbusTCP::ModbusTCP() | 11 | +ModbusTCP::ModbusTCP(const ConnectionConfig &port_configuration) |
| 12 | +{ | ||
| 13 | + | ||
| 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 | + | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +bool ModbusTCP::isConnected() const | ||
| 57 | +{ | ||
| 58 | + | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +ssize_t ModbusTCP::modbusSend(uint8_t *to_send, size_t length) | ||
| 62 | +{ | ||
| 63 | + | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +ssize_t ModbusTCP::modbusReceive(uint8_t *buffer) const | ||
| 12 | { | 67 | { |
| 13 | 68 | ||
| 14 | } | 69 | } |
src/modbustcp.h
| @@ -7,15 +7,67 @@ | @@ -7,15 +7,67 @@ | ||
| 7 | #pragma once | 7 | #pragma once |
| 8 | 8 | ||
| 9 | #include "modbusbase.h" | 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> | ||
| 10 | 16 | ||
| 11 | namespace osdev { | 17 | namespace osdev { |
| 12 | namespace components { | 18 | namespace components { |
| 13 | namespace modbus { | 19 | namespace modbus { |
| 14 | 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 | + | ||
| 15 | class ModbusTCP : public ModbusBase | 29 | class ModbusTCP : public ModbusBase |
| 16 | { | 30 | { |
| 17 | public: | 31 | public: |
| 18 | - ModbusTCP(); | 32 | + /*! |
| 33 | + * \brief ModbusTCP | ||
| 34 | + * \param port_configuration | ||
| 35 | + */ | ||
| 36 | + ModbusTCP(const ConnectionConfig &port_configuration); | ||
| 37 | + | ||
| 38 | + // Pure virtuals. Override when inherited. | ||
| 39 | + /*! | ||
| 40 | + * \brief Connect | ||
| 41 | + * \return | ||
| 42 | + */ | ||
| 43 | + virtual bool Connect() override; | ||
| 44 | + | ||
| 45 | + /*! | ||
| 46 | + * \brief Close | ||
| 47 | + */ | ||
| 48 | + virtual void Close() override; | ||
| 49 | + | ||
| 50 | +private: // Methods | ||
| 51 | + /*! | ||
| 52 | + * \brief modbusSend | ||
| 53 | + * \param to_send | ||
| 54 | + * \param length | ||
| 55 | + * \return | ||
| 56 | + */ | ||
| 57 | + virtual ssize_t modbusSend(uint8_t *to_send, size_t length) override; | ||
| 58 | + | ||
| 59 | + /*! | ||
| 60 | + * \brief modbusReceive | ||
| 61 | + * \param buffer | ||
| 62 | + * \return | ||
| 63 | + */ | ||
| 64 | + virtual ssize_t modbusReceive(uint8_t *buffer) const override; | ||
| 65 | + | ||
| 66 | +private: // Members | ||
| 67 | + uint16_t m_port {}; | ||
| 68 | + std::string m_host; | ||
| 69 | + X_SOCKET m_socket{}; | ||
| 70 | + SOCKADDR_IN m_server{}; | ||
| 19 | }; | 71 | }; |
| 20 | 72 | ||
| 21 | } // End namespace modbus | 73 | } // End namespace modbus |