From f80bfff01765c806fbefbeb2a05d460c0160a709 Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Sun, 25 Sep 2022 23:09:10 +0200 Subject: [PATCH] Implmenting ModbusBase class --- src/modbusbase.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/modbusbase.h | 24 ++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) diff --git a/src/modbusbase.cpp b/src/modbusbase.cpp index 90a6f1a..847e170 100644 --- a/src/modbusbase.cpp +++ b/src/modbusbase.cpp @@ -9,12 +9,77 @@ int ModbusBase::readCoils(uint16_t address, uint16_t amount, bool *buffer) { + if (m_connected) + { + if (amount > 2040) + { + setBadInput(); + return EX_BAD_DATA; + } + modbusRead(address, amount, READ_COILS); + uint8_t to_rec[MAX_MSG_LENGTH]; + ssize_t result = modbusReceive(to_rec); + if (result == -1) + { + setBadConnection(); + return BAD_CON; + } + + modbusErrorHandle(to_rec, READ_COILS); + if (err) + { + return err_no; + } + + for (auto i = 0; i < amount; i++) + { + buffer[i] = (bool)((to_rec[9u + i / 8u] >> (i % 8u)) & 1u); + } + return 0; + } + else + { + setBadConnection(); + return BAD_CON; + } } int ModbusBase::readInputBits(uint16_t address, uint16_t amount, bool *buffer) { + if (m_connected) + { + if (amount > 2040) + { + setBadInput(); + return EX_BAD_DATA; + } + modbusRead(address, amount, READ_INPUT_BITS); + uint8_t to_rec[MAX_MSG_LENGTH]; + ssize_t result = modbusReceive(to_rec); + if (result == -1) + { + setBadConnection(); + return BAD_CON; + } + + if (err) + { + return err_no; + } + + for (auto i = 0; i < amount; i++) + { + buffer[i] = (bool)((to_rec[9u + i / 8u] >> (i % 8u)) & 1u); + } + modbusErrorHandle(to_rec, READ_INPUT_BITS); + return 0; + } + else + { + return BAD_CON; + } } int ModbusBase::readHoldingRegisters(uint16_t address, uitn16_t amount, uint16_t *buffer) @@ -54,13 +119,39 @@ int ModbusBase::readInputRegisters(uint16_t address, uint16_t amount, uint16_t * { modbusRead(address, amount, READ_INPUT_REGS); uint8_t to_rec[MSG_MAX_LENGTH]; + size_t result = modbusReceive(to_rec); + if (result == -1) + { + setBadConnection(); + return BAD_CON; + } + modbusErrorHandle(to_rec, READ_INPUT_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::writeCoil(uint16_t address, const bool &to_write) { - + if (m_connected) + { + int value = to_write * 0xFF00; + } } int ModbusBase::writeRegister(uint16_t address, const uint16_t &value) diff --git a/src/modbusbase.h b/src/modbusbase.h index 12fe5b5..79949cc 100644 --- a/src/modbusbase.h +++ b/src/modbusbase.h @@ -67,7 +67,22 @@ public: virtual bool isConnected() const - 0; // Modbus implementation(s) + /*! + * Read Coils + * MODBUS Function 0x01 + * \param address - Reference Address + * \param amount - Amount of Coils to Read + * \param buffer - Buffer to Store Data Read from Coils + */ int readCoils(uint16_t address, uint16_t amount, bool *buffer); // Replace buffer with something sensible? + + /*! + * Read Input Bits + * MODBUS Function 0x02 + * \param address - Refernce Address + * \param amount - Amount of BITS to Read + * \param buffer - Buffer to store Data Read from Input Bits + */ int readInputBits(uint16_t address, uint16_t amount, bool *buffer); // Replace buffer with something sensible? /*! @@ -88,9 +103,18 @@ public: */ int readInputRegisters(uint16_t address, uint16_t amount, uint16_t *buffer); + /*! + * Write Single Coils + * MODBUS Function 0x05 + * \param address - Reference Address + * \param to_write - Value to be written to Coil + */ int writeCoil(uint16_t address, const bool &to_write); + int writeRegister(uint16_t address, const uint16_t &value); + int writeCoils(uint16_t address, uint16_t amount, const bool *value); + int writeRegisters(uint16_t address, uint16_t amount, const uint16_t *value); private: // Methods -- libgit2 0.21.4