Hi,

I try to emit a signal with the data actually received by QextSerialPort: something like the event DataReceived of C# .NET.

When I open the port I start a QTimer that every 100ms checks if there is byte available on the port and emit the signal with the data read.

My GUI freezes in this way If I block the sender device, my GUI exits from the freeze state and show some data.

Any ideas?

serialport.h
Qt Code:
  1. #include <QObject>
  2. class QextSerialPort;
  3. class QTimer;
  4.  
  5. class SerialPort : public QObject
  6. {
  7. Q_OBJECT
  8.  
  9. public:
  10. SerialPort(QObject *parent = 0);
  11. ~SerialPort();
  12.  
  13. signals:
  14. void receivedBytes(const QByteArray&, int);
  15.  
  16. public slots:
  17. bool openPort();
  18. void closePort();
  19.  
  20. private slots:
  21. void checkRx();
  22.  
  23. private:
  24. QextSerialPort *d_port;
  25. QTimer *d_timer;
  26. };
To copy to clipboard, switch view to plain text mode 

serialport.cpp
Qt Code:
  1. #include <QTimer>
  2. #include <QDebug>
  3. #include <qextserialport.h>
  4. #include "serialport.h"
  5.  
  6. const int timeout = 100;
  7.  
  8. SerialPort::SerialPort(QObject *parent):
  9. QObject(parent)
  10. {
  11. //modify the port settings on your own
  12. d_port = new QextSerialPort("/dev/ttyUSB0");
  13. d_port->setBaudRate(BAUD19200);
  14. d_port->setFlowControl(FLOW_OFF);
  15. d_port->setParity(PAR_NONE);
  16. d_port->setDataBits(DATA_8);
  17. d_port->setStopBits(STOP_1);
  18.  
  19. d_timer = NULL;
  20. }
  21.  
  22. SerialPort::~SerialPort()
  23. {
  24. d_port->close();
  25. delete d_port;
  26. d_port = NULL;
  27.  
  28. d_timer->stop();
  29. delete d_timer;
  30. d_timer = NULL;
  31. }
  32.  
  33. bool SerialPort::openPort()
  34. {
  35. bool state;
  36. state = d_port->open(QIODevice::ReadWrite);
  37.  
  38. if (state)
  39. {
  40. if ( !d_timer )
  41. {
  42. d_timer = new QTimer(this);
  43. connect(d_timer, SIGNAL(timeout()),
  44. this, SLOT(checkRx()));
  45. }
  46. d_timer->start(timeout);
  47. }
  48. return state;
  49. }
  50.  
  51. void SerialPort::closePort()
  52. {
  53. if (d_timer)
  54. d_timer->stop();
  55.  
  56. d_port->close();
  57. }
  58.  
  59. void SerialPort::checkRx()
  60. {
  61. int numBytes;
  62.  
  63. numBytes = d_port->bytesAvailable();
  64.  
  65. if (numBytes>0)
  66. {
  67. qDebug() << numBytes;
  68.  
  69. numBytes = (numBytes > 1024) ? 1024 : numBytes;
  70.  
  71. QByteArray bytes = d_port->read(numBytes);
  72.  
  73. emit receivedBytes(bytes, numBytes);
  74. }
  75. }
To copy to clipboard, switch view to plain text mode