Added seneca powermeter driver
This commit is contained in:
114
lib/SENECA/S50140_Driver.cpp
Normal file
114
lib/SENECA/S50140_Driver.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
#include <S50140_Driver.h>
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
S50140::S50140(drivers::MODBUS &bus, const uint8_t address) : m_bus(bus), m_address(address)
|
||||
{
|
||||
}
|
||||
S50140::~S50140()
|
||||
{
|
||||
}
|
||||
|
||||
const S50140::powerinfo_t S50140::getAll()
|
||||
{
|
||||
powerinfo_t info;
|
||||
info.v = getV();
|
||||
info.a = getA();
|
||||
info.pAct = getPact();
|
||||
info.pApp = getPapp();
|
||||
info.pRea = getPrea();
|
||||
info.f = getF();
|
||||
info.whTot = getWhTot();
|
||||
info.whPar = getWhPar();
|
||||
return info;
|
||||
}
|
||||
|
||||
const float_t S50140::getV()
|
||||
{
|
||||
return readFloatReg(REG_V);
|
||||
}
|
||||
const float_t S50140::getA()
|
||||
{
|
||||
return readFloatReg(REG_A);
|
||||
}
|
||||
const float_t S50140::getPact()
|
||||
{
|
||||
return readFloatReg(REG_Pact);
|
||||
}
|
||||
const float_t S50140::getPapp()
|
||||
{
|
||||
return readFloatReg(REG_Papp);
|
||||
}
|
||||
const float_t S50140::getPrea()
|
||||
{
|
||||
return readFloatReg(REG_Prea);
|
||||
}
|
||||
const float_t S50140::getF()
|
||||
{
|
||||
return readFloatReg(REG_Freq);
|
||||
}
|
||||
const float_t S50140::getWhTot()
|
||||
{
|
||||
return readFloatReg(REG_WhTot);
|
||||
}
|
||||
const float_t S50140::getWhPar()
|
||||
{
|
||||
return readFloatReg(REG_WhPart);
|
||||
}
|
||||
|
||||
void S50140::resetTotalCounters()
|
||||
{
|
||||
uint8_t retries(0);
|
||||
const uint16_t resetAll = 0x0000;
|
||||
while (retries++ < maxRetries)
|
||||
{
|
||||
if (m_bus.writeRegister(m_address, REG_TotCount, resetAll))
|
||||
return;
|
||||
LOG_ERROR("Unable to Reset Powermeter Total Counters, device", m_address);
|
||||
delay(10);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void S50140::resetPartialCounters()
|
||||
{
|
||||
uint8_t retries(0);
|
||||
const uint16_t resetAll = 0x0A03;
|
||||
const uint16_t stopAll = 0x0A02;
|
||||
const uint16_t startAll = 0x0A01;
|
||||
while (retries++ < maxRetries)
|
||||
{
|
||||
bool ok(true);
|
||||
ok &= m_bus.writeRegister(m_address, REG_TotCount, stopAll);
|
||||
delay(10);
|
||||
ok &= m_bus.writeRegister(m_address, REG_TotCount, resetAll);
|
||||
delay(10);
|
||||
ok &= m_bus.writeRegister(m_address, REG_TotCount, startAll);
|
||||
if (ok)
|
||||
return;
|
||||
LOG_ERROR("Unable to Reset Powermeter Partial Counters, device", m_address);
|
||||
delay(10);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
float_t S50140::readFloatReg(const uint16_t reg)
|
||||
{
|
||||
uint8_t retries(0);
|
||||
std::vector<uint16_t> values;
|
||||
|
||||
while (retries++ < maxRetries)
|
||||
{
|
||||
if (m_bus.readHoldingRegisters(m_address, reg, dataWords, values) && values.size() == dataWords)
|
||||
{
|
||||
floatval_t fv; // potrebbe essere il contrario, vedremo
|
||||
fv.hi = values[0]; // magari va invertita ancora l'endianness
|
||||
fv.lo = values[1];
|
||||
return fv.f;
|
||||
}
|
||||
LOG_ERROR("Unable to Read Powermeter values, device", m_address);
|
||||
}
|
||||
return MAXFLOAT;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user