Fixed MODBUS and seneca drivers, added partial counter reset
This commit is contained in:
@@ -12,7 +12,7 @@ namespace drivers
|
||||
|
||||
const S50140::powerinfo_t S50140::getAll()
|
||||
{
|
||||
powerinfo_t info;
|
||||
powerinfo_t info{MAXFLOAT};
|
||||
info.v = getV();
|
||||
info.a = getA();
|
||||
info.pAct = getPact();
|
||||
@@ -57,19 +57,36 @@ namespace drivers
|
||||
return readFloatReg(REG_WhPart);
|
||||
}
|
||||
|
||||
void S50140::resetTotalCounters()
|
||||
void S50140::delayRequest()
|
||||
{
|
||||
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);
|
||||
auto now = millis();
|
||||
if ((now - m_lastRequest) < minDelay)
|
||||
{ // minimum 500ms between requests
|
||||
delay(now - m_lastRequest);
|
||||
}
|
||||
return;
|
||||
m_lastRequest = now;
|
||||
}
|
||||
|
||||
const uint8_t S50140::getRegset()
|
||||
{
|
||||
std::vector<uint16_t> value;
|
||||
delayRequest();
|
||||
m_bus.readHoldingRegisters(m_address, REG_Regset, 2, value);
|
||||
if (value.empty())
|
||||
return UINT8_MAX;
|
||||
return value.front() + value.back();
|
||||
}
|
||||
|
||||
const uint16_t S50140::getCounterStatus()
|
||||
{
|
||||
std::vector<uint16_t> value;
|
||||
delayRequest();
|
||||
m_bus.readHoldingRegisters(m_address, REG_PartCount, 2, value);
|
||||
if (value.empty())
|
||||
return UINT16_MAX;
|
||||
return value.front() + value.back();
|
||||
}
|
||||
|
||||
void S50140::resetPartialCounters()
|
||||
{
|
||||
uint8_t retries(0);
|
||||
@@ -79,15 +96,18 @@ namespace drivers
|
||||
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);
|
||||
delayRequest();
|
||||
LOG_WARN("Powermeter Counter STOP");
|
||||
ok &= m_bus.writeRegisters(m_address, REG_PartCount, {0x0000, stopAll});
|
||||
delayRequest();
|
||||
LOG_WARN("Powermeter Counter RESET");
|
||||
ok &= m_bus.writeRegisters(m_address, REG_PartCount, {0x0000, resetAll});
|
||||
delayRequest();
|
||||
LOG_WARN("Powermeter Counter START");
|
||||
ok &= m_bus.writeRegisters(m_address, REG_PartCount, {0x0000, startAll});
|
||||
if (ok)
|
||||
return;
|
||||
LOG_ERROR("Unable to Reset Powermeter Partial Counters, device", m_address);
|
||||
delay(10);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -99,11 +119,12 @@ namespace drivers
|
||||
|
||||
while (retries++ < maxRetries)
|
||||
{
|
||||
delayRequest();
|
||||
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];
|
||||
floatval_t fv; // potrebbe essere il contrario, vedremo
|
||||
fv.words.lo = values[0]; // magari va invertita ancora l'endianness
|
||||
fv.words.hi = values[1];
|
||||
return fv.f;
|
||||
}
|
||||
LOG_ERROR("Unable to Read Powermeter values, device", m_address);
|
||||
|
||||
@@ -11,24 +11,28 @@ namespace drivers
|
||||
private:
|
||||
const uint8_t maxRetries = 5;
|
||||
const uint8_t dataWords = 2;
|
||||
const uint16_t minDelay = 500;
|
||||
|
||||
const uint16_t REG_V = 0x100C;
|
||||
const uint16_t REG_A = 0x1016;
|
||||
const uint16_t REG_Pact = 0x1026;
|
||||
const uint16_t REG_Papp = 0x102E;
|
||||
const uint16_t REG_Prea = 0x1036;
|
||||
const uint16_t REG_Freq = 0x1036;
|
||||
const uint16_t REG_Freq = 0x1038;
|
||||
const uint16_t REG_WhTot = 0x1106;
|
||||
const uint16_t REG_WhPart = 0x1400;
|
||||
const uint16_t REG_Serial = 0x0500;
|
||||
const uint16_t REG_Regset = 0x0538;
|
||||
const uint16_t REG_TotCount = 0x0516;
|
||||
const uint16_t REG_PartCount = 0x0517;
|
||||
const uint16_t REG_PartCount = 0x0526;
|
||||
|
||||
typedef union
|
||||
{
|
||||
float_t f;
|
||||
uint16_t hi;
|
||||
uint16_t lo;
|
||||
struct
|
||||
{
|
||||
uint16_t hi;
|
||||
uint16_t lo;
|
||||
} words;
|
||||
} floatval_t;
|
||||
|
||||
public:
|
||||
@@ -59,14 +63,18 @@ namespace drivers
|
||||
const float_t getWhTot();
|
||||
const float_t getWhPar();
|
||||
|
||||
void resetTotalCounters();
|
||||
const uint8_t getRegset();
|
||||
const uint16_t getCounterStatus();
|
||||
|
||||
void resetPartialCounters();
|
||||
|
||||
private:
|
||||
void delayRequest();
|
||||
float_t readFloatReg(const uint16_t reg);
|
||||
|
||||
private:
|
||||
const uint8_t m_address;
|
||||
drivers::MODBUS &m_bus;
|
||||
uint64_t m_lastRequest = 0;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user