Added some try-catch to handle device reset

Former-commit-id: 98caccf476fbd703adea1f2c5a19dabb6626e50f
This commit is contained in:
T. van der Zwan 2013-12-17 19:28:57 +00:00
parent c3bd109a99
commit 6cb0fdf072
1 changed files with 27 additions and 0 deletions

View File

@ -4,6 +4,9 @@
#include <cstdio>
#include <iostream>
// Serial includes
#include <serial/serial.h>
// 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;