Commit 5718c3258bd19b0e0c1d4c45519f8bf829be8f24

Authored by Peter M. Groen
1 parent fed64770

Implementing Modbus

src/connectionconfig.h renamed to include/connectionconfig.h
include/imodbus.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 "request.h"
  10 +#include "connectionconfig.h"
  11 +#include <vector>
  12 +
  13 +namespace osdev {
  14 +namespace components {
  15 +namespace modbus {
  16 +
  17 +class IModBus
  18 +{
  19 +public:
  20 + virtual ~IModBus() {}
  21 +
  22 + virtual bool Open(const ConnectionConfig &connection_config) = 0;
  23 + virtual bool Close() = 0;
  24 + virtual std::vector<uint8_t> Read(const Request &request) = 0;
  25 + virtual std::vector<uint8_t> Write(const Request &request) = 0;
  26 +};
  27 +
  28 +} /* End namespace modbus */
  29 +} /* End namespace components */
  30 +} /* End namespace osdev */
include/request.h
@@ -111,25 +111,25 @@ public: @@ -111,25 +111,25 @@ public:
111 /// @note never change the length of this vector. 111 /// @note never change the length of this vector.
112 std::vector<uint8_t> getDataBuffer() const {return m_dataBuffer;} 112 std::vector<uint8_t> getDataBuffer() const {return m_dataBuffer;}
113 113
114 - /// @return Callbacks registered to call when new data is available.  
115 - /// @note Functionn pointer returns False if the device state is OFFLINE. True, otherwise.  
116 - std::vector<std::function<bool(Response)>> callbacks;  
117 -  
118 private: 114 private:
119 /// Function code of this request. 115 /// Function code of this request.
120 - FunctionCode m_functionCode; 116 + FunctionCode m_functionCode = FunctionCode::FC_UNKNOWN;
121 117
122 /// SlaveID of this request. 118 /// SlaveID of this request.
123 - uint8_t m_slaveId; 119 + uint8_t m_slaveId = 0;
124 120
125 /// Start address of the register for this request 121 /// Start address of the register for this request
126 - uint8_t m_startAddress; 122 + uint8_t m_startAddress = 0x00;
127 123
128 /// Amount of registers to read/write in this request 124 /// Amount of registers to read/write in this request
129 - uint8_t m_numberOfRegisters; 125 + uint8_t m_numberOfRegisters = 0x00;
130 126
131 /// Data to send to the device; must be the same size as \ref m_numberOfItems. 127 /// Data to send to the device; must be the same size as \ref m_numberOfItems.
132 - std::vector<uint8_t> m_dataBuffer; 128 + std::vector<uint8_t> m_dataBuffer = {};
  129 +
  130 + /// @return Callbacks registered to call when new data is available.
  131 + /// @note Functionn pointer returns False if the device state is OFFLINE. True, otherwise.
  132 + std::vector<std::function<bool(Response)>> callbacks = {};
133 }; 133 };
134 134
135 } /* End namespace modbus */ 135 } /* End namespace modbus */
src/CMakeLists.txt
@@ -19,7 +19,6 @@ set(SRC_LIST @@ -19,7 +19,6 @@ set(SRC_LIST
19 ${CMAKE_CURRENT_SOURCE_DIR}/modbustcp.cpp 19 ${CMAKE_CURRENT_SOURCE_DIR}/modbustcp.cpp
20 ${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.h 20 ${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.h
21 ${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.cpp 21 ${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.cpp
22 - ${CMAKE_CURRENT_SOURCE_DIR}/connectionconfig.h  
23 ${CMAKE_CURRENT_SOURCE_DIR}/connectionconfig.cpp 22 ${CMAKE_CURRENT_SOURCE_DIR}/connectionconfig.cpp
24 ) 23 )
25 24
src/modbus.cpp
1 #include "modbus.h" 1 #include "modbus.h"
2 2
  3 +using namespace osdev::components::modbus;
  4 +
3 ModBus::ModBus() 5 ModBus::ModBus()
4 { 6 {
5 7
6 } 8 }
  9 +
  10 +bool ModBus::Open(const ConnectionConfig &connection_config)
  11 +{
  12 + (void)connection_config;
  13 +
  14 + return true;
  15 +}
  16 +
  17 +bool ModBus::Close()
  18 +{
  19 + return true;
  20 +}
  21 +
  22 +std::vector<uint8_t> ModBus::Read(const Request &request)
  23 +{
  24 + (void)request;
  25 +
  26 + return std::vector<uint8_t>();
  27 +}
  28 +
  29 +std::vector<uint8_t> ModBus::Write(const Request &request)
  30 +{
  31 + (void)request;
  32 +
  33 + return std::vector<uint8_t>();
  34 +}
src/modbus.h
1 -#ifndef MODBUS_H  
2 -#define MODBUS_H 1 +/*
  2 + * Copyright (c) 2023 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
3 8
  9 +#include "imodbus.h"
4 10
5 -class ModBus 11 +namespace osdev {
  12 +namespace components {
  13 +namespace modbus {
  14 +
  15 +class ModBus : public IModBus
6 { 16 {
7 public: 17 public:
8 ModBus(); 18 ModBus();
  19 +
  20 + virtual ~ModBus() {}
  21 +
  22 + virtual bool Open(const ConnectionConfig &connection_config) override;
  23 + virtual bool Close() override;
  24 + virtual std::vector<uint8_t> Read(const Request &request) override;
  25 + virtual std::vector<uint8_t> Write(const Request &request) override;
9 }; 26 };
10 27
11 -#endif // MODBUS_H 28 +} /* End namespace modbus */
  29 +} /* End namespace components */
  30 +} /* End namespace osdev */