Results 1 to 1 of 1

Thread: Best Method to Plot Data Coming through a Socket.

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Best Method to Plot Data Coming through a Socket.

    Hi
    I want to plot the data which comes through socket continously.So i have created a thread which receives the data from the socket and emits it as shown in the code.
    Qt Code:
    1. void Thread::run()
    2. {
    3. QTcpSocket socket;
    4. qDebug()<<"Connecting to host";
    5. do
    6. {//Even if the server is not started keep trying
    7. socket.connectToHost(QHostAddress::LocalHost,12345);
    8. if(socket.waitForConnected(-1))
    9. break;
    10. qDebug()<<"Unable to connect to host "<<socket.errorString()<<" retrying...";
    11. }while(true);
    12.  
    13. qDebug()<<"Connected...";
    14.  
    15. while(true)
    16. {
    17. do
    18. {
    19. socket.waitForReadyRead(-1);
    20. }while(socket.bytesAvailable() < sizeof(short int));
    21.  
    22. QByteArray byteArray = socket.read(socket.bytesAvailable() - (socket.bytesAvailable()%sizeof(short int)));
    23. short int si;
    24. for(int i=0; i<(byteArray.size() / sizeof(short int)); i++)
    25. {
    26. si = *((short int *)(byteArray.constData() + i*sizeof(short int)));
    27. emit send(si);
    28. }
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    In the gui thread the signal is caught and is plotted as shown in the below code.
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindow)
    3. {
    4. ui->setupUi(this);
    5.  
    6. plot = new QwtPlot();
    7. // Disable polygon clipping
    8. QwtPainter::setDeviceClipping(false);
    9.  
    10. // We don't need the cache here
    11. plot->canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
    12. plot->canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
    13. setCentralWidget(plot);
    14. curve = new QwtPlotCurve();
    15. curve->attach(plot);
    16. memset(data,0,sizeof(data));
    17. for(int i = 0; i< HISTORY;i++)
    18. timeData[i] = i;
    19. curve->setRawData(timeData,data,HISTORY);
    20. plot->setAxisScale(QwtPlot::xBottom, timeData[0],timeData[HISTORY - 1]);
    21. thread = new Thread;
    22. connect(thread,SIGNAL(send(short)),this,SLOT(receive(short)));
    23. thread->start();
    24. }
    25.  
    26. MainWindow::~MainWindow()
    27. {
    28. delete ui;
    29. }
    30.  
    31. void MainWindow::receive(short si)//This is the slot which receives and updates the plot.
    32. {
    33. for(int i = 0; i < HISTORY-1; ++i)
    34. data[i] = data[i+1];
    35. data[HISTORY - 1] = si;
    36.  
    37. plot->replot();
    38. QCoreApplication::processEvents();
    39. }
    To copy to clipboard, switch view to plain text mode 

    But the above program gets crashed after some time of plotting .What is the bug in above code that causes the crash.

    Should i follow the dataplot example in which a timerEvent is used for plotting ,or is there any other method available.

    Thanks in advance.
    Last edited by babu198649; 29th May 2009 at 08:08.

Similar Threads

  1. Replies: 12
    Last Post: 31st October 2010, 17:08
  2. Saving pure plot data to image file
    By Debilski in forum Qwt
    Replies: 4
    Last Post: 7th April 2009, 17:02
  3. Replies: 6
    Last Post: 26th March 2009, 05:45
  4. Spectrogram Plot Hints
    By shargath in forum Qwt
    Replies: 2
    Last Post: 25th February 2009, 11:11
  5. Replies: 15
    Last Post: 27th May 2008, 01:46

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.