Hello,
I have a serial hardware device that is sending a certain (hardware-controlled) numeric value in the range [0 .. 255] over and over again.
I am sure the device is sending the correct value, but when I am trying to read it through an instance of QextSerialPort, I am getting wrong values.
And even when changing that (hardware-controlled) value, I am reading the same old wrong value

Here is the code used (a modified version of the example shipped with QextSerialPort):

Qt Code:
  1. /* qesptest.h
  2. **************************************/
  3.  
  4. #ifndef _QESPTEST_H_
  5. #define _QESPTEST_H_
  6.  
  7. #include <QWidget>
  8.  
  9.  
  10. class QTextEdit;
  11. class QextSerialPort;
  12.  
  13. class QespTest : public QWidget
  14. {
  15. Q_OBJECT
  16. public:
  17. QespTest(QWidget *parent=0);
  18.  
  19. virtual ~QespTest();
  20.  
  21. private:
  22. QTextEdit *received_msg;
  23. QextSerialPort *port;
  24.  
  25. private slots:
  26. void receiveByte();
  27. };
  28.  
  29. #endif
To copy to clipboard, switch view to plain text mode 
charToBinaryStdString() is created so that I can display the read char as 1s and 0s...
Qt Code:
  1. /* qesptest.cpp
  2. **************************************/
  3. #include "qesptest.h"
  4. #include <qextserialport.h>
  5. #include <QLayout>
  6. #include <QTextEdit>
  7. #include <QPushButton>
  8. #include <string>
  9.  
  10. QespTest::QespTest(QWidget* parent)
  11. : QWidget(parent)
  12.  
  13. {
  14. //port settings
  15. port = new QextSerialPort("COM1");
  16. port->setBaudRate(BAUD9600);
  17. port->setFlowControl(FLOW_XONXOFF);
  18. port->setParity(PAR_NONE);
  19. port->setDataBits(DATA_8);
  20. port->setStopBits(STOP_1);
  21. port->open(QIODevice::ReadOnly);
  22.  
  23. QPushButton *receiveButton = new QPushButton("Receive");
  24. connect(receiveButton, SIGNAL(clicked()), SLOT(receiveByte()));
  25.  
  26. received_msg = new QTextEdit();
  27.  
  28. QVBoxLayout *myVBox = new QVBoxLayout;
  29. myVBox->addWidget(receiveButton);
  30. myVBox->addWidget(received_msg);
  31. setLayout(myVBox);
  32. }
  33.  
  34. QespTest::~QespTest()
  35. {
  36. port->close();
  37. delete port;
  38. port = NULL;
  39. }
  40.  
  41. std::string charToBinaryStdString(char ch)
  42. {
  43. std::string result;
  44. int i = 8;
  45.  
  46. while (i >0)
  47. {
  48. --i;
  49. result += (ch&(1 << i) ? '1' : '0');
  50. }
  51.  
  52. return result;
  53. }
  54.  
  55. void QespTest::receiveByte()
  56. {
  57. port->open(QIODevice::ReadOnly);
  58.  
  59. char ch;
  60. if(port->bytesAvailable() > 0)
  61. {
  62. port->read(&ch,1);
  63. received_msg->append(QString::fromStdString(charToBinaryStdString(ch)));
  64. }
  65. }
To copy to clipboard, switch view to plain text mode 

main() just includes an instance of QespTest and QApplication...

--------

What's wrong?