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
2016-08-28 07:12:48 +02:00
# include "ProviderRs232.h"
2013-11-05 16:46:17 +01:00
2016-10-08 08:14:36 +02:00
ProviderRs232 : : ProviderRs232 ( )
2016-08-23 20:07:12 +02:00
: _rs232Port ( this )
2016-07-20 16:04:56 +02:00
, _blockedForDelay ( false )
, _stateChanged ( true )
2016-09-25 22:20:01 +02:00
, _bytesToWrite ( 0 )
, _bytesWritten ( 0 )
, _frameDropCounter ( 0 )
, _lastError ( QSerialPort : : NoError )
2013-11-05 16:46:17 +01:00
{
2016-07-20 23:26:47 +02:00
connect ( & _rs232Port , SIGNAL ( error ( QSerialPort : : SerialPortError ) ) , this , SLOT ( error ( QSerialPort : : SerialPortError ) ) ) ;
2016-09-25 22:20:01 +02:00
connect ( & _rs232Port , SIGNAL ( bytesWritten ( qint64 ) ) , this , SLOT ( bytesWritten ( qint64 ) ) ) ;
connect ( & _rs232Port , SIGNAL ( readyRead ( ) ) , this , SLOT ( readyRead ( ) ) ) ;
2016-07-20 23:26:47 +02:00
}
2016-10-13 21:59:58 +02:00
bool ProviderRs232 : : init ( const QJsonObject & deviceConfig )
2016-08-23 20:07:12 +02:00
{
closeDevice ( ) ;
2016-10-13 21:59:58 +02:00
_deviceName = deviceConfig [ " output " ] . toString ( ) . toStdString ( ) ;
_baudRate_Hz = deviceConfig [ " rate " ] . toInt ( ) ;
_delayAfterConnect_ms = deviceConfig [ " delayAfterConnect " ] . toInt ( 250 ) ;
2016-11-29 23:14:15 +01:00
_refresh_timer . setInterval ( deviceConfig [ " rewriteTime " ] . toInt ( 5000 ) ) ;
2016-08-23 20:07:12 +02:00
return true ;
}
2016-09-25 22:20:01 +02:00
void ProviderRs232 : : bytesWritten ( qint64 bytes )
{
_bytesWritten + = bytes ;
if ( _bytesWritten > = _bytesToWrite )
{
_bytesToWrite = 0 ;
_blockedForDelay = false ;
}
}
void ProviderRs232 : : readyRead ( )
{
QByteArray data = _rs232Port . readAll ( ) ;
Debug ( _log , " received %d bytes data " , data . size ( ) ) ;
}
2016-08-28 07:12:48 +02:00
void ProviderRs232 : : error ( QSerialPort : : SerialPortError error )
2016-07-20 23:26:47 +02:00
{
if ( error ! = QSerialPort : : NoError )
{
2016-09-25 22:20:01 +02:00
if ( _lastError ! = error )
2016-07-20 23:26:47 +02:00
{
2016-09-25 22:20:01 +02:00
_lastError = error ;
switch ( error )
{
2016-07-20 23:26:47 +02:00
case QSerialPort : : DeviceNotFoundError :
Error ( _log , " An error occurred while attempting to open an non-existing device. " ) ; break ;
case QSerialPort : : PermissionError :
Error ( _log , " An error occurred while attempting to open an already opened device by another process or a user not having enough permission and credentials to open. " ) ; break ;
case QSerialPort : : OpenError :
Error ( _log , " An error occurred while attempting to open an already opened device in this object. " ) ; break ;
case QSerialPort : : NotOpenError :
Error ( _log , " This error occurs when an operation is executed that can only be successfully performed if the device is open. " ) ; break ;
case QSerialPort : : ParityError :
2016-09-25 22:20:01 +02:00
Error ( _log , " Parity error detected by the hardware while reading data. " ) ; break ;
2016-07-20 23:26:47 +02:00
case QSerialPort : : FramingError :
2016-09-25 22:20:01 +02:00
Error ( _log , " Framing error detected by the hardware while reading data. " ) ; break ;
2016-07-20 23:26:47 +02:00
case QSerialPort : : BreakConditionError :
2016-09-25 22:20:01 +02:00
Error ( _log , " Break condition detected by the hardware on the input line. " ) ; break ;
2016-07-20 23:26:47 +02:00
case QSerialPort : : WriteError :
Error ( _log , " An I/O error occurred while writing the data. " ) ; break ;
case QSerialPort : : ReadError :
Error ( _log , " An I/O error occurred while reading the data. " ) ; break ;
case QSerialPort : : ResourceError :
Error ( _log , " An I/O error occurred when a resource becomes unavailable, e.g. when the device is unexpectedly removed from the system. " ) ; break ;
case QSerialPort : : UnsupportedOperationError :
Error ( _log , " The requested device operation is not supported or prohibited by the running operating system. " ) ; break ;
case QSerialPort : : TimeoutError :
Error ( _log , " A timeout error occurred. " ) ; break ;
default : Error ( _log , " An unidentified error occurred. (%d) " , error ) ;
2016-09-25 22:20:01 +02:00
}
_rs232Port . clearError ( ) ;
closeDevice ( ) ;
2016-07-20 23:26:47 +02:00
}
}
2013-11-05 16:46:17 +01:00
}
2016-08-28 07:12:48 +02:00
ProviderRs232 : : ~ ProviderRs232 ( )
2013-11-05 16:46:17 +01:00
{
2016-07-20 23:26:47 +02:00
disconnect ( & _rs232Port , SIGNAL ( error ( QSerialPort : : SerialPortError ) ) , this , SLOT ( error ( QSerialPort : : SerialPortError ) ) ) ;
2016-08-23 20:07:12 +02:00
closeDevice ( ) ;
}
2016-08-28 07:12:48 +02:00
void ProviderRs232 : : closeDevice ( )
2016-08-23 20:07:12 +02:00
{
2013-11-05 16:46:17 +01:00
if ( _rs232Port . isOpen ( ) )
2016-07-20 16:04:56 +02:00
{
2013-11-05 16:46:17 +01:00
_rs232Port . close ( ) ;
2016-07-20 16:04:56 +02:00
Debug ( _log , " Close UART: %s " , _deviceName . c_str ( ) ) ;
}
2013-11-05 16:46:17 +01:00
}
2016-08-28 07:12:48 +02:00
int ProviderRs232 : : open ( )
2013-11-05 16:46:17 +01:00
{
2016-06-23 00:11:09 +02:00
Info ( _log , " Opening UART: %s " , _deviceName . c_str ( ) ) ;
_rs232Port . setPortName ( _deviceName . c_str ( ) ) ;
2016-09-25 22:20:01 +02:00
return tryOpen ( _delayAfterConnect_ms ) ? 0 : - 1 ;
2016-06-23 00:11:09 +02:00
}
2014-05-04 11:31:13 +02:00
2016-09-25 22:20:01 +02:00
bool ProviderRs232 : : tryOpen ( const int delayAfterConnect_ms )
2016-06-23 00:11:09 +02:00
{
if ( ! _rs232Port . isOpen ( ) )
{
2016-09-25 22:20:01 +02:00
if ( ! _rs232Port . open ( QIODevice : : ReadWrite ) )
2014-05-04 11:31:13 +02:00
{
2016-07-20 16:04:56 +02:00
if ( _stateChanged )
{
Error ( _log , " Unable to open RS232 device (%s) " , _deviceName . c_str ( ) ) ;
_stateChanged = false ;
}
2016-06-23 00:11:09 +02:00
return false ;
2014-05-04 11:31:13 +02:00
}
2016-09-26 17:50:11 +02:00
Debug ( _log , " Setting baud rate to %d " , _baudRate_Hz ) ;
2016-06-23 00:11:09 +02:00
_rs232Port . setBaudRate ( _baudRate_Hz ) ;
2016-07-20 16:04:56 +02:00
_stateChanged = true ;
2013-11-05 16:46:17 +01:00
}
2016-07-20 16:04:56 +02:00
2016-09-25 22:20:01 +02:00
if ( delayAfterConnect_ms > 0 )
2013-11-05 16:46:17 +01:00
{
2016-06-23 00:11:09 +02:00
_blockedForDelay = true ;
2016-09-25 22:20:01 +02:00
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
2016-09-25 22:20:01 +02:00
int ProviderRs232 : : writeBytes ( const qint64 size , const uint8_t * data )
2013-11-05 16:46:17 +01:00
{
2016-09-25 22:20:01 +02:00
if ( ! _blockedForDelay )
2016-08-14 10:46:44 +02:00
{
2016-09-25 22:20:01 +02:00
if ( ! _rs232Port . isOpen ( ) )
{
2016-10-08 08:14:36 +02:00
return tryOpen ( 5000 ) ? 0 : - 1 ;
2016-09-25 22:20:01 +02:00
}
2014-05-04 11:41:55 +02:00
2016-11-26 22:34:46 +01:00
if ( _frameDropCounter > 5 )
2016-09-25 22:20:01 +02:00
{
Debug ( _log , " %d frames dropped " , _frameDropCounter ) ;
}
_frameDropCounter = 0 ;
_blockedForDelay = true ;
_bytesToWrite = size ;
qint64 bytesWritten = _rs232Port . write ( reinterpret_cast < const char * > ( data ) , size ) ;
if ( bytesWritten = = - 1 | | bytesWritten ! = size )
{
Warning ( _log , " failed writing data " ) ;
QTimer : : singleShot ( 500 , this , SLOT ( unblockAfterDelay ( ) ) ) ;
return - 1 ;
}
QTimer : : singleShot ( 5000 , this , SLOT ( unblockAfterDelay ( ) ) ) ;
2013-11-05 16:46:17 +01:00
}
2016-09-25 22:20:01 +02:00
else
2016-07-20 23:26:47 +02:00
{
2016-09-25 22:20:01 +02:00
_frameDropCounter + + ;
2016-07-20 23:26:47 +02:00
}
2013-12-17 20:28:57 +01:00
2016-07-20 23:26:47 +02:00
return 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
2016-08-28 07:12:48 +02:00
void ProviderRs232 : : unblockAfterDelay ( )
2014-05-04 11:41:55 +02:00
{
_blockedForDelay = false ;
}
2016-10-08 08:14:36 +02:00
int ProviderRs232 : : rewriteLeds ( )
{
return writeBytes ( _ledBuffer . size ( ) , _ledBuffer . data ( ) ) ;
}