Added debug config
This commit is contained in:
@@ -1,14 +1,7 @@
|
||||
#include "RS485_driver.h"
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <machine/endian.h>
|
||||
#ifdef ESP32
|
||||
#include <endian.h>
|
||||
#else
|
||||
#define be16toh(x) __bswap16(x)
|
||||
#define htobe16(x) __bswap16(x)
|
||||
#define htole16(x) x
|
||||
#endif
|
||||
|
||||
uint8_t data[][8] = {
|
||||
// ESP32-S3-POE-ETH-8DI-8RO Control Command (RS485 receiving data)
|
||||
@@ -37,7 +30,7 @@ uint8_t Send_Data[][8] = {
|
||||
{0x01, 0x05, 0x00, 0xFF, 0x00, 0x00, 0xFD, 0xFA}, // Modbus RTU Relay ALL OFF
|
||||
};
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE
|
||||
// #define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE
|
||||
void printBytes(const std::vector<uint8_t> &b)
|
||||
{
|
||||
Serial0.flush();
|
||||
@@ -56,50 +49,41 @@ namespace drivers
|
||||
//////////// RS485 ////////////
|
||||
////////////////////////////////
|
||||
|
||||
#ifdef ESP32
|
||||
RS485::RS485(const uint32_t baud, const SerialConfig conf)
|
||||
RS485::RS485(const uint32_t baud, const SerialConfig conf): m_serial(Serial1)
|
||||
{
|
||||
LOG_INFO("Init serial port 1");
|
||||
// m_serial = std::make_unique<HardwareSerial>(1); // RS485 is hardwired to serial port 1
|
||||
Serial1.begin(baud, conf, 18, 17);
|
||||
Serial1.flush();
|
||||
// RS485 is hardwired to serial port 1
|
||||
m_serial.begin(baud, conf, 18, 17);
|
||||
m_serial.flush();
|
||||
}
|
||||
#else
|
||||
|
||||
RS485::RS485(const uint32_t baud)
|
||||
{
|
||||
LOG_INFO("Init serial port 1");
|
||||
m_serial = std::make_unique<HardwareSerial>(PORT); // RS485 is hardwired to serial port 1
|
||||
m_serial->begin(baud);
|
||||
}
|
||||
#endif
|
||||
const bool RS485::write(const std::vector<uint8_t> data)
|
||||
{
|
||||
return data.size() == Serial1.write(data.data(), data.size());
|
||||
return data.size() == m_serial.write(data.data(), data.size());
|
||||
}
|
||||
|
||||
const bool RS485::readAll(std::vector<uint8_t> &data)
|
||||
{
|
||||
const uint32_t avail(Serial1.available());
|
||||
const uint32_t avail(m_serial.available());
|
||||
if (avail == 0)
|
||||
return true;
|
||||
data.resize(avail);
|
||||
return data.size() == m_serial->readBytes(data.data(), avail);
|
||||
return data.size() == m_serial.readBytes(data.data(), avail);
|
||||
}
|
||||
|
||||
const bool RS485::readN(const uint16_t nBytes, std::vector<uint8_t> &data)
|
||||
{
|
||||
data.resize(nBytes);
|
||||
if (Serial1.readBytes(data.data(), nBytes) == nBytes)
|
||||
if (m_serial.readBytes(data.data(), nBytes) == nBytes)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool RS485::readUntil(const uint8_t ch, std::vector<uint8_t> &data)
|
||||
{
|
||||
const uint32_t avail(Serial1.available());
|
||||
const uint32_t avail(m_serial.available());
|
||||
data.resize(avail);
|
||||
Serial1.readBytesUntil(ch, data.data(), avail);
|
||||
m_serial.readBytesUntil(ch, data.data(), avail);
|
||||
data.shrink_to_fit();
|
||||
return true;
|
||||
}
|
||||
@@ -107,7 +91,6 @@ namespace drivers
|
||||
////////////////////////////////
|
||||
//////////// MODBUS ////////////
|
||||
////////////////////////////////
|
||||
#ifdef ESP32
|
||||
MODBUS::MODBUS(const uint32_t baud, const SerialConfig conf) : RS485::RS485(baud, conf)
|
||||
{
|
||||
std::vector<uint8_t> garbage;
|
||||
@@ -115,12 +98,7 @@ namespace drivers
|
||||
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);
|
||||
}
|
||||
#else
|
||||
MODBUS::MODBUS(const uint32_t baud) : RS485::RS485(baud)
|
||||
{
|
||||
LOG_INFO("Init MODBUS Master Mode");
|
||||
}
|
||||
#endif
|
||||
|
||||
// Func 0x01
|
||||
const bool MODBUS::readCoils(const uint8_t device, const uint16_t reg, const uint16_t num, std::vector<bool> &coils)
|
||||
{
|
||||
@@ -157,7 +135,7 @@ namespace drivers
|
||||
const bool MODBUS::writeCoil(const uint8_t device, const uint16_t coil, const bool value)
|
||||
{
|
||||
constexpr uint8_t func = 0x05;
|
||||
LOG_DEBUG("Write single coils: dev[", device, "], coil[", coil, "], value[", value ? "true" : "false", "]");
|
||||
LOG_DEBUG("Write single coil: dev[", device, "], coil[", coil, "], value[", value ? "true" : "false", "]");
|
||||
return writeBinary(device, func, coil, 1, {value});
|
||||
}
|
||||
|
||||
@@ -181,7 +159,7 @@ 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;
|
||||
LOG_DEBUG("Write multi coils: dev[", device, "], start[", reg, "], num[", values.size(), "]");
|
||||
LOG_DEBUG("Write multi registers: dev[", device, "], start[", reg, "], num[", values.size(), "]");
|
||||
return writeInteger(device, func, reg, values.size(), values);
|
||||
}
|
||||
|
||||
@@ -196,7 +174,7 @@ namespace drivers
|
||||
LOG_ERROR("Failed send readBinary command");
|
||||
return false;
|
||||
}
|
||||
const uint16_t nRespDataBytes = 1 + (uint16_t)(bits / 8); // 1 bit for every coil, if not 8 mutiple padded with zeroes
|
||||
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)
|
||||
std::vector<uint8_t> response;
|
||||
if (!readN(expectedRespLen, response))
|
||||
@@ -228,10 +206,9 @@ namespace drivers
|
||||
const std::vector<uint8_t> respData(response.begin() + RESP_HEADER_SIZE, response.end() - sizeof(crc_t));
|
||||
for (auto it = respData.begin(); it < respData.end(); it++)
|
||||
{
|
||||
const auto v = *it;
|
||||
for (uint8_t j(0); j < 8 && bitNum < bits; j++)
|
||||
{
|
||||
const auto cv((0x01 << j) && v);
|
||||
const bool cv((0x01 << j) & *it);
|
||||
out.push_back(cv);
|
||||
bitNum++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user