Fix lock on MODBUS

This commit is contained in:
Emanuele Trabattoni
2025-07-22 11:15:57 +02:00
parent 146a2b558b
commit 16bb029e93
5 changed files with 21 additions and 8 deletions

View File

@@ -71,7 +71,6 @@ namespace drivers
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;
std::lock_guard<std::mutex> lock(m_mutex);
LOG_DEBUG("Read coils: dev[", device, "], reg[", reg, "], num[", num, "]");
return readBinary(device, func, reg, num, coils);
}
@@ -80,7 +79,6 @@ 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;
std::lock_guard<std::mutex> lock(m_mutex);
LOG_DEBUG("Read multi inputs: dev[", device, "], reg[", reg, "], num[", num, "]");
return readBinary(device, func, reg, num, inputs);
}
@@ -89,7 +87,6 @@ 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;
std::lock_guard<std::mutex> lock(m_mutex);
LOG_DEBUG("Read multi holding registers: dev[", device, "], reg[", reg, "], num[", num, "]");
return readInteger(device, func, reg, num, values);
}
@@ -98,7 +95,6 @@ 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;
std::lock_guard<std::mutex> lock(m_mutex);
LOG_DEBUG("Read multi input registers: dev[", device, "], reg[", reg, "], num[", num, "]");
return readInteger(device, func, reg, num, values);
}
@@ -107,7 +103,6 @@ namespace drivers
const bool MODBUS::writeCoil(const uint8_t device, const uint16_t coil, const bool value)
{
constexpr uint8_t func = 0x05;
std::lock_guard<std::mutex> lock(m_mutex);
LOG_DEBUG("Write single coil: dev[", device, "], coil[", coil, "], value[", value ? "true" : "false", "]");
return writeBinary(device, func, coil, {value});
}
@@ -116,7 +111,6 @@ namespace drivers
const bool MODBUS::writeRegister(const uint8_t device, const uint16_t reg, const uint16_t value)
{
constexpr uint8_t func = 0x06;
std::lock_guard<std::mutex> lock(m_mutex);
LOG_DEBUG("Write single register: dev[", device, "], reg[", reg, "], value[", value, "]");
return writeInteger(device, func, reg, {value}, false);
}
@@ -125,7 +119,6 @@ namespace drivers
const bool MODBUS::writeCoils(const uint8_t device, const uint16_t coils, const std::vector<bool> &values)
{
constexpr uint8_t func = 0x0F;
std::lock_guard<std::mutex> lock(m_mutex);
LOG_DEBUG("Write multi coils: dev[", device, "], start[", coils, "], num[", values.size(), "]");
return writeBinary(device, func, coils, values);
}
@@ -134,7 +127,6 @@ 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;
std::lock_guard<std::mutex> lock(m_mutex);
LOG_DEBUG("Write multi registers: dev[", device, "], start[", reg, "], num[", values.size(), "]");
return writeInteger(device, func, reg, values, true);
}

View File

@@ -60,6 +60,10 @@ namespace drivers
MODBUS(const MODBUS &) = delete; // remove copy constructors
MODBUS &operator=(const MODBUS &) = delete;
// Get transaction lock
std::unique_lock<std::mutex> getLock() {
return std::unique_lock<std::mutex>(m_mutex);
}
// Func 0x01
const bool readCoils(const uint8_t device, const uint16_t reg, const uint16_t num, std::vector<bool> &coils);

View File

@@ -76,6 +76,7 @@ namespace drivers
{
std::vector<uint16_t> value;
delayRequest();
auto lock = m_bus.getLock();
m_bus.readHoldingRegisters(m_address, REG_Regset, 2, value);
if (value.empty())
return UINT8_MAX;
@@ -86,6 +87,7 @@ namespace drivers
{
std::vector<uint16_t> value;
delayRequest();
auto lock = m_bus.getLock();
m_bus.readHoldingRegisters(m_address, REG_PartCount, 2, value);
if (value.empty())
return UINT16_MAX;
@@ -103,6 +105,7 @@ namespace drivers
{
bool ok(true);
delayRequest();
auto lock = m_bus.getLock();
LOG_WARN("Powermeter Counter STOP");
ok &= m_bus.writeRegisters(m_address, REG_PartCount, {nullVal, stopAll});
delayRequest();
@@ -126,6 +129,7 @@ namespace drivers
while (retries++ < maxRetries)
{
delayRequest();
auto lock = m_bus.getLock();
if (m_bus.readHoldingRegisters(m_address, reg, dataWords, values) && values.size() == dataWords)
{
floatval_t fv; // potrebbe essere il contrario, vedremo

View File

@@ -22,6 +22,7 @@ namespace drivers
}
while (retries++ < maxRetries)
{
auto lock = m_bus.getLock();
if (m_bus.readHoldingRegisters(m_address, REG_TEMP + ch, 1, rawT) && !rawT.empty())
{
return rawT.front() / 10.0f;
@@ -40,6 +41,7 @@ namespace drivers
std::vector<float> out;
while (retries++ < maxRetries)
{
auto lock = m_bus.getLock();
if (m_bus.readHoldingRegisters(m_address, REG_TEMP, getNum(), rawT) && !rawT.empty())
{
out.reserve(rawT.size());
@@ -67,6 +69,7 @@ namespace drivers
{ // convert to decimal degreees to register value
while (retries++ < maxRetries)
{
auto lock = m_bus.getLock();
if (m_bus.writeRegister(m_address, REG_TEMPCORR + channel, v*10))
{
channel++;
@@ -88,6 +91,7 @@ namespace drivers
while (retries++ < maxRetries)
{
auto lock = m_bus.getLock();
if (m_bus.readHoldingRegisters(m_address, REG_TEMPCORR, getNum(), rawV))
{
out.reserve(rawV.size());
@@ -114,6 +118,7 @@ namespace drivers
std::vector<uint16_t> rawT;
while (retries++ < maxRetries)
{
auto lock = m_bus.getLock();
if (m_bus.readHoldingRegisters(m_address, REG_TEMP, T_MAX, rawT))
{
for (auto v : rawT)

View File

@@ -4,6 +4,7 @@ remoteIO::remoteIO(const uint8_t address, drivers::MODBUS &bus) : m_address(addr
{
LOG_INFO("Initializing relay module");
std::vector<uint16_t> response;
auto lock = m_bus.getLock();
if (!m_bus.readHoldingRegisters(m_address, REG_VERSION, 1, response))
{
LOG_ERROR("Unable to inizialize relay module");
@@ -21,6 +22,7 @@ remoteIO::~remoteIO()
const bool remoteIO::setOut(const channel_t ch, const bool value)
{
auto lock = m_bus.getLock();
if (!m_initialized)
return false;
LOG_DEBUG("Write Channel", ch, "->", value ? "True" : "False");
@@ -29,6 +31,7 @@ const bool remoteIO::setOut(const channel_t ch, const bool value)
const bool remoteIO::toggleOut(const channel_t ch)
{
auto lock = m_bus.getLock();
if (!m_initialized)
return false;
std::vector<bool> value;
@@ -40,6 +43,7 @@ const bool remoteIO::toggleOut(const channel_t ch)
const bool remoteIO::setOutPort(const std::vector<bool> values)
{
auto lock = m_bus.getLock();
if (!m_initialized)
return false;
LOG_DEBUG("Write Port", CH_MAX);
@@ -48,6 +52,7 @@ const bool remoteIO::setOutPort(const std::vector<bool> values)
const bool remoteIO::getOut(const channel_t ch, bool &value)
{
auto lock = m_bus.getLock();
if (!m_initialized)
return false;
std::vector<bool> values;
@@ -60,6 +65,7 @@ const bool remoteIO::getOut(const channel_t ch, bool &value)
const bool remoteIO::getOutPort(std::vector<bool> &values)
{
auto lock = m_bus.getLock();
if (!m_initialized)
return false;
LOG_DEBUG("Read Port", CH_MAX);
@@ -68,6 +74,7 @@ const bool remoteIO::getOutPort(std::vector<bool> &values)
const bool remoteIO::getIn(const channel_t input, bool &value)
{
auto lock = m_bus.getLock();
if (!m_initialized)
return false;
std::vector<bool> values;
@@ -80,6 +87,7 @@ const bool remoteIO::getIn(const channel_t input, bool &value)
const bool remoteIO::getInPort(std::vector<bool> &values)
{
auto lock = m_bus.getLock();
if (!m_initialized)
return false;
LOG_DEBUG("Read Inputs", CH_MAX);