From f86d40241e17600dc9874785cabfc14fbdfd1be5 Mon Sep 17 00:00:00 2001 From: Drath Vader Date: Thu, 27 Nov 2025 15:23:17 +0100 Subject: [PATCH] i was never checking the return value of write --- include/LCDDisplay.hpp | 2 +- src/LCDDisplay.cpp | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/LCDDisplay.hpp b/include/LCDDisplay.hpp index c891793..7c7937d 100644 --- a/include/LCDDisplay.hpp +++ b/include/LCDDisplay.hpp @@ -15,7 +15,7 @@ class LCDDisplay { char writebuf[LCD_LINES * LCD_COLS + 8]; // 4x40 chars + 2x4 setcursor commands static constexpr bool dualDisp = (LCD_LINES * LCD_COLS > 80); // is it dual display device or not, evauluated from the size at compile time - void open(); + void open(bool reopen = false); void write(const uint8_t* cmdbuf, uint8_t len); void close(); diff --git a/src/LCDDisplay.cpp b/src/LCDDisplay.cpp index 132fbdb..580f897 100644 --- a/src/LCDDisplay.cpp +++ b/src/LCDDisplay.cpp @@ -33,17 +33,15 @@ LCDDisplay::LCDDisplay(std::string dev, uint32_t baud) { // clear display and set cursor home this->clear(); - this->close(); - log(LOG::INF, "LCDDisplay", "Opened LCD port at \"{}\"", _dev); } LCDDisplay::~LCDDisplay() { this->close(); - log(LOG::INF, "LCDDisplay", "Closed LCD port"); } -void LCDDisplay::open() { - if (_lcd < 0) +void LCDDisplay::open(bool reopen) { + log(LOG::INF, "LCDDisplay", "Opening serial port at \"{}\"", _dev); + if (_lcd < 0 || reopen) _lcd = ::open(_dev.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); // ::open calls the global open() instead of LCDDisplay::open() if (_lcd < 0) { log(LOG::ERR, "LCDDisplay", "Error opening serial port \"{}\"", _dev); // err @@ -52,15 +50,19 @@ void LCDDisplay::open() { } void LCDDisplay::close() { - if (_lcd >= 0) + if (_lcd >= 0) { ::close(_lcd); + log(LOG::INF, "LCDDisplay", "Closed LCD port"); + } _lcd = -1; } void LCDDisplay::write(const uint8_t* cmdbuf, uint8_t len) { - this->open(); - ::write(_lcd, cmdbuf, len); - this->close(); + int rc = ::write(_lcd, cmdbuf, len); + if(rc < 0) { + log(LOG::ERR, "LCDDisplay::write", "failed to execute ::write, lost access to port?"); + this->open(true); + } } void LCDDisplay::clear() {