variables name refactoring
This commit is contained in:
@@ -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++);
|
||||
|
||||
Reference in New Issue
Block a user