Skip to content

Commit

Permalink
fix: make SDM power meter use serial port manager
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
schlimmchen committed Jun 2, 2024
1 parent be41e6b commit 5a007e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/PowerMeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class PowerMeterClass {

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<SoftwareSerial> _upSmlSerial = nullptr;

Expand Down
11 changes: 9 additions & 2 deletions src/PowerMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "MqttSettings.h"
#include "NetworkSettings.h"
#include "MessageOutput.h"
#include "SerialPortManager.h"
#include <ctime>
#include <SMA_HM.h>

Expand Down Expand Up @@ -57,16 +58,22 @@ void PowerMeterClass::init(Scheduler& scheduler)
}

case Source::SDM1PH:
case Source::SDM3PH:
case Source::SDM3PH: {
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)");
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);
_upSdm->begin();
break;
}

case Source::HTTP:
HttpPowerMeter.init();
Expand Down

0 comments on commit 5a007e5

Please sign in to comment.