2013-11-05 16:46:17 +01:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
|
2014-05-04 11:41:55 +02:00
|
|
|
// Qt includes
|
|
|
|
#include <QTimer>
|
|
|
|
|
2013-12-17 20:28:57 +01:00
|
|
|
// Serial includes
|
|
|
|
#include <serial/serial.h>
|
|
|
|
|
2013-11-05 16:46:17 +01:00
|
|
|
// Local Hyperion includes
|
|
|
|
#include "LedRs232Device.h"
|
|
|
|
|
2014-05-04 11:31:13 +02:00
|
|
|
LedRs232Device::LedRs232Device(const std::string& outputDevice, const unsigned baudrate, int delayAfterConnect_ms) :
|
|
|
|
_deviceName(outputDevice),
|
|
|
|
_baudRate_Hz(baudrate),
|
|
|
|
_delayAfterConnect_ms(delayAfterConnect_ms),
|
2014-05-04 11:41:55 +02:00
|
|
|
_rs232Port(),
|
|
|
|
_blockedForDelay(false)
|
2013-11-05 16:46:17 +01:00
|
|
|
{
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
|
|
|
|
LedRs232Device::~LedRs232Device()
|
|
|
|
{
|
|
|
|
if (_rs232Port.isOpen())
|
|
|
|
{
|
|
|
|
_rs232Port.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedRs232Device::open()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-05-04 11:31:13 +02:00
|
|
|
std::cout << "Opening UART: " << _deviceName << std::endl;
|
|
|
|
_rs232Port.setPort(_deviceName);
|
|
|
|
_rs232Port.setBaudrate(_baudRate_Hz);
|
2013-11-05 16:46:17 +01:00
|
|
|
_rs232Port.open();
|
2014-05-04 11:31:13 +02:00
|
|
|
|
|
|
|
if (_delayAfterConnect_ms > 0)
|
|
|
|
{
|
2014-05-04 11:41:55 +02:00
|
|
|
_blockedForDelay = true;
|
|
|
|
QTimer::singleShot(_delayAfterConnect_ms, this, SLOT(unblockAfterDelay()));
|
|
|
|
std::cout << "Device blocked for " << _delayAfterConnect_ms << " ms" << std::endl;
|
2014-05-04 11:31:13 +02:00
|
|
|
}
|
2013-11-05 16:46:17 +01:00
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
std::cerr << "Unable to open RS232 device (" << e.what() << ")" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedRs232Device::writeBytes(const unsigned size, const uint8_t * data)
|
|
|
|
{
|
2014-05-04 11:41:55 +02:00
|
|
|
if (_blockedForDelay)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-05 16:46:17 +01:00
|
|
|
if (!_rs232Port.isOpen())
|
|
|
|
{
|
2015-07-20 14:01:24 +02:00
|
|
|
// try to reopen
|
|
|
|
int status = open();
|
|
|
|
if(status == -1){
|
|
|
|
// Try again in 3 seconds
|
|
|
|
int seconds = 3000;
|
|
|
|
_blockedForDelay = true;
|
|
|
|
QTimer::singleShot(seconds, this, SLOT(unblockAfterDelay()));
|
|
|
|
std::cout << "Device blocked for " << seconds << " ms" << std::endl;
|
|
|
|
}
|
|
|
|
return status;
|
2013-11-05 16:46:17 +01:00
|
|
|
}
|
|
|
|
|
2013-11-11 21:07:24 +01:00
|
|
|
// for (int i = 0; i < 20; ++i)
|
|
|
|
// std::cout << std::hex << (int)data[i] << " ";
|
|
|
|
// std::cout << std::endl;
|
2013-11-05 21:32:21 +01:00
|
|
|
|
2013-11-05 16:46:17 +01:00
|
|
|
try
|
|
|
|
{
|
2013-12-28 08:34:49 +01:00
|
|
|
_rs232Port.flushOutput();
|
2013-11-05 16:46:17 +01:00
|
|
|
_rs232Port.write(data, size);
|
2013-12-23 22:58:54 +01:00
|
|
|
_rs232Port.flush();
|
2013-11-05 16:46:17 +01:00
|
|
|
}
|
2013-12-17 20:28:57 +01:00
|
|
|
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);
|
2013-12-23 22:58:54 +01:00
|
|
|
_rs232Port.flush();
|
2013-12-17 20:28:57 +01:00
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
// We failed again, this not good, do nothing maybe in the next loop we have more success
|
|
|
|
}
|
|
|
|
}
|
2013-11-05 16:46:17 +01:00
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
std::cerr << "Unable to write to RS232 device (" << e.what() << ")" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-04 11:41:55 +02:00
|
|
|
|
|
|
|
void LedRs232Device::unblockAfterDelay()
|
|
|
|
{
|
|
|
|
std::cout << "Device unblocked" << std::endl;
|
|
|
|
_blockedForDelay = false;
|
|
|
|
}
|