Results 1 to 2 of 2

Thread: serial port communication: unstable reading (SOLVED)

  1. #1
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question serial port communication: unstable reading (SOLVED)

    hi,

    i am using QextSerialPort to read a string "ABCDEF01" from serial port. i have a device sends this string every half second, and the baud rate is 115200.

    my serial port setting is:

    Qt Code:
    1. void MainWindow::openSerialPort()
    2. {
    3. serialPort = new QextSerialPort("/dev/ttyUSB0");
    4. if (!serialPort) {
    5. QMessageBox::warning(this, "Warning",
    6. "Could not open serial port\n"
    7. "Please check connection.",
    8. }
    9. else {
    10. serialPort->setBaudRate(BAUD115200);
    11. serialPort->setParity(PAR_NONE);
    12. serialPort->setFlowControl(FLOW_OFF);
    13. serialPort->setDataBits(DATA_8);
    14. serialPort->setStopBits(STOP_1);
    15. if (!serialPort->open(QIODevice::ReadOnly))
    16. ui->label_temp->setText("can not open serial port.");
    17. else
    18. ui->label_temp->setText("serial port opened.");
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    My function serReceive() reads serial port every 100m/second which generated by a Qtimer event.

    Qt Code:
    1. void MainWindow::serReceive()
    2. {
    3. serialPort->open(QIODevice::ReadOnly);
    4. char recBuffer[15];
    5. QString serMsg;
    6. sIn = serialPort->bytesAvailable();
    7. if (sIn > 0) {
    8. if (sIn >= 10)
    9. sIn = 10;
    10. int i = serialPort->read(recBuffer, sIn);
    11. recBuffer[i] = '\0';
    12. serMsg = recBuffer;
    13. ui->label_temp->setText(serMsg);
    14. sIn = 0;
    15. serialPort->flush();
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    here is my problem:
    when i run my program, the program freezes in the first 10 seconds or more. then, i can see the the string i received. but, after a while, maybe 20 seconds, program freezes again, and waiting for anther 10 or more seconds back to normal.

    in the debug mode, the first time program processed
    Qt Code:
    1. int i = serialPort->read(recBuffer, sIn);
    To copy to clipboard, switch view to plain text mode 
    it took about 10 seconds, that's why my program froze for 10 seconds.

    it looks like the buffer waiting for full charged in the 10 seconds time.

    can anyone help me please? i am really appreciate your help and time.
    thanks in advance.
    Last edited by cooper; 1st July 2009 at 01:38.

  2. #2
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: serial port communication: unstable reading

    solved

    i need to put "unbuffered" when i open the serial port.

    Qt Code:
    1. serialPort->open(QIODevice::ReadOnly | QIODevice::Unbuffered)
    To copy to clipboard, switch view to plain text mode 

    i hope this will be helpful for anyone else

Similar Threads

  1. Serial Port communication
    By mgurbuz in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2011, 03:38
  2. Replies: 1
    Last Post: 16th June 2009, 10:09
  3. serial port same local machine
    By adamatic in forum Qt Programming
    Replies: 7
    Last Post: 3rd February 2009, 08:29
  4. accessing serial port without CONFIG += console
    By bnilsson in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2008, 22:47
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

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.