improved bus wait with raii class that updates last access
This commit is contained in:
@@ -3,8 +3,11 @@
|
||||
#include <cstring>
|
||||
#include <endian.h>
|
||||
|
||||
#include <busdelay.h>
|
||||
#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<bool> &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<bool> &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<uint16_t> &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<uint16_t> &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<bool> &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<uint16_t> &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<bool> &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<uint16_t> &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<bool> &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<uint8_t> 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<uint16_t> &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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#undef BUS_DELAY
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
37
lib/RS485/busdelay.h
Normal file
37
lib/RS485/busdelay.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
#include <S50140_Driver.h>
|
||||
#include <busdelay.h>
|
||||
|
||||
#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<uint16_t> value;
|
||||
std::lock_guard<std::mutex> 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<uint16_t> value;
|
||||
std::lock_guard<std::mutex> 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<uint16_t> 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#undef BUS_DELAY
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <RS485_Driver.h>
|
||||
#include <utils.h>
|
||||
|
||||
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:
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
#include <R4DCB08_Driver.h>
|
||||
#include <busdelay.h>
|
||||
|
||||
#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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <RS485_Driver.h>
|
||||
#include <utils.h>
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
void printBytes(const char title[], const std::vector<uint8_t> &b)
|
||||
{
|
||||
Serial0.flush();
|
||||
@@ -40,7 +39,7 @@ void printBool(const char title[], const std::vector<bool> &vals)
|
||||
const std::string printBoolVec(const std::vector<bool> &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<bool> &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;
|
||||
}
|
||||
@@ -16,3 +16,7 @@ void printBytes(const char title[], const std::vector<uint16_t> &b);
|
||||
void printBool(const char title[], const std::vector<bool> &vals);
|
||||
|
||||
const std::string printBoolVec(const std::vector<bool> &vals);
|
||||
|
||||
const std::string printHex(const uint8_t val);
|
||||
|
||||
const std::string printHex(const uint16_t val);
|
||||
|
||||
Reference in New Issue
Block a user