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-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),
|
2016-06-23 00:11:09 +02:00
|
|
|
_rs232Port(this),
|
2016-06-25 22:08:17 +02:00
|
|
|
_blockedForDelay(false)
|
2013-11-05 16:46:17 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LedRs232Device::~LedRs232Device()
|
|
|
|
{
|
|
|
|
if (_rs232Port.isOpen())
|
|
|
|
_rs232Port.close();
|
|
|
|
}
|
|
|
|
|
2016-06-23 00:11:09 +02:00
|
|
|
|
2013-11-05 16:46:17 +01:00
|
|
|
int LedRs232Device::open()
|
|
|
|
{
|
2016-06-23 00:11:09 +02:00
|
|
|
Info(_log, "Opening UART: %s", _deviceName.c_str());
|
|
|
|
_rs232Port.setPortName(_deviceName.c_str());
|
|
|
|
|
|
|
|
return tryOpen() ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2014-05-04 11:31:13 +02:00
|
|
|
|
2016-06-23 00:11:09 +02:00
|
|
|
bool LedRs232Device::tryOpen()
|
|
|
|
{
|
|
|
|
if ( ! _rs232Port.isOpen() )
|
|
|
|
{
|
|
|
|
if ( ! _rs232Port.open(QIODevice::WriteOnly) )
|
2014-05-04 11:31:13 +02:00
|
|
|
{
|
2016-06-23 00:11:09 +02:00
|
|
|
Error(_log, "Unable to open RS232 device (%s)", _deviceName.c_str());
|
|
|
|
return false;
|
2014-05-04 11:31:13 +02:00
|
|
|
}
|
2016-06-23 00:11:09 +02:00
|
|
|
_rs232Port.setBaudRate(_baudRate_Hz);
|
2013-11-05 16:46:17 +01:00
|
|
|
}
|
2016-06-23 00:11:09 +02:00
|
|
|
|
|
|
|
if (_delayAfterConnect_ms > 0)
|
2013-11-05 16:46:17 +01:00
|
|
|
{
|
2016-06-23 00:11:09 +02:00
|
|
|
_blockedForDelay = true;
|
|
|
|
QTimer::singleShot(_delayAfterConnect_ms, this, SLOT(unblockAfterDelay()));
|
|
|
|
Debug(_log, "Device blocked for %d ms", _delayAfterConnect_ms);
|
2013-11-05 16:46:17 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 00:11:09 +02:00
|
|
|
return _rs232Port.isOpen();
|
2013-11-05 16:46:17 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 00:11:09 +02:00
|
|
|
|
2013-11-05 16:46:17 +01:00
|
|
|
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())
|
|
|
|
{
|
2016-06-23 00:11:09 +02:00
|
|
|
_delayAfterConnect_ms = 3000;
|
|
|
|
return tryOpen() ? 0 : -1;
|
2013-11-05 16:46:17 +01:00
|
|
|
}
|
2013-12-17 20:28:57 +01:00
|
|
|
|
2016-06-23 00:11:09 +02:00
|
|
|
_rs232Port.flush();
|
|
|
|
int result = _rs232Port.write(reinterpret_cast<const char*>(data), size);
|
|
|
|
_rs232Port.waitForBytesWritten(100);
|
|
|
|
Debug(_log, "write %d ", result);
|
|
|
|
_rs232Port.flush();
|
2013-12-17 20:28:57 +01:00
|
|
|
|
2016-06-23 00:11:09 +02:00
|
|
|
return (result<0) ? -1 : 0;
|
2013-11-05 16:46:17 +01:00
|
|
|
}
|
2014-05-04 11:41:55 +02:00
|
|
|
|
2016-06-23 00:11:09 +02:00
|
|
|
|
2014-05-04 11:41:55 +02:00
|
|
|
void LedRs232Device::unblockAfterDelay()
|
|
|
|
{
|
2016-06-23 00:11:09 +02:00
|
|
|
Debug(_log, "Device unblocked");
|
2014-05-04 11:41:55 +02:00
|
|
|
_blockedForDelay = false;
|
|
|
|
}
|