From 31c6cd9606bef787e5aa31397ef4b7249cd3575b Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Fri, 25 Jul 2025 10:57:17 +0200 Subject: [PATCH] improved bus wait with raii class that updates last access --- lib/RS485/RS485_Driver.cpp | 75 +++++++++++++++++++++--------------- lib/RS485/RS485_Driver.h | 2 +- lib/RS485/busdelay.h | 37 ++++++++++++++++++ lib/SENECA/S50140_Driver.cpp | 53 +++++++++++++------------ lib/SENECA/S50140_Driver.h | 4 +- lib/TEMP/R4DCB08_Driver.cpp | 38 ++++++++---------- lib/TEMP/R4DCB08_Driver.h | 1 + lib/utils/utils.cpp | 17 +++++++- lib/utils/utils.h | 4 ++ src/commands.cpp | 5 ++- src/commands.h | 2 + src/main.cpp | 26 +++++-------- src/remoteIO.cpp | 32 +++++++-------- src/remoteIO.h | 4 +- 14 files changed, 173 insertions(+), 127 deletions(-) create mode 100644 lib/RS485/busdelay.h diff --git a/lib/RS485/RS485_Driver.cpp b/lib/RS485/RS485_Driver.cpp index 45296c5..02ab562 100644 --- a/lib/RS485/RS485_Driver.cpp +++ b/lib/RS485/RS485_Driver.cpp @@ -3,8 +3,11 @@ #include #include +#include #include "utils.h" +#define BUS_DELAY drivers::BusDelay(m_lastAccess, c_minDelay, "MODBUS") + namespace drivers { @@ -80,26 +83,11 @@ namespace drivers return m_mutex; } - void MODBUS::delayAccess(const uint8_t device) - { - if (device == m_lastDevice) - return; - auto now = millis(); - if ((now - m_lastAccess) < c_minDelay) // fixed milliseconds delay between commands to different devices - { - LOG_DEBUG("MODBUS access delay", (now - m_lastAccess), "device", device); - delay(now - m_lastAccess); - } - m_lastDevice = device; - m_lastAccess = millis(); - } - // Func 0x01 const bool MODBUS::readCoils(const uint8_t device, const uint16_t reg, const uint16_t num, std::vector &coils) { constexpr uint8_t func = 0x01; - delayAccess(device); - LOG_DEBUG("Read coils: dev[", device, "], reg[", reg, "], num[", num, "]"); + LOG_DEBUG("Read coils: dev[", printHex(device).c_str(), "], reg[", printHex(reg).c_str(), "], num[", num, "]"); return readBinary(device, func, reg, num, coils); } @@ -107,8 +95,7 @@ namespace drivers const bool MODBUS::readInputs(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector &inputs) { constexpr uint8_t func = 0x02; - delayAccess(device); - LOG_DEBUG("Read multi inputs: dev[", device, "], reg[", reg, "], num[", num, "]"); + LOG_DEBUG("Read multi inputs: dev[", printHex(device).c_str(), "], reg[", printHex(reg).c_str(), "], num[", num, "]"); return readBinary(device, func, reg, num, inputs); } @@ -116,8 +103,7 @@ namespace drivers const bool MODBUS::readHoldingRegisters(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector &values) { constexpr uint8_t func = 0x03; - delayAccess(device); - LOG_DEBUG("Read multi holding registers: dev[", device, "], reg[", reg, "], num[", num, "]"); + LOG_DEBUG("Read multi holding registers: dev[", printHex(device).c_str(), "], reg[", printHex(reg).c_str(), "], num[", num, "]"); return readInteger(device, func, reg, num, values); } @@ -125,8 +111,7 @@ namespace drivers const bool MODBUS::readInputRegisters(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector &values) { constexpr uint8_t func = 0x04; - delayAccess(device); - LOG_DEBUG("Read multi input registers: dev[", device, "], reg[", reg, "], num[", num, "]"); + LOG_DEBUG("Read multi input registers: dev[", printHex(device).c_str(), "], reg[", printHex(reg).c_str(), "], num[", num, "]"); return readInteger(device, func, reg, num, values); } @@ -134,8 +119,7 @@ namespace drivers const bool MODBUS::writeCoil(const uint8_t device, const uint16_t coil, const bool value) { constexpr uint8_t func = 0x05; - delayAccess(device); - LOG_DEBUG("Write single coil: dev[", device, "], coil[", coil, "], value[", value ? "true" : "false", "]"); + LOG_DEBUG("Write single coil: dev[", printHex(device).c_str(), "], coil[", printHex(coil).c_str(), "], value[", value ? "true" : "false", "]"); return writeBinary(device, func, coil, {value}); } @@ -143,8 +127,7 @@ namespace drivers const bool MODBUS::writeRegister(const uint8_t device, const uint16_t reg, const uint16_t value) { constexpr uint8_t func = 0x06; - delayAccess(device); - LOG_DEBUG("Write single register: dev[", device, "], reg[", reg, "], value[", value, "]"); + LOG_DEBUG("Write single register: dev[", printHex(device).c_str(), "], reg[", printHex(reg).c_str(), "], value[", value, "]"); return writeInteger(device, func, reg, {value}, false); } @@ -152,8 +135,7 @@ namespace drivers const bool MODBUS::writeCoils(const uint8_t device, const uint16_t coils, const std::vector &values) { constexpr uint8_t func = 0x0F; - delayAccess(device); - LOG_DEBUG("Write multi coils: dev[", device, "], start[", coils, "], num[", values.size(), "]"); + LOG_DEBUG("Write multi coils: dev[", printHex(device).c_str(), "], start[", printHex(coils).c_str(), "], num[", values.size(), "]"); return writeBinary(device, func, coils, values); } @@ -161,8 +143,7 @@ namespace drivers const bool MODBUS::writeRegisters(const uint8_t device, const uint16_t reg, const std::vector &values) { constexpr uint8_t func = 0x10; - delayAccess(device); - LOG_DEBUG("Write multi registers: dev[", device, "], start[", reg, "], num[", values.size(), "]"); + LOG_DEBUG("Write multi registers: dev[", printHex(device).c_str(), "], start[", printHex(reg).c_str(), "], num[", values.size(), "]"); return writeInteger(device, func, reg, values, true); } @@ -172,6 +153,13 @@ namespace drivers const bool MODBUS::readBinary(const uint8_t device, const uint8_t func, const uint16_t reg, const uint16_t bits, std::vector &out) { + // Delay Bus Access between different devices + if (device != m_lastDevice) + { + LOG_WARN("MODBUS device change from ", printHex(m_lastDevice).c_str(), "to", printHex(device).c_str()); + BUS_DELAY; + m_lastDevice = device; + } if (!write(singleRequest(device, func, reg, bits))) { LOG_ERROR("Failed send readBinary command"); @@ -222,6 +210,13 @@ namespace drivers const bool MODBUS::readInteger(const uint8_t device, const uint8_t func, const uint16_t reg, const uint16_t num, std::vector &out) { + // Delay Bus Access between different devices + if (device != m_lastDevice) + { + LOG_WARN("MODBUS device change from ", printHex(m_lastDevice).c_str(), "to", printHex(device).c_str()); + BUS_DELAY; + m_lastDevice = device; + } if (!write(singleRequest(device, func, reg, num))) { LOG_ERROR("Failed send readInteger command"); @@ -268,6 +263,13 @@ namespace drivers const bool MODBUS::writeBinary(const uint8_t device, const uint8_t func, const uint16_t reg, const std::vector &in) { + // Delay Bus Access between different devices + if (device != m_lastDevice) + { + LOG_WARN("MODBUS device change from ", printHex(m_lastDevice).c_str(), "to", printHex(device).c_str()); + BUS_DELAY; + m_lastDevice = device; + } const uint16_t bits(in.size()); std::vector bitsOut; if (bits == 1) // if single coil value must be 0x00FF[00] for on[off] @@ -320,6 +322,13 @@ namespace drivers const bool MODBUS::writeInteger(const uint8_t device, const uint8_t func, const uint16_t reg, const std::vector &in, const bool multi) { + // Delay Bus Access between different devices + if (device != m_lastDevice) + { + LOG_WARN("MODBUS device change from ", printHex(m_lastDevice).c_str(), "to", printHex(device).c_str()); + BUS_DELAY; + m_lastDevice = device; + } const uint16_t num(in.size()); if (!multi) { @@ -439,10 +448,12 @@ namespace drivers // verify crc code if (highByte(computedCrc) != crcHi || lowByte(computedCrc) != crcLo) { - LOG_ERROR("Failed verify CRC code: comp[", computedCrc, "], rec[", receivedCrc, "]"); + LOG_ERROR("Failed verify CRC code: comp[", printHex(computedCrc).c_str(), "], rec[", printHex(receivedCrc).c_str(), "]"); return false; } return true; } -} \ No newline at end of file +} + +#undef BUS_DELAY diff --git a/lib/RS485/RS485_Driver.h b/lib/RS485/RS485_Driver.h index 275996f..417b352 100644 --- a/lib/RS485/RS485_Driver.h +++ b/lib/RS485/RS485_Driver.h @@ -34,7 +34,7 @@ namespace drivers const uint8_t c_respHeaderSize = 3; const uint8_t c_respCrcSize = 2; - const uint32_t c_minDelay = 500; + const uint32_t c_minDelay = 250; typedef struct { diff --git a/lib/RS485/busdelay.h b/lib/RS485/busdelay.h new file mode 100644 index 0000000..493842d --- /dev/null +++ b/lib/RS485/busdelay.h @@ -0,0 +1,37 @@ +#pragma once + +#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO + +#include +#include + +namespace drivers +{ + + class BusDelay + { + public: + BusDelay(uint32_t &lastAccess, const uint32_t minDelay, const char *title) : m_lastAccess(lastAccess) + { + const uint32_t now = millis(); + const uint32_t wait = now - lastAccess; + if (wait < minDelay) + { + LOG_WARN(title, "delay", wait); + delay(wait); + } + } + + BusDelay(BusDelay &) = delete; + BusDelay operator=(BusDelay &) = delete; + + ~BusDelay() + { + m_lastAccess = millis(); + } + + private: + uint32_t &m_lastAccess; + }; + +} \ No newline at end of file diff --git a/lib/SENECA/S50140_Driver.cpp b/lib/SENECA/S50140_Driver.cpp index 5152ef2..b4946ef 100644 --- a/lib/SENECA/S50140_Driver.cpp +++ b/lib/SENECA/S50140_Driver.cpp @@ -1,4 +1,7 @@ #include +#include + +#define BUS_DELAY drivers::BusDelay(m_lastRequest, c_minDelay, "S50140") namespace drivers { @@ -63,22 +66,11 @@ namespace drivers return readFloatReg(REG_WhPart); } - void S50140::delayRequest() - { - auto now = millis(); - if ((now - m_lastRequest) < c_minDelay) - { // minimum m_lastRequest between requests - LOG_DEBUG("S50140 delay request", (now-m_lastRequest)); - delay(now - m_lastRequest); - } - m_lastRequest = millis(); - } - const uint8_t S50140::getRegset() { std::vector value; std::lock_guard lock(m_bus.getMutex()); - delayRequest(); + BUS_DELAY; m_bus.readHoldingRegisters(m_address, REG_Regset, 2, value); if (value.empty()) return UINT8_MAX; @@ -89,7 +81,7 @@ namespace drivers { std::vector value; std::lock_guard lock(m_bus.getMutex()); - delayRequest(); + BUS_DELAY; m_bus.readHoldingRegisters(m_address, REG_PartCount, 2, value); if (value.empty()) return UINT16_MAX; @@ -107,18 +99,24 @@ namespace drivers while (retries++ < c_maxRetries) { bool ok(true); - delayRequest(); - LOG_WARN("Powermeter Counter STOP"); - ok &= m_bus.writeRegisters(m_address, REG_PartCount, {nullVal, stopAll}); - delayRequest(); - LOG_WARN("Powermeter Counter RESET"); - ok &= m_bus.writeRegisters(m_address, REG_PartCount, {nullVal, resetAll}); - delayRequest(); - LOG_WARN("Powermeter Counter START"); - ok &= m_bus.writeRegisters(m_address, REG_PartCount, {nullVal, startAll}); + { + LOG_WARN("Powermeter Counter STOP"); + BUS_DELAY; + ok &= m_bus.writeRegisters(m_address, REG_PartCount, {nullVal, stopAll}); + }; + { + LOG_WARN("Powermeter Counter RESET"); + BUS_DELAY; + ok &= m_bus.writeRegisters(m_address, REG_PartCount, {nullVal, resetAll}); + }; + { + LOG_WARN("Powermeter Counter START"); + BUS_DELAY; + ok &= m_bus.writeRegisters(m_address, REG_PartCount, {nullVal, startAll}); + }; if (ok) return; - LOG_ERROR("Unable to Reset Powermeter Partial Counters, device", m_address); + LOG_ERROR("Unable to Reset Powermeter Partial Counters, device", printHex(m_address).c_str()); } return; } @@ -127,10 +125,9 @@ namespace drivers { uint8_t retries(0); std::vector values; - while (retries++ < c_maxRetries) { - delayRequest(); + BUS_DELAY; if (m_bus.readHoldingRegisters(m_address, reg, c_dataWords, values) && values.size() == c_dataWords) { floatval_t fv; // potrebbe essere il contrario, vedremo @@ -138,9 +135,11 @@ namespace drivers fv.words.hi = values[1]; return fv.f; } - LOG_ERROR("Unable to Read Powermeter values, device", m_address); + LOG_ERROR("Unable to Read Powermeter values, device", printHex(m_address).c_str()); } return MAXFLOAT; } -} \ No newline at end of file +} + +#undef BUS_DELAY diff --git a/lib/SENECA/S50140_Driver.h b/lib/SENECA/S50140_Driver.h index 7d27770..659f108 100644 --- a/lib/SENECA/S50140_Driver.h +++ b/lib/SENECA/S50140_Driver.h @@ -4,6 +4,7 @@ #include #include +#include namespace drivers { @@ -13,7 +14,7 @@ namespace drivers private: const uint8_t c_maxRetries = 5; const uint8_t c_dataWords = 2; - const uint32_t c_minDelay = 500; + const uint32_t c_minDelay = 100; const uint16_t REG_V = 0x100C; const uint16_t REG_A = 0x1016; @@ -74,7 +75,6 @@ namespace drivers void resetPartialCounters(); private: - void delayRequest(); float_t readFloatReg(const uint16_t reg); private: diff --git a/lib/TEMP/R4DCB08_Driver.cpp b/lib/TEMP/R4DCB08_Driver.cpp index 6baca72..39eded7 100644 --- a/lib/TEMP/R4DCB08_Driver.cpp +++ b/lib/TEMP/R4DCB08_Driver.cpp @@ -1,4 +1,7 @@ #include +#include + +#define BUS_DELAY drivers::BusDelay(m_lastRequest, c_minDelay, "R4DCB08") namespace drivers { @@ -12,17 +15,6 @@ namespace drivers { } - void R4DCB08::delayRequest() - { - auto now = millis(); - if ((now - m_lastRequest) < c_minDelay) - { // minimum m_lastRequest between requests - LOG_DEBUG("R4DCB08 delay request", (now-m_lastRequest)); - delay(now - m_lastRequest); - } - m_lastRequest = millis(); - } - const float R4DCB08::getTemp(const uint8_t ch) { uint8_t retries(0); @@ -35,12 +27,12 @@ namespace drivers std::lock_guard lock(m_bus.getMutex()); while (retries++ < c_maxRetries) { - delayRequest(); + BUS_DELAY; if (m_bus.readHoldingRegisters(m_address, REG_TEMP + ch, 1, rawT) && !rawT.empty()) { return rawT.front() / 10.0f; } - LOG_ERROR("Failed to Read Temperature, device", m_address, "channel", ch); + LOG_ERROR("Failed to Read Temperature, device", printHex(m_address).c_str(), "channel", ch); rawT.clear(); } return MAXFLOAT; @@ -54,7 +46,7 @@ namespace drivers std::lock_guard lock(m_bus.getMutex()); while (retries++ < c_maxRetries) { - delayRequest(); + BUS_DELAY; if (m_bus.readHoldingRegisters(m_address, REG_TEMP, getNum(), rawT) && !rawT.empty()) { out.reserve(rawT.size()); @@ -64,7 +56,7 @@ namespace drivers } return out; } - LOG_ERROR("Failed to Read All Temperature, device", m_address); + LOG_ERROR("Failed to Read All Temperature, device", printHex(m_address).c_str()); rawT.clear(); } out.clear(); @@ -81,13 +73,13 @@ namespace drivers { while (retries++ < c_maxRetries) { - delayRequest(); + BUS_DELAY; if (m_bus.writeRegister(m_address, REG_TEMPCORR + channel, v * 10)) // convert to decimal degreees to register value { channel++; break; } - LOG_ERROR("Failed to Set Temperature Correction, device", m_address); + LOG_ERROR("Failed to Set Temperature Correction, device", printHex(m_address).c_str()); } } } @@ -102,7 +94,7 @@ namespace drivers std::lock_guard lock(m_bus.getMutex()); while (retries++ < c_maxRetries) { - delayRequest(); + BUS_DELAY; if (m_bus.readHoldingRegisters(m_address, REG_TEMPCORR, getNum(), rawV)) { out.reserve(rawV.size()); @@ -112,7 +104,7 @@ namespace drivers } return out; } - LOG_ERROR("Failed to Get Temperature Correction, device", m_address); + LOG_ERROR("Failed to Get Temperature Correction, device", printHex(m_address).c_str()); rawV.clear(); } out.clear(); @@ -129,7 +121,7 @@ namespace drivers std::lock_guard lock(m_bus.getMutex()); while (retries++ < c_maxRetries) { - delayRequest(); + BUS_DELAY; if (m_bus.readHoldingRegisters(m_address, REG_TEMP, T_MAX, rawT)) { for (auto v : rawT) @@ -140,9 +132,11 @@ namespace drivers m_sensors = sensors; return m_sensors; } - LOG_ERROR("Failed to Get Sensor Number, device", m_address); + LOG_ERROR("Failed to Get Sensor Number, device", printHex(m_address).c_str()); } - LOG_ERROR("No Temperature Sensors Detected, device", m_address); + LOG_ERROR("No Temperature Sensors Detected, device", printHex(m_address).c_str()); return 0; } } + +#undef BUS_DELAY diff --git a/lib/TEMP/R4DCB08_Driver.h b/lib/TEMP/R4DCB08_Driver.h index 401473c..3e9563e 100644 --- a/lib/TEMP/R4DCB08_Driver.h +++ b/lib/TEMP/R4DCB08_Driver.h @@ -4,6 +4,7 @@ #include #include +#include namespace drivers { diff --git a/lib/utils/utils.cpp b/lib/utils/utils.cpp index d296446..f5c22fc 100644 --- a/lib/utils/utils.cpp +++ b/lib/utils/utils.cpp @@ -1,6 +1,5 @@ #include "utils.h" - void printBytes(const char title[], const std::vector &b) { Serial0.flush(); @@ -40,7 +39,7 @@ void printBool(const char title[], const std::vector &vals) const std::string printBoolVec(const std::vector &vals) { std::string buf; - buf.reserve(vals.size()+1); + buf.reserve(vals.size() + 1); buf.append("b"); for (const auto v : vals) { @@ -48,3 +47,17 @@ const std::string printBoolVec(const std::vector &vals) } return buf; } + +const std::string printHex(const uint8_t val) +{ + std::string buf(5, '\0'); + sprintf(buf.data(), "0x%02x", val); + return buf; +} + +const std::string printHex(const uint16_t val) +{ + std::string buf(7, '\0'); + sprintf(buf.data(), "0x%04x", val); + return buf; +} \ No newline at end of file diff --git a/lib/utils/utils.h b/lib/utils/utils.h index 6e04830..2524c09 100644 --- a/lib/utils/utils.h +++ b/lib/utils/utils.h @@ -16,3 +16,7 @@ void printBytes(const char title[], const std::vector &b); void printBool(const char title[], const std::vector &vals); const std::string printBoolVec(const std::vector &vals); + +const std::string printHex(const uint8_t val); + +const std::string printHex(const uint16_t val); diff --git a/src/commands.cpp b/src/commands.cpp index 380a8fe..089dd25 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -32,11 +32,12 @@ namespace commands ArduinoJson::JsonDocument response; const auto pinfo = dev.seneca.getAll(); response["cmd"] = "getHPpower"; - auto values = response["params"].to(); + auto values = response["values"].to(); values["power"] = pinfo.pAct; values["current"] = pinfo.a; + values["voltage"] = pinfo.v; values["energy"] = pinfo.whPar; - LOG_INFO("getHPpower -> power", pinfo.pAct, "current", pinfo.a, "energy", pinfo.whPar); + LOG_INFO("getHPpower -> power", pinfo.pAct, "current", pinfo.a, "voltage", pinfo.v, "energy", pinfo.whPar); return response; } diff --git a/src/commands.h b/src/commands.h index 1a90542..83d62df 100644 --- a/src/commands.h +++ b/src/commands.h @@ -1,5 +1,7 @@ #pragma once +#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO + #include #include #include diff --git a/src/main.cpp b/src/main.cpp index 1b9f246..298f85c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,5 @@ +#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO + #include #include #include @@ -57,7 +59,7 @@ void loop() std::function commandsCallback = [&mqtt, &devices](const ArduinoJson::JsonDocument &doc) { - if (!doc["cmd"].is() || !doc["params"].is()) + if (!doc["cmd"].is()) { LOG_ERROR("Invalid Json Command"); return; @@ -123,19 +125,19 @@ void loop() while (true) { + const uint32_t start(millis()); const std::string timeStr(rtc.getTimeStr()); LOG_INFO("[", k++, "] Loop - Current Datetime", timeStr.c_str()); { ArduinoJson::JsonDocument poll; poll["cmd"] = "POLL"; - auto params = poll["params"].to(); + auto params = poll["values"].to(); params["time"] = timeStr; params["number"] = k; mqtt.publish(conf.m_mqttPublish["answers"], poll); }; - { ArduinoJson::JsonDocument ti; auto tempinfo = tmp.getTempAll(); @@ -144,17 +146,7 @@ void loop() ti["heating"] = tempinfo.at(0); mqtt.publish(conf.m_mqttPublish["temperatures"], ti); }; - - { - ArduinoJson::JsonDocument pi; - auto powerinfo = seneca.getAll(); - pi["power"] = powerinfo.pAct; - pi["current"] = powerinfo.a; - pi["energy"] = powerinfo.whPar; - pi["voltage"] = powerinfo.v; - mqtt.publish(conf.m_mqttPublish["heatpump"], pi); - } - + if (io.digitalInRead(0)) // ROSSO - Config Reset { LOG_WARN("Config RESET!"); @@ -162,7 +154,7 @@ void loop() delay(500); conf.resetConfig(); } - + if (io.digitalInRead(1)) // GIALLO - Restart { LOG_WARN("RESTART!"); @@ -171,9 +163,9 @@ void loop() esp_restart(); } - delay(conf.m_globalLoopDelay); // to avoid too fast loop + delay(conf.m_globalLoopDelay - (start - millis())); // to avoid too fast loop, keep precise timing computing loop time } - + //////////////////////////////////////// ///////// MAIN LOOP INSIDE LOOP //////// //////////////////////////////////////// diff --git a/src/remoteIO.cpp b/src/remoteIO.cpp index 66d69e8..b3a1607 100644 --- a/src/remoteIO.cpp +++ b/src/remoteIO.cpp @@ -1,4 +1,7 @@ #include +#include + +#define BUS_DELAY drivers::BusDelay(m_lastRequest, c_minDelay, "remoteIO") remoteIO::remoteIO(const uint8_t address, drivers::MODBUS &bus) : m_address(address), m_initialized(false), m_bus(bus) { @@ -21,23 +24,12 @@ remoteIO::~remoteIO() resetAll(false); } -void remoteIO::delayRequest() -{ - auto now = millis(); - if ((now - m_lastRequest) < c_minDelay) - { // minimum m_lastRequest between requests - LOG_DEBUG("remoteIO delay request", (now - m_lastRequest)); - delay(now - m_lastRequest); - } - m_lastRequest = millis(); -} - const bool remoteIO::setOut(const channel_t ch, const bool value) { if (!m_initialized) return false; std::lock_guard lock(m_bus.getMutex()); - delayRequest(); + BUS_DELAY; LOG_DEBUG("Write Channel", ch, "->", value ? "True" : "False"); return m_bus.writeCoil(m_address, REG_COILS + ch, value); } @@ -47,7 +39,7 @@ const bool remoteIO::toggleOut(const channel_t ch) if (!m_initialized) return false; std::lock_guard lock(m_bus.getMutex()); - delayRequest(); + BUS_DELAY; std::vector value; if (!m_bus.readCoils(m_address, REG_COILS + ch, 1, value)) return false; @@ -60,7 +52,7 @@ const bool remoteIO::setOutPort(const std::vector values) if (!m_initialized) return false; std::lock_guard lock(m_bus.getMutex()); - delayRequest(); + BUS_DELAY; LOG_DEBUG("Write Port", CH_MAX); return m_bus.writeCoils(m_address, REG_COILS, values); } @@ -70,7 +62,7 @@ const bool remoteIO::getOut(const channel_t ch, bool &value) if (!m_initialized) return false; std::lock_guard lock(m_bus.getMutex()); - delayRequest(); + BUS_DELAY; std::vector values; if (!m_bus.readCoils(m_address, REG_COILS + ch, 1, values)) return false; @@ -84,7 +76,7 @@ const bool remoteIO::getOutPort(std::vector &values) if (!m_initialized) return false; std::lock_guard lock(m_bus.getMutex()); - delayRequest(); + BUS_DELAY; LOG_DEBUG("Read Port", CH_MAX); return m_bus.readCoils(m_address, REG_COILS, CH_MAX, values); } @@ -94,7 +86,7 @@ const bool remoteIO::getIn(const channel_t input, bool &value) if (!m_initialized) return false; std::lock_guard lock(m_bus.getMutex()); - delayRequest(); + BUS_DELAY; std::vector values; if (!m_bus.readInputs(m_address, REG_INPUT + input, 1, values)) return false; @@ -108,7 +100,7 @@ const bool remoteIO::getInPort(std::vector &values) if (!m_initialized) return false; std::lock_guard lock(m_bus.getMutex()); - delayRequest(); + BUS_DELAY; LOG_DEBUG("Read Inputs", CH_MAX); return m_bus.readInputs(m_address, REG_INPUT, CH_MAX, values); } @@ -117,4 +109,6 @@ void remoteIO::resetAll(const bool value) { LOG_DEBUG("Reset All ->", value ? "True" : "False"); m_bus.writeCoil(m_address, REG_ALLCOILS, value); -} \ No newline at end of file +} + +#undef BUS_DELAY diff --git a/src/remoteIO.h b/src/remoteIO.h index d31ca4b..8cbfb04 100644 --- a/src/remoteIO.h +++ b/src/remoteIO.h @@ -4,6 +4,7 @@ #include #include +#include class remoteIO { @@ -44,9 +45,6 @@ public: void resetAll(const bool value); -private: - void delayRequest(); - private: bool m_initialized; drivers::MODBUS &m_bus;