Results 1 to 17 of 17

Thread: Plotting of QProcess output taking about 5 minutes to plot single value.

  1. #1
    Join Date
    Jan 2017
    Posts
    13
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Android

    Default Plotting of QProcess output taking about 5 minutes to plot single value.

    Hello all ,
    I am new to Qt , i am designing a system which will receive values from sensors and Plot in GUI. I have complete C code for serial port setting and data Processing . By using Q Process i am running this C program and Plotting the Processed values .
    My GUI contain a text Edit for displaying Proceeded output readings and widget for plotting processed values with respect to time . Output values and Plot are as expected . Device sending with 4800 Baud rate and with odd parity , which is set in C program .But its taking more than 5 Minutes to read and plot one value and taking more than 30 minutes to read and plot about 6 values . why it taking this much time to read ?

    My Code is attached ...

    Qt Code:
    1. ui->setupUi(this);
    2. QVector<double> data_x(101), data_y(101);
    3. timer.start();
    4. init_port();
    5. init_line_plot();
    6. x_position = 0;
    7. }
    8.  
    9. graphwidget::~graphwidget()
    10. {
    11. delete ui;
    12. }
    13. void graphwidget::init_port()
    14. {
    15. process = new QProcess(this);
    16. connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(receive()));
    17. process->start("./test2");
    18.  
    19. process->waitForStarted();
    20. qDebug()<<"process error code:" <<process->error();
    21.  
    22.  
    23. }
    24. void graphwidget::init_line_plot()
    25. {
    26.  
    27. ui->customPlot->addGraph();
    28. // ui->customPlot->setMinimumSize(500,500);
    29. ui->customPlot->xAxis->setLabel("t");
    30. ui->customPlot->yAxis->setLabel("V");
    31. ui->customPlot->axisRect()->setAutoMargins(QCP::msNone);
    32. ui->customPlot->axisRect()->setMargins(QMargins(0,0,0,0));
    33. }
    34.  
    35.  
    36. void graphwidget::receive()
    37. {
    38. // recieves data as ASCII string
    39. int datalength = 1000;
    40. char data [1000];
    41. int bytesRead =process->readLine(data, datalength);
    42. data[bytesRead]='\0';
    43.  
    44. ui->textEdit->append(QString(data));
    45.  
    46. QTextStream out(stdout);
    47. out << data << endl;
    48. addDataPoint(atof(data));
    49. }
    50.  
    51. void graphwidget::addDataPoint(double datapoint)
    52. {
    53. if (x_position>60)data_x.pop_front();
    54. double ms = timer.elapsed();
    55. data_x.push_back((double)ms/1000);
    56. x_position++;
    57. if (x_position>60) data_y.pop_front();
    58. data_y.push_back(datapoint);
    59.  
    60. ui->customPlot->graph(0)->setData(data_x,data_y);
    61. ui->customPlot->xAxis->grid()->setSubGridVisible(false);
    62. ui->customPlot->yAxis->grid()->setSubGridVisible(false);
    63. ui->customPlot->xAxis->grid()->setVisible(false);
    64. ui->customPlot->yAxis->grid()->setVisible(false);
    65. ui->customPlot->graph(0)->setPen(QPen(QColor(0,200,0)));
    66. ui->customPlot->setBackground(Qt::black);
    67.  
    68. ui->customPlot->graph(0)->rescaleAxes();
    69. ui->customPlot->replot();
    70. }
    To copy to clipboard, switch view to plain text mode 



    please suggest necessary changes ........
    Last edited by anda_skoa; 20th February 2017 at 12:46. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    data_x and data_y in line 2 are local stack variables (I assume this is your class constructor) and have no relationship to the variables of the same name you use later.

    In line 56, you increment x_position. I don't see anywhere where you reset its value, so once it reaches 60, it will continue to increase.

    Otherwise, there doesn't appear to be anything in the code you have posted which would cause such delays. I would write a test program that strips out everything except the communication between your sensors and your monitoring program and see if that is where the trouble starts. No GUI, no writing to files, no plotting, just qDebug() statements to write what you receive to the console. And run it in the debugger, of course.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    suhairkp (21st February 2017)

  4. #3
    Join Date
    Jan 2017
    Posts
    13
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Android

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    k. But i tested same qt program with simple C program . it will just print 1,2,3,4,5,4,3,2,1... and no serial port setting in C program , it worked fine and giving continues output without delay .

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    Quote Originally Posted by suhairkp View Post
    k. But i tested same qt program with simple C program . it will just print 1,2,3,4,5,4,3,2,1... and no serial port setting in C program , it worked fine and giving continues output without delay .
    So the problem is with the serial port. Why You don't use QSerialPort ?

  6. #5
    Join Date
    Jan 2017
    Posts
    13
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Android

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    Serial port is set in C program , so calling QProcess will set serial port . thats y not using Qserialport in Qt .Also , C program process the received signal from serial port and print output . this output is storing in byte array and using for plot .
    I compiled C program from terminal and getting correct output in terminal . but Qt taking more time call QProcess and plot , don't getting idea where this delay occurring ...

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    I still think you need to break the problem down into the smallest piece possible, than add more features. I would start by writing a Qt program that simply starts the listener process, reads the output from your sensor and prints it using qDebug(). No printing it to a text edit, no logging to files, no plotting. At this point, you have no idea which step in this chain is going wrong, so eliminate everything except the first one: getting data successfully from the QProcess into your program. Once that seems to be working in the same way as your C program, then you know the communication works, so add the graphics and other features.

    You might also try using QProcess:readAllStandardOutput() to get all of the available data at once instead of using readLine(). Use QByteArray::data() or QByteArray::constData() to get the NULL-terminated C string it contains. This way you do not have to worry about the buffer size or whether you have read it all.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. The following user says thank you to d_stranz for this useful post:

    suhairkp (23rd February 2017)

  9. #7
    Join Date
    Jan 2017
    Posts
    13
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Android

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    k . sir. I am testing it without plot . in my above code , it displaying "process error code : 2 " in Application output area of Qt Creator , when output gui display open and saying " Qprocess : Destroyed while process is still running ". when i changed process->waitForStarted from process->waitForFinished , it show that "process error code : 5 "
    Last edited by suhairkp; 23rd February 2017 at 12:10.

  10. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    Once again : why You use external program to read data from serial port ? Why You don't do it in Qt program ?

  11. #9
    Join Date
    Jan 2017
    Posts
    13
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Android

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    I have complete C code for processing different sensor value . there are 4 sensors and it takes about two years to complete this C code . this system working perfectly using frame buffer . Now i want to design GUI using Qt . If i am going to write complete code in Qt , sometime it will take more time . To reduce this time , i am using Q Process .


    Added after 16 minutes:


    I tested Code without plotting. Output of QProces is reading using QProcess:readAllStandardOutput() and qDebug() will print output .


    Qt Code:
    1. ui->setupUi(this)
    2. init_port();
    3. }
    4.  
    5. graphwidget::~graphwidget()
    6. {
    7. delete ui;
    8. }
    9. void graphwidget::init_port()
    10. {
    11. process = new QProcess(this);
    12. connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(receive()));
    13. process->start("./test2");
    14.  
    15. process->waitForReadyRead();
    16. qDebug()<<"process error code:" <<process->error();
    17. }
    18.  
    19. void graphwidget::receive()
    20. {
    21. QString data = process ->readAllStandardoutput();
    22. qDebug << data ;
    23.  
    24. QTextStream out(stdout);
    25. out << data << endl;
    26. }
    To copy to clipboard, switch view to plain text mode 

    it also displaying "process error code : 2" and taking more time to print .After 4 minutes , it print about 50 values then stopped . I googled about process error code , didn't get any details about .
    Last edited by suhairkp; 23rd February 2017 at 14:46.

  12. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    Just look in the Qt documentation: a value of 2 means The last waitFor...() function timed out. The state of QProcess is unchanged, and you can try calling waitFor...() again..

  13. #11
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    Is your C program writing its output to stdout or stderr and are you reading the correct channel? Look at QProcess:setProcessChannelMode and perhaps use QProcess::MergedChannels if you want both stdout and stderr to be merged, etc.

    Since you are timing out waiting to read output from the C program, perhaps your C program is writing its output to stderr?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  14. #12
    Join Date
    Jan 2017
    Posts
    13
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Android

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    Quote Originally Posted by Lesiok View Post
    Just look in the Qt documentation: a value of 2 means The last waitFor...() function timed out. The state of QProcess is unchanged, and you can try calling waitFor...() again..
    is it is by adding waitFor...() many times?


    Added after 5 minutes:


    Quote Originally Posted by jefftee View Post
    Is your C program writing its output to stdout or stderr and are you reading the correct channel? Look at QProcess:setProcessChannelMode and perhaps use QProcess::MergedChannels if you want both stdout and stderr to be merged, etc.

    Since you are timing out waiting to read output from the C program, perhaps your C program is writing its output to stderr?
    i used printf to write output from Qprocess. so i used QProcess::readAllStandardoutput().
    Last edited by suhairkp; 24th February 2017 at 05:31.

  15. #13
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    Quote Originally Posted by suhairkp View Post
    i used printf to write output from Qprocess. so i used QProcess::readAllStandardoutput().
    And do you have a newline at the end of your output and are you flushing stdout like so: fflush(stdout)?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  16. #14
    Join Date
    Jan 2017
    Posts
    13
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Android

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    s. each output values are in new line . but not flushing stdout.

  17. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    process->start("./test2");
    So if you open a terminal window, cd to this directory, and execute "./test2", you see the expected output immediately in the terminal window? No delay at all?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  18. #16
    Join Date
    Jan 2017
    Posts
    13
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Android

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    s... getting currect output without delay in terminal , but QProcess taking more time to print out.

  19. #17
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Plotting of QProcess output taking about 5 minutes to plot single value.

    I just noticed that the initial code you posted seems to omit the function definition that should preceed line 1. It would appear that this is your graphwidget constructor, which I presume is based on QObject and probably allocated on the stack in main.

    In that same constructor, you are starting the timer and executing those init* methods, which then actually creates and starts your QProcess, all before you have started the message loop.

    I would recommend that you take the processing logic out of the constructor and put into a separate init method, so that you execute the initialization *after* your graphwidget object is fully constructed and then you can being your processing.

    For example, add a slot to graphwidget named init() or whatever you want to name it, then make the following changes to graphwidget (untested):
    Qt Code:
    1. graphwidget::graphwidget(QObject *parent)
    2. {
    3. ui->setupUi(this);
    4. x_position = 0;
    5. QTimer::singleShot(0, this, SLOT(init));
    6. }
    7.  
    8. void graphwidget::init()
    9. {
    10. timer.start();
    11. init_port();
    12. init_line_plot();
    13. }
    To copy to clipboard, switch view to plain text mode 
    This way, your graphwidget will be completely constructed and the singleshot timer will execute your init routine as soon as the event loop is started, etc. While I can't guarantee this is your problem, it's easy to try...
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  20. The following user says thank you to jefftee for this useful post:

    suhairkp (3rd March 2017)

Similar Threads

  1. How to Plot from QProcess output
    By suhairkp in forum Newbie
    Replies: 0
    Last Post: 31st January 2017, 15:12
  2. Can I read output of a single with the help of Mingw Compiler?
    By parulkalra14 in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2014, 11:03
  3. Replies: 2
    Last Post: 28th January 2014, 10:02
  4. Problem in plotting 3D plot using QWT
    By johnMick in forum Newbie
    Replies: 1
    Last Post: 15th July 2011, 12:19
  5. Replies: 1
    Last Post: 9th September 2010, 11:22

Tags for this Thread

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.