From 6cb0fdf07277f47a7bd6b8e86fb00bf32d0d8873 Mon Sep 17 00:00:00 2001 From: "T. van der Zwan" Date: Tue, 17 Dec 2013 19:28:57 +0000 Subject: [PATCH] Added some try-catch to handle device reset Former-commit-id: 98caccf476fbd703adea1f2c5a19dabb6626e50f --- libsrc/leddevice/LedRs232Device.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/libsrc/leddevice/LedRs232Device.cpp b/libsrc/leddevice/LedRs232Device.cpp index efdd296e..3486ac4a 100644 --- a/libsrc/leddevice/LedRs232Device.cpp +++ b/libsrc/leddevice/LedRs232Device.cpp @@ -4,6 +4,9 @@ #include #include +// Serial includes +#include + // Local Hyperion includes #include "LedRs232Device.h" @@ -56,6 +59,30 @@ int LedRs232Device::writeBytes(const unsigned size, const uint8_t * data) { _rs232Port.write(data, size); } + catch (const serial::SerialException & serialExc) + { + // TODO[TvdZ]: Maybe we should limit the frequency of this error report somehow + std::cerr << "Serial exception caught while writing to device: " << serialExc.what() << std::endl; + std::cout << "Attempting to re-open the device." << std::endl; + + // First make sure the device is properly closed + try + { + _rs232Port.close(); + } + catch (const std::exception & e) {} + + // Attempt to open the device and write the data + try + { + _rs232Port.open(); + _rs232Port.write(data, size); + } + catch (const std::exception & e) + { + // We failed again, this not good, do nothing maybe in the next loop we have more success + } + } catch (const std::exception& e) { std::cerr << "Unable to write to RS232 device (" << e.what() << ")" << std::endl;