Modbus cpp 0.1
Loading...
Searching...
No Matches
modbusException.hpp
1// Modbus for c++ <https://github.com/Mazurel/Modbus>
2// Copyright (c) 2020 Mateusz Mazur aka Mazurel
3// Licensed under: MIT License <http://opensource.org/licenses/MIT>
4
5#pragma once
6
7#include <cstdint>
8#include <cstring>
9#include <stdexcept>
10#include <vector>
11
12#include "modbusUtils.hpp"
13
17namespace MB {
23class ModbusException : public std::exception {
24 private:
25 uint8_t _slaveId;
26 bool _validSlave;
27 utils::MBErrorCode _errorCode;
28 utils::MBFunctionCode _functionCode;
29
30 public:
31 // We do not allow default CTORs: https://github.com/Mazurel/Modbus/issues/6
32 ModbusException() = delete;
33
42 explicit ModbusException(const std::vector<uint8_t> &inputData,
43 bool CRC = false) noexcept;
44
47 utils::MBErrorCode errorCode, uint8_t slaveId = 0xFF,
48 utils::MBFunctionCode functionCode = utils::Undefined) noexcept
49 : _slaveId(slaveId), _validSlave(true), _errorCode(errorCode),
50 _functionCode(functionCode) {}
51
52 /*
53 * Check if there is Modbus error in raw modbus input
54 * NOTE: this method doesn't detect invalid byte order, byte order is
55 * checked at ModbusRequest/ModbusResponse
56 * */
57 static bool exist(const std::vector<uint8_t> &inputData) noexcept {
58 if (inputData.size() < 2) // TODO Figure out better solution to such mistake
59 return false;
60
61 return inputData[1] & 0b10000000;
62 }
63
64 /*
65 * Returns attached SlaveID
66 * NOTE: it is worth to check if slaveId is specified with isSlaveValid()
67 * */
68 [[nodiscard]] uint8_t slaveID() const noexcept { return _slaveId; }
69
71 [[nodiscard]] bool isSlaveValid() const noexcept { return _validSlave; }
72
74 void setSlaveID(uint8_t slaveId) noexcept {
75 _validSlave = true;
76 _slaveId = slaveId;
77 }
78
80 [[nodiscard]] utils::MBErrorCode getErrorCode() const noexcept { return _errorCode; }
81
86 [[nodiscard]] const char *what() const noexcept override {
87 auto og = toString();
88 char *str = new char[og.size()];
89 stpcpy(str, og.c_str());
90 return str;
91 }
92
94 [[nodiscard]] std::string toString() const noexcept;
96 [[nodiscard]] std::vector<uint8_t> toRaw() const noexcept;
97
98 [[nodiscard]] utils::MBFunctionCode functionCode() const noexcept {
99 return _functionCode;
100 }
101
102 void setFunctionCode(utils::MBFunctionCode functionCode) noexcept {
103 _functionCode = functionCode;
104 }
105};
106} // namespace MB
Definition modbusException.hpp:23
const char * what() const noexcept override
Definition modbusException.hpp:86
std::string toString() const noexcept
Returns string representation of object.
Definition modbusException.cpp:36
ModbusException(utils::MBErrorCode errorCode, uint8_t slaveId=0xFF, utils::MBFunctionCode functionCode=utils::Undefined) noexcept
Constructs Exception based on error code, function code and slaveId.
Definition modbusException.hpp:46
bool isSlaveValid() const noexcept
Checks if SlaveID is specified.
Definition modbusException.hpp:71
void setSlaveID(uint8_t slaveId) noexcept
Sets SlaveID.
Definition modbusException.hpp:74
std::vector< uint8_t > toRaw() const noexcept
Converts object to modbus byte representation.
Definition modbusException.cpp:53
utils::MBErrorCode getErrorCode() const noexcept
Returns detected error code.
Definition modbusException.hpp:80
MBErrorCode
Definition modbusUtils.hpp:25
MBFunctionCode
All modbus standard function codes + Undefined one.
Definition modbusUtils.hpp:117
Definition crc.hpp:5