Serialport (#36)

* migrate serialport to qt5 serialport

* remove old serialport
add logging to serialport

* remove try catch - qt serialport uses plain return values

* tiny fix, but not working atm

* make it work, tested with adalight
This commit is contained in:
redPanther
2016-06-23 00:11:09 +02:00
committed by brindosch
parent 48134d0aef
commit 93e3981df1
17 changed files with 57 additions and 4462 deletions

View File

@@ -5,7 +5,7 @@ SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/leddevice)
#add libusb and pthreads (required for the Lighpack usb device)
find_package(libusb-1.0 REQUIRED)
find_package(Threads REQUIRED)
#find_package(Threads REQUIRED)
include_directories(
../../include/hidapi
@@ -137,11 +137,11 @@ add_library(leddevice
${Leddevice_SOURCES}
)
qt5_use_modules(leddevice Network)
qt5_use_modules(leddevice Network SerialPort)
target_link_libraries(leddevice
hyperion-utils
serialport
# serialport
${LIBUSB_1_LIBRARIES} #apt-get install libusb-1.0-0-dev
${CMAKE_THREAD_LIBS_INIT}
${QT_LIBRARIES}

View File

@@ -1,5 +1,6 @@
// Stl includes
#include <string>
#include <sstream>
#include <algorithm>
// Build configuration

View File

@@ -6,9 +6,6 @@
// Qt includes
#include <QTimer>
// Serial includes
#include <serial/serial.h>
// Local Hyperion includes
#include "LedRs232Device.h"
@@ -16,112 +13,74 @@ LedRs232Device::LedRs232Device(const std::string& outputDevice, const unsigned b
_deviceName(outputDevice),
_baudRate_Hz(baudrate),
_delayAfterConnect_ms(delayAfterConnect_ms),
_rs232Port(),
_blockedForDelay(false)
_rs232Port(this),
_blockedForDelay(false),
_log(Logger::getInstance("LedDevice"))
{
// empty
}
LedRs232Device::~LedRs232Device()
{
if (_rs232Port.isOpen())
{
_rs232Port.close();
}
}
int LedRs232Device::open()
{
try
{
std::cout << "Opening UART: " << _deviceName << std::endl;
_rs232Port.setPort(_deviceName);
_rs232Port.setBaudrate(_baudRate_Hz);
_rs232Port.open();
Info(_log, "Opening UART: %s", _deviceName.c_str());
_rs232Port.setPortName(_deviceName.c_str());
if (_delayAfterConnect_ms > 0)
{
_blockedForDelay = true;
QTimer::singleShot(_delayAfterConnect_ms, this, SLOT(unblockAfterDelay()));
std::cout << "Device blocked for " << _delayAfterConnect_ms << " ms" << std::endl;
}
}
catch (const std::exception& e)
{
std::cerr << "Unable to open RS232 device (" << e.what() << ")" << std::endl;
return -1;
}
return 0;
return tryOpen() ? 0 : -1;
}
bool LedRs232Device::tryOpen()
{
if ( ! _rs232Port.isOpen() )
{
if ( ! _rs232Port.open(QIODevice::WriteOnly) )
{
Error(_log, "Unable to open RS232 device (%s)", _deviceName.c_str());
return false;
}
_rs232Port.setBaudRate(_baudRate_Hz);
}
if (_delayAfterConnect_ms > 0)
{
_blockedForDelay = true;
QTimer::singleShot(_delayAfterConnect_ms, this, SLOT(unblockAfterDelay()));
Debug(_log, "Device blocked for %d ms", _delayAfterConnect_ms);
}
return _rs232Port.isOpen();
}
int LedRs232Device::writeBytes(const unsigned size, const uint8_t * data)
{
if (_blockedForDelay)
{
return 0;
}
if (!_rs232Port.isOpen())
{
// 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;
_delayAfterConnect_ms = 3000;
return tryOpen() ? 0 : -1;
}
// for (int i = 0; i < 20; ++i)
// std::cout << std::hex << (int)data[i] << " ";
// std::cout << std::endl;
_rs232Port.flush();
int result = _rs232Port.write(reinterpret_cast<const char*>(data), size);
_rs232Port.waitForBytesWritten(100);
Debug(_log, "write %d ", result);
_rs232Port.flush();
try
{
_rs232Port.flushOutput();
_rs232Port.write(data, size);
_rs232Port.flush();
}
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);
_rs232Port.flush();
}
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;
return -1;
}
return 0;
return (result<0) ? -1 : 0;
}
void LedRs232Device::unblockAfterDelay()
{
std::cout << "Device unblocked" << std::endl;
Debug(_log, "Device unblocked");
_blockedForDelay = false;
}

View File

@@ -1,13 +1,13 @@
#pragma once
#include <QObject>
// Serial includes
#include <serial/serial.h>
#include <QSerialPort>
// Leddevice includes
#include <leddevice/LedDevice.h>
#include <utils/Logger.h>
///
/// The LedRs232Device implements an abstract base-class for LedDevices using a RS232-device.
///
@@ -52,17 +52,23 @@ private slots:
void unblockAfterDelay();
private:
// tries to open device if not opened
bool tryOpen();
/// The name of the output device
const std::string _deviceName;
/// The used baudrate of the output device
const int _baudRate_Hz;
const qint32 _baudRate_Hz;
/// Sleep after the connect before continuing
const int _delayAfterConnect_ms;
int _delayAfterConnect_ms;
/// The RS232 serial-device
serial::Serial _rs232Port;
QSerialPort _rs232Port;
bool _blockedForDelay;
/// logger instance
Logger* _log;
};