Adjusted mutex lock and delay for modbus
This commit is contained in:
@@ -5,12 +5,24 @@ namespace drivers
|
||||
R4DCB08::R4DCB08(drivers::MODBUS &bus, const uint8_t address) : m_address(address), m_bus(bus), m_sensors(0)
|
||||
{
|
||||
m_sensors = getNum();
|
||||
m_lastRequest = millis();
|
||||
}
|
||||
|
||||
R4DCB08::~R4DCB08()
|
||||
{
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -20,16 +32,16 @@ namespace drivers
|
||||
LOG_ERROR("Invalid Temperature Channel number", ch);
|
||||
return MAXFLOAT;
|
||||
}
|
||||
auto lock = m_bus.getLock();
|
||||
while (retries++ < maxRetries)
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
delayRequest();
|
||||
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);
|
||||
rawT.clear();
|
||||
delay(50);
|
||||
}
|
||||
return MAXFLOAT;
|
||||
}
|
||||
@@ -39,9 +51,10 @@ namespace drivers
|
||||
uint8_t retries(0);
|
||||
std::vector<uint16_t> rawT;
|
||||
std::vector<float> out;
|
||||
auto lock = m_bus.getLock();
|
||||
while (retries++ < maxRetries)
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
delayRequest();
|
||||
if (m_bus.readHoldingRegisters(m_address, REG_TEMP, getNum(), rawT) && !rawT.empty())
|
||||
{
|
||||
out.reserve(rawT.size());
|
||||
@@ -53,7 +66,6 @@ namespace drivers
|
||||
}
|
||||
LOG_ERROR("Failed to Read All Temperature, device", m_address);
|
||||
rawT.clear();
|
||||
delay(50);
|
||||
}
|
||||
out.clear();
|
||||
return out;
|
||||
@@ -64,20 +76,18 @@ namespace drivers
|
||||
uint8_t retries(0);
|
||||
uint8_t channel(0);
|
||||
corr.resize(getNum()); // max number of temperature correction values is equal to number of sensors
|
||||
|
||||
auto lock = m_bus.getLock();
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
for (auto v : corr)
|
||||
{
|
||||
while (retries++ < maxRetries)
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
delayRequest();
|
||||
if (m_bus.writeRegister(m_address, REG_TEMPCORR + channel, v * 10)) // convert to decimal degreees to register value
|
||||
{
|
||||
channel++;
|
||||
delay(50);
|
||||
break;
|
||||
}
|
||||
LOG_ERROR("Failed to Set Temperature Correction, device", m_address);
|
||||
delay(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,9 +99,10 @@ namespace drivers
|
||||
std::vector<float> out;
|
||||
rawV.reserve(getNum());
|
||||
|
||||
while (retries++ < maxRetries)
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
auto lock = m_bus.getLock();
|
||||
delayRequest();
|
||||
if (m_bus.readHoldingRegisters(m_address, REG_TEMPCORR, getNum(), rawV))
|
||||
{
|
||||
out.reserve(rawV.size());
|
||||
@@ -103,7 +114,6 @@ namespace drivers
|
||||
}
|
||||
LOG_ERROR("Failed to Get Temperature Correction, device", m_address);
|
||||
rawV.clear();
|
||||
delay(50);
|
||||
}
|
||||
out.clear();
|
||||
return out;
|
||||
@@ -116,9 +126,10 @@ namespace drivers
|
||||
uint8_t retries(0);
|
||||
uint8_t sensors(0);
|
||||
std::vector<uint16_t> rawT;
|
||||
while (retries++ < maxRetries)
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
auto lock = m_bus.getLock();
|
||||
delayRequest();
|
||||
if (m_bus.readHoldingRegisters(m_address, REG_TEMP, T_MAX, rawT))
|
||||
{
|
||||
for (auto v : rawT)
|
||||
@@ -130,7 +141,6 @@ namespace drivers
|
||||
return m_sensors;
|
||||
}
|
||||
LOG_ERROR("Failed to Get Sensor Number, device", m_address);
|
||||
delay(50);
|
||||
}
|
||||
LOG_ERROR("No Temperature Sensors Detected, device", m_address);
|
||||
return 0;
|
||||
|
||||
@@ -25,7 +25,9 @@ namespace drivers
|
||||
T_MAX
|
||||
};
|
||||
|
||||
const uint8_t maxRetries = 5;
|
||||
private:
|
||||
const uint8_t c_maxRetries = 5;
|
||||
const uint32_t c_minDelay = 500;
|
||||
const uint16_t REG_TEMP = 0x0000;
|
||||
const uint16_t REG_TEMPCORR = 0x0008;
|
||||
|
||||
@@ -41,9 +43,13 @@ namespace drivers
|
||||
|
||||
const uint8_t getNum();
|
||||
|
||||
private:
|
||||
void delayRequest();
|
||||
|
||||
private:
|
||||
const uint8_t m_address;
|
||||
uint8_t m_sensors;
|
||||
MODBUS &m_bus;
|
||||
uint32_t m_lastRequest;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user