Results 1 to 10 of 10

Thread: QextSerialPort reading error: wrong values

  1. #1
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QextSerialPort reading error: wrong values

    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?

  2. #2
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QextSerialPort reading error: wrong values

    since I found no solution, I am going to use a USB device.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QextSerialPort reading error: wrong values

    Shouldn't you be using unsigned char instead of char?

    By the way, reading the first character in the buffer is not a good idea. You'll be getting stale data if you read slower than the device writes data to the port. Also check the endianess differences between the machines.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:

    Lawand (29th April 2009)

  5. #4
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QextSerialPort reading error: wrong values

    Quote Originally Posted by wysota View Post
    Shouldn't you be using unsigned char instead of char?
    Thanks a million, this seems to have solved the problem, the only thing left is:
    Quote Originally Posted by wysota View Post
    By the way, reading the first character in the buffer is not a good idea.
    That is one of life's mysteries to me
    I am really looking for a way not to do that, and I would really appreciate it if you told me how to...
    Last edited by Lawand; 29th April 2009 at 21:49.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QextSerialPort reading error: wrong values

    Quote Originally Posted by Lawand View Post
    That is one of life's mysteries to me
    I am really looking for a way not to do that, and I would really appreciate it if you told me how to...
    I'd read everything there is to be read using QIODevice::readAll() and I'd take the last character, not the first one.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QextSerialPort reading error: wrong values

    Quote Originally Posted by wysota View Post
    I'd read everything there is to be read using QIODevice::readAll() and I'd take the last character, not the first one.
    This works, thanks
    However, is there any more efficient (in terms of complexity) way to do it?, perhaps just clearing the buffer or something like that, because readAll() is taking about 5 seconds to get done (because of low data transfer rate)

    BTW, I tried closing and opening the port each time receiveByte() is invoked but I get a wrong value...

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QextSerialPort reading error: wrong values

    The data gets transfered TO the port in a low rate but once it reaches the port (bytesAvailable()>0) it's read from a buffer. So if you experience a delay it might be that there is no data to be read and your read() call blocks until some data is returned. So before reading check if there is anything to be read.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QextSerialPort reading error: wrong values

    Quote Originally Posted by wysota View Post
    The data gets transfered TO the port in a low rate but once it reaches the port (bytesAvailable()>0) it's read from a buffer. So if you experience a delay it might be that there is no data to be read and your read() call blocks until some data is returned. So before reading check if there is anything to be read.
    I don't thinks so, because once I start the program, bytesAvailable() are 0 then it increases into ~4000 in like 5 or 6 seconds, and it stops there, then when I read a byte it increases again until it reaches ~20000 and stabilizes, henceforth it doesn't increase again until it's less than the first number (~4000).

    Note that it takes a fraction of a second to read a byte when the number is around 20000 but it takes about 5 only in the time the number gets below the certain limit (~4000) and the QextSerialPort refills about 16000 bytes...

    So, my guess is, it takes a long time for these values to get filled then read.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QextSerialPort reading error: wrong values

    As far as I remember QExtSerialPort is a stupid class that does not do any buffering on its own and it's only a thin layer over native API. So if you are experiencing such behaviours, they are probably happening in your operating system. In general I'd suggest reading faster so that the system buffers don't get full.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QextSerialPort reading error: wrong values

    Thanks for all the info.

Similar Threads

  1. Replies: 7
    Last Post: 29th August 2008, 11:24
  2. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 21:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.