diff --git a/include/JkBmsController.h b/include/JkBmsController.h index b21744d3f..bbc5a5ac0 100644 --- a/include/JkBmsController.h +++ b/include/JkBmsController.h @@ -19,7 +19,9 @@ class Controller : public BatteryProvider { void deinit() final; void loop() final; std::shared_ptr getStats() const final { return _stats; } - bool usesHwPort2() const final { return true; } + bool usesHwPort2() const final { + return ARDUINO_USB_CDC_ON_BOOT != 1; + } private: enum class Status : unsigned { diff --git a/include/VictronSmartShunt.h b/include/VictronSmartShunt.h index 42b65774e..97b421325 100644 --- a/include/VictronSmartShunt.h +++ b/include/VictronSmartShunt.h @@ -9,7 +9,9 @@ class VictronSmartShunt : public BatteryProvider { void deinit() final { } void loop() final; std::shared_ptr getStats() const final { return _stats; } - bool usesHwPort2() const final { return true; } + bool usesHwPort2() const final { + return ARDUINO_USB_CDC_ON_BOOT != 1; + } private: uint32_t _lastUpdate = 0; diff --git a/lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp b/lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp index 074285dbd..44435dacf 100644 --- a/lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp +++ b/lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp @@ -65,6 +65,7 @@ template void VeDirectFrameHandler::init(char const* who, int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging, uint16_t hwSerialPort) { _vedirectSerial = std::make_unique(hwSerialPort); + _vedirectSerial->end(); // make sure the UART will be re-initialized _vedirectSerial->begin(19200, SERIAL_8N1, rx, tx); _vedirectSerial->flush(); _canSend = (tx != -1); diff --git a/lib/VeDirectFrameHandler/VeDirectShuntController.cpp b/lib/VeDirectFrameHandler/VeDirectShuntController.cpp index 54229b93d..2d8b85a71 100644 --- a/lib/VeDirectFrameHandler/VeDirectShuntController.cpp +++ b/lib/VeDirectFrameHandler/VeDirectShuntController.cpp @@ -5,7 +5,8 @@ VeDirectShuntController VeDirectShunt; void VeDirectShuntController::init(int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging) { - VeDirectFrameHandler::init("SmartShunt", rx, tx, msgOut, verboseLogging, 2); + VeDirectFrameHandler::init("SmartShunt", rx, tx, msgOut, verboseLogging, + ((ARDUINO_USB_CDC_ON_BOOT != 1)?2:0)); } bool VeDirectShuntController::processTextDataDerived(std::string const& name, std::string const& value) diff --git a/src/JkBmsController.cpp b/src/JkBmsController.cpp index 3f924030f..94f80cb08 100644 --- a/src/JkBmsController.cpp +++ b/src/JkBmsController.cpp @@ -198,7 +198,7 @@ class DummySerial { }; DummySerial HwSerial; #else -HardwareSerial HwSerial(2); +HardwareSerial HwSerial((ARDUINO_USB_CDC_ON_BOOT != 1)?2:0); #endif namespace JkBms { @@ -220,6 +220,7 @@ bool Controller::init(bool verboseLogging) return false; } + HwSerial.end(); // make sure the UART will be re-initialized HwSerial.begin(115200, SERIAL_8N1, pin.battery_rx, pin.battery_tx); HwSerial.flush();