From e651a10fb00874c3dd93ef07429a16b5490a7d88 Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Sun, 25 Sep 2022 20:29:02 +0200 Subject: [PATCH] Implmenting ModbusBase class --- src/modbusbase.cpp | 38 +++++++++++++++++++++++++++++++++++--- src/modbusbase.h | 30 +++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/modbusbase.cpp b/src/modbusbase.cpp index e3bd4bc..90a6f1a 100644 --- a/src/modbusbase.cpp +++ b/src/modbusbase.cpp @@ -19,12 +19,43 @@ int ModbusBase::readInputBits(uint16_t address, uint16_t amount, bool *buffer) int ModbusBase::readHoldingRegisters(uint16_t address, uitn16_t amount, uint16_t *buffer) { - + if (m_connected) + { + modbusRead(address, amount, READ_REGS); + uint8_t to_rec[MAX_MSG_LENGTH]; // <-- Transport layer dependent .. ? + ssize_t result = modbusReceive(to_rec); + if (result == -1) + { + setBadConnection(); + return BAD_CON; + } + modbusErrorHandle(to_rec, READ_REGS); + if (err) + { + return err_no; + } + for (auto i = 0; i < amount; i++) + { + buffer[i] = ((uint16_t)to_rec[9u + 2u * i]) << 8u; + buffer[i] += (uint16_t)to_rec[10u + 2u * i]; + } + return 0; + } + else + { + setBadConnection(); + return BAD_CON; + } } int ModbusBase::readInputRegisters(uint16_t address, uint16_t amount, uint16_t *buffer) { + if (m_connected) + { + modbusRead(address, amount, READ_INPUT_REGS); + uint8_t to_rec[MSG_MAX_LENGTH]; + } } int ModbusBase::writeCoil(uint16_t address, const bool &to_write) @@ -68,8 +99,9 @@ int ModbusBase::modbusRead(uint16_t address, uint16_t amount, int function_code) uint8_t to_send[12]; buildRequest(to_send, address, function_code); to_send[5] = 6; - to_send[10] = (uint8_t) - } + to_send[10] = (uint8_t)(amount >> 8u); + to_send[11] = (uint8_t)(amount & 0x00FFu); + return modbusSend(to_send, 12); } int ModbusBase::modbusWrite(uint16_t address, uint16_t amount, int function_code, const uint16_t *value) diff --git a/src/modbusbase.h b/src/modbusbase.h index 6b7af77..12fe5b5 100644 --- a/src/modbusbase.h +++ b/src/modbusbase.h @@ -69,7 +69,23 @@ public: // Modbus implementation(s) int readCoils(uint16_t address, uint16_t amount, bool *buffer); // Replace buffer with something sensible? int readInputBits(uint16_t address, uint16_t amount, bool *buffer); // Replace buffer with something sensible? + + /*! + * Read Holding Registers + * MODBUS Function 0x03 + * \param address - Reference Address + * \param amount - Amount of Registers to Read + * \param buffer - Buffer to Store Data Read from Registers + */ int readHoldingRegisters(uint16_t address, uitn16_t amount, uint16_t *buffer); + + /*! + * Read Input Registers + * MODBUS Function 0x04 + * \param address - Reference Address + * \param amount - Amount of registers to read + * \param buffer - Buffer to store Data Read from Registers + */ int readInputRegisters(uint16_t address, uint16_t amount, uint16_t *buffer); int writeCoil(uint16_t address, const bool &to_write); @@ -95,17 +111,17 @@ private: // Methods * * \return int */ - int modbusWrite(uint16_t address, uint16_t amount, int function_code, const uint16_t *value); + int modbusWrite(uint16_t address, uint16_t amount, int function_code, const uint16_t *value); virtual ssize_t modbusSend(uint8_t *to_send, size_t length) = 0; virtual ssize_t modbusReceive(uint8_t *buffer) const = 0; - void modbusErrorHandle(const uint8_t *msg, int function_code); - void setBadConnection(); - void setBadInput(); + void modbusErrorHandle(const uint8_t *msg, int function_code); + void setBadConnection(); + void setBadInput(); private: // Members (Giggity!) - bool m_connected{}; - uint32_t m_msg_id{}; - int m_slaveId{}; + bool m_connected{}; + uint32_t m_msg_id{}; + int m_slaveId{}; }; -- libgit2 0.21.4