Commit da70a172565c06f34b94b13662d8a591ac3a4216
1 parent
481027bb
Added Request Class
Showing
2 changed files
with
104 additions
and
2 deletions
include/request.h
0 → 100644
| 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 | |
| 8 | + | |
| 9 | +#include <cstdint> | |
| 10 | +#include <functional> | |
| 11 | +#include <variant> | |
| 12 | +#include <vector> | |
| 13 | + | |
| 14 | +/// The response based on the \ref Request made. | |
| 15 | +struct Response | |
| 16 | +{ | |
| 17 | + /// Whether the \ref Request was successful. | |
| 18 | + /// Positive values indicate success and how many entries are in the \ref responseBuffer. | |
| 19 | + /// Negative values indicate failure and can be resolved with \ref Modbus::ErrorToString() to a string. | |
| 20 | + int resultValue = -1; | |
| 21 | + | |
| 22 | + /// Buffer contaiining the response of the \ref Request. | |
| 23 | + std::vector<std::variant<uint8_t, uint16_t>> responseBuffer = {}; | |
| 24 | +}; | |
| 25 | + | |
| 26 | +/// A request to be made over ModBus. | |
| 27 | +/// | |
| 28 | +/// TThis contains all the information the stack needs to actually make the request. | |
| 29 | +class Request | |
| 30 | +{ | |
| 31 | +public: | |
| 32 | + /// Function code of the request. | |
| 33 | + /// We only support a small subset of the ModBus Standard. | |
| 34 | + enum class FunctionCode | |
| 35 | + { | |
| 36 | + READ_COILS, | |
| 37 | + READ_DISCRETE_INPUTS, | |
| 38 | + READ_HOLDING_REGISTERS, | |
| 39 | + READ_INPUT_REGISTERS, | |
| 40 | + WRITE_SINGLE_COIL, | |
| 41 | + WRITE_SINGLE_REGISTER, | |
| 42 | + WRITE_MULTIPLE_COILS, | |
| 43 | + WRITE_MULTIPLE_REGISTERS, | |
| 44 | + WRITE_FILE_RECORD, | |
| 45 | + }; | |
| 46 | + | |
| 47 | + /// Constructor taking all necessary information to create a request. | |
| 48 | + /// | |
| 49 | + /// @param functionCode - The code of the action this request wants to perform. | |
| 50 | + /// @param slaveId - The Device ID number. | |
| 51 | + /// @param startAddress - Register number to start reading from or writing to. | |
| 52 | + /// @param numberOfItems - Number of items to read or write from start_address. | |
| 53 | + /// @param callback - Function to call when this request returns a response. | |
| 54 | + Request(Request::FunctionCode functionCode, | |
| 55 | + int slaveId, | |
| 56 | + int startAddress, | |
| 57 | + int numberOfItems, | |
| 58 | + std::function<bool(Response)> callback) | |
| 59 | + : m_functionCode(functionCode) | |
| 60 | + , m_slaveId(slaveId) | |
| 61 | + , m_startAddress(startAddress) | |
| 62 | + , m_numberOfItems(numberOfItems) | |
| 63 | + { | |
| 64 | + callbacks.push_back(callback); | |
| 65 | + } | |
| 66 | + | |
| 67 | + /// @return The functioncode this request handles. | |
| 68 | + FunctionCode GetFunctionCode() const {return m_functionCode;} | |
| 69 | + | |
| 70 | + /// @return The slaveID of the device this request is intended for. | |
| 71 | + int GetSlaveId() const {return m_slaveId;} | |
| 72 | + | |
| 73 | + /// @return The start Address of the register(s) we're reading. | |
| 74 | + int GetStartAddress() const {return m_startAddress;} | |
| 75 | + | |
| 76 | + /// @return The number of registers we read from the \ref m_startAddress. | |
| 77 | + int GetNumberOfItems() const {return m_numberOfItems;} | |
| 78 | + | |
| 79 | + /// @return The values read from, or to be written to, the device. | |
| 80 | + /// @note never change the length of this vector. | |
| 81 | + std::vector<int> GetDataBuffer() const {return m_dataBuffer;} | |
| 82 | + | |
| 83 | + /// @return Callbacks registered to call when new data is available. | |
| 84 | + /// @note Functionn pointer returns False if the device state is OFFLINE. True, otherwise. | |
| 85 | + std::vector<std::function<bool(Response)>> callbacks; | |
| 86 | + | |
| 87 | +private: | |
| 88 | + /// Function code of this request. | |
| 89 | + FunctionCode m_functionCode; | |
| 90 | + | |
| 91 | + /// SlaveID of this request. | |
| 92 | + int m_slaveId; | |
| 93 | + | |
| 94 | + /// Start address of the register for this request | |
| 95 | + int m_startAddress; | |
| 96 | + | |
| 97 | + /// Amount of registers to read/write in this request | |
| 98 | + int m_numberOfItems; | |
| 99 | + | |
| 100 | + /// Data to send to the device; must be the same size as \ref m_numberOfItems. | |
| 101 | + std::vector<int> m_dataBuffer; | |
| 102 | +}; | ... | ... |
src/modbustcp.cpp
| ... | ... | @@ -9,8 +9,8 @@ |
| 9 | 9 | using namespace osdev::components::modbus; |
| 10 | 10 | |
| 11 | 11 | ModbusTcp::ModbusTcp(const ConnectionConfig &con_config) |
| 12 | - : m_host( con_config.getIpAddress() ) | |
| 13 | - , m_port( con_config.getPortNumber() ) | |
| 12 | + : m_host(con_config.getIpAddress()) | |
| 13 | + , m_port(con_config.getPortNumber()) | |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | 16 | } | ... | ... |