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

@@ -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);