fix: make SDM power meter use serial port manager

instead of hard-coding the use of hardware UART 2, the SDM power meter
instance now asks for a free hardware serial port to use and
instanciates the respective HardwareSerial object using said port.
This commit is contained in:
Bernhard Kirchen 2024-06-01 17:22:04 +02:00 committed by Bernhard Kirchen
parent be41e6b906
commit 5a007e5352
2 changed files with 11 additions and 2 deletions

View File

@ -60,6 +60,8 @@ private:
mutable std::mutex _mutex; mutable std::mutex _mutex;
static char constexpr _sdmSerialPortOwner[] = "SDM power meter";
std::unique_ptr<HardwareSerial> _upSdmSerial = nullptr;
std::unique_ptr<SDM> _upSdm = nullptr; std::unique_ptr<SDM> _upSdm = nullptr;
std::unique_ptr<SoftwareSerial> _upSmlSerial = nullptr; std::unique_ptr<SoftwareSerial> _upSmlSerial = nullptr;

View File

@ -9,6 +9,7 @@
#include "MqttSettings.h" #include "MqttSettings.h"
#include "NetworkSettings.h" #include "NetworkSettings.h"
#include "MessageOutput.h" #include "MessageOutput.h"
#include "SerialPortManager.h"
#include <ctime> #include <ctime>
#include <SMA_HM.h> #include <SMA_HM.h>
@ -57,16 +58,22 @@ void PowerMeterClass::init(Scheduler& scheduler)
} }
case Source::SDM1PH: case Source::SDM1PH:
case Source::SDM3PH: case Source::SDM3PH: {
if (pin.powermeter_rx < 0 || pin.powermeter_tx < 0) { if (pin.powermeter_rx < 0 || pin.powermeter_tx < 0) {
MessageOutput.println("[PowerMeter] invalid pin config for SDM power meter (RX and TX pins must be defined)"); MessageOutput.println("[PowerMeter] invalid pin config for SDM power meter (RX and TX pins must be defined)");
return; return;
} }
_upSdm = std::make_unique<SDM>(Serial2, 9600, pin.powermeter_dere, auto oHwSerialPort = SerialPortManager.allocatePort(_sdmSerialPortOwner);
if (!oHwSerialPort) { return; }
_upSdmSerial = std::make_unique<HardwareSerial>(*oHwSerialPort);
_upSdmSerial->end(); // make sure the UART will be re-initialized
_upSdm = std::make_unique<SDM>(*_upSdmSerial, 9600, pin.powermeter_dere,
SERIAL_8N1, pin.powermeter_rx, pin.powermeter_tx); SERIAL_8N1, pin.powermeter_rx, pin.powermeter_tx);
_upSdm->begin(); _upSdm->begin();
break; break;
}
case Source::HTTP: case Source::HTTP:
HttpPowerMeter.init(); HttpPowerMeter.init();