variables name refactoring

This commit is contained in:
Emanuele Trabattoni
2025-07-23 22:39:40 +02:00
parent 8f5615a034
commit 59d8c2c2d4
12 changed files with 98 additions and 100 deletions

View File

@@ -65,68 +65,89 @@ namespace drivers
readAll(garbage);
LOG_INFO("Init MODBUS Master Mode");
m_crc.reset(CRC16_MODBUS_POLYNOME, CRC16_MODBUS_INITIAL, CRC16_MODBUS_XOR_OUT, CRC16_MODBUS_REV_IN, CRC16_MAXIM_REV_OUT);
m_lastAccess = millis();
}
void MODBUS::delayAccess(const uint8_t device)
{
if (device == m_lastDevice) return;
auto now = millis();
if ((now - m_lastAccess) < c_minDelay) // fixed 10 milliseconds delay between commands
{ // minimum m_lastRequest between requests
delay(now - m_lastAccess);
}
m_lastAccess = now;
m_lastDevice = device;
}
// 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, "]");
return readBinary(device, func, reg, num, coils);
}
// Func 0x02
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, "]");
return readBinary(device, func, reg, num, inputs);
}
// Func 0x03
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, "]");
return readInteger(device, func, reg, num, values);
}
// Func 0x04
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, "]");
return readInteger(device, func, reg, num, values);
}
// Func 0x05
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", "]");
return writeBinary(device, func, coil, {value});
}
// Func 0x06
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, "]");
return writeInteger(device, func, reg, {value}, false);
}
// Func 0x0F
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(), "]");
return writeBinary(device, func, coils, values);
}
// Func 0x10
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(), "]");
return writeInteger(device, func, reg, values, true);
}
@@ -143,7 +164,7 @@ namespace drivers
return false;
}
const uint16_t nRespDataBytes = (uint16_t)ceil(bits / 8.0f); // 1 bit for every coil, if not 8 mutiple padded with zeroes
const uint16_t expectedRespLen = (RESP_HEADER_SIZE + RESP_CRC_SIZE) + nRespDataBytes; // device + function + nbytes + data[] + crc(16b)
const uint16_t expectedRespLen = (c_respHeaderSize + c_respCrcSize) + nRespDataBytes; // device + function + nbytes + data[] + crc(16b)
std::vector<uint8_t> response;
if (!readN(expectedRespLen, response))
{
@@ -172,7 +193,7 @@ namespace drivers
uint16_t bitNum(0);
// get response data bytes excluding header and crc
const std::vector<uint8_t> respData(response.begin() + RESP_HEADER_SIZE, response.end() - sizeof(crc_t));
const std::vector<uint8_t> respData(response.begin() + c_respHeaderSize, response.end() - sizeof(crc_t));
for (auto it = respData.begin(); it < respData.end(); it++)
{
for (uint8_t j(0); j < 8 && bitNum < bits; j++)
@@ -193,7 +214,7 @@ namespace drivers
return false;
}
const uint16_t nRespDataBytes = num * sizeof(uint16_t);
const uint16_t expectedRespLen = (RESP_HEADER_SIZE + sizeof(crc_t)) + nRespDataBytes; // device + function + nbytes + data[] + crc(16b)
const uint16_t expectedRespLen = (c_respHeaderSize + sizeof(crc_t)) + nRespDataBytes; // device + function + nbytes + data[] + crc(16b)
std::vector<uint8_t> response;
if (!readN(expectedRespLen, response))
{
@@ -220,7 +241,7 @@ namespace drivers
out.clear();
out.reserve(nRespDataBytes / sizeof(uint16_t));
// get response data bytes excluding header and crc
const std::vector<uint8_t> respData(response.begin() + RESP_HEADER_SIZE, response.end() - RESP_CRC_SIZE);
const std::vector<uint8_t> respData(response.begin() + c_respHeaderSize, response.end() - c_respCrcSize);
for (auto it = respData.begin(); it < respData.end(); it++)
{
const uint8_t lo(*it++);

View File

@@ -13,7 +13,7 @@ namespace drivers
{
class RS485
{
static const uint8_t PORT = 1;
const uint8_t c_port = 1;
public:
RS485(const uint32_t baud, const SerialConfig conf);
@@ -32,8 +32,9 @@ namespace drivers
class MODBUS : private RS485
{
static const uint8_t RESP_HEADER_SIZE = 3;
static const uint8_t RESP_CRC_SIZE = 2;
const uint8_t c_respHeaderSize = 3;
const uint8_t c_respCrcSize = 2;
const uint16_t c_minDelay = 50;
typedef struct
{
@@ -92,6 +93,9 @@ namespace drivers
private:
CRC16 m_crc;
std::mutex m_mutex;
uint8_t m_lastDevice;
uint32_t m_lastAccess;
void delayAccess(const uint8_t device);
const std::vector<uint8_t> singleRequest(const uint8_t device, const uint8_t func, const uint16_t reg, const uint16_t data);
const std::vector<uint8_t> multiRequest(const uint8_t device, const uint8_t func, const uint16_t reg, const uint16_t qty, const std::vector<uint8_t> &data);
const bool readBinary(const uint8_t device, const uint8_t func, const uint16_t reg, const uint16_t bits, std::vector<bool> &out);