Results 1 to 2 of 2

Thread: QFileSystemWatcher with a Qwt Real-time plot

  1. #1
    Join Date
    Jun 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default QFileSystemWatcher with a Qwt Real-time plot

    Hi,

    I work on Windows XP, andI want to plot a signal coming from a device. This device is connected to my PC by a RS232 serial port. I can't access directly to the data coming in my PC by the serial port, but only from a log text file.

    I know that new data enters the text file every second, but because I want to be sure of the synchronisation between the entering of data and the plotting, I try to implement a QFileSystemWatcher: Everytime the file changes, a fileChanged signal is emitted and received by a function who opens the text file, read the just entered new data and close the file.

    data.h:
    Qt Code:
    1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    2. //The following class (data) is used as a sampling thread with the QFileSystemWatcher.
    3. // It puts the significant values extracted from the text file in a data buffer. This data buffer
    4. //is passed to another class (plot) who take cares of displaing these datas.
    5.  
    6. #ifndef DATA_H
    7. #define DATA_H
    8.  
    9. #include <QtGui>
    10. #include <QFileSystemWatcher>
    11. #include <QString>
    12. #include <QFile>
    13. #include <QVector>
    14. #include <string>
    15. #include <iostream>
    16. #include <sstream>
    17. #include <fstream>
    18. #include <QFileInfo>
    19.  
    20.  
    21. using namespace::std;
    22.  
    23. class Data: public QObject
    24. {
    25. Q_OBJECT
    26.  
    27. public:
    28. Data(double* &_voies,QObject*p=NULL);
    29. void on_watcher();
    30. private:
    31. QFileSystemWatcher *m_watcher;
    32. QFile file;
    33. QString file_name;
    34. double *voies;
    35. qint64 size1;
    36. qint64 size2;
    37. int m_position;
    38. int m_num;
    39. std::string line;
    40. std::string contractions_hex;
    41. std::string valeur_cardio_connecte;
    42. public slots:
    43. void onFileChanged(const QString& file_name);
    44.  
    45. };
    46.  
    47. #endif // DATA_H
    To copy to clipboard, switch view to plain text mode 

    data.cpp:
    Qt Code:
    1. #include "data.h"
    2.  
    3. Data::Data(double * &_voies, QObject*p):
    4. {
    5.  
    6. _voies = voies= new double[2];
    7.  
    8. file_name = "Portmon.log";
    9. QFile file2(file_name);
    10. size2 = QFileInfo(file2).size();
    11. m_position =0;
    12. m_num = 0;
    13.  
    14. }
    15. void Data::on_watcher()
    16. {
    17. m_watcher = new QFileSystemWatcher();
    18. m_watcher->addPath(file_name);
    19. QObject::connect(m_watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(onFileChanged(const QString&)));
    20. }
    21.  
    22.  
    23. //------------------------------------------------------------------------------
    24. // plot::Traitement()
    25. //When the file is detected as changed,
    26. //the text file is opened and the data forming the signal
    27. // is extracted
    28. // //------------------------------------------------------------------------------
    29.  
    30. void Data::onFileChanged( const QString& file_name)
    31. {
    32. int nb_data =0;
    33. double perSec=1;
    34.  
    35.  
    36. ifstream file("Portmon.log");
    37. file.clear();
    38. file.seekg(m_position,ios::beg);
    39. while(getline(file,ligne))
    40. {
    41. string::size_type loc = line.find("Length 13:",0);
    42. if (loc != string::npos)
    43. {
    44.  
    45. contractions_hex = ligne.substr (loc+14,2); // Data collecting
    46. valeur_cardio_connecte = ligne.substr(loc+26,2);//
    47. m_position = file.tellg();//position of the file cursor
    48. std::istringstream iss (contractions_hex); //Conversion from hexadecimal to int.
    49. iss >> std::hex >> m_num;
    50. double y1 =m_num/2;
    51. double temps = nb_data*perSec;
    52. //Adding data to the data buffer, another class is in charge of displaying the data on the plot
    53. voies[0]=temps;
    54. voies[1]=y1;
    55. nb_data++;
    56.  
    57. }
    58.  
    59. }
    60.  
    61. }
    To copy to clipboard, switch view to plain text mode 

    Here's an example at of a text file:

    Qt Code:
    1. [\\ISTILPORT06]
    2. 1179 10:06:58 SUCCESS
    3. 1180 10:06:59 dacspy.exe IRP_MJ_READ Serial0 Length 100
    4. 1180 10:06:59 SUCCESS Length 14: 10 02 43 80 02 00 00 00 00 00 00 00 00 00
    5. 1181 10:06:59 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
    6. 1181 10:06:59 SUCCESS
    7. 1182 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    8. 1182 10:06:59 SUCCESS
    9. 1183 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    10. 1183 10:06:59 SUCCESS
    11. 1184 10:06:59 dacspy.exe IRP_MJ_READ Serial0 Length 100
    12. 1184 10:06:59 SUCCESS Length 14: 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    13. 1185 10:06:59 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
    14. 1185 10:06:59 SUCCESS
    15. 1186 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    16. 1186 10:06:59 SUCCESS
    17. 1187 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    18. 1187 10:06:59 SUCCESS
    19. 1188 10:06:59 dacspy.exe IRP_MJ_READ Serial0 Length 100
    20. 1188 10:06:59 SUCCESS Length 13: 00 0A 0B 0C 0A 00 00 08 00 10 03 92 38
    21. 1189 10:06:59 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
    22. 1189 10:06:59 SUCCESS
    23. 1190 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    24. 1190 10:06:59 SUCCESS
    25. 1191 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    26. 1191 10:06:59 SUCCESS
    27. 1192 10:06:59 dacspy.exe IRP_MJ_READ Serial0 Length 100
    28. 1192 10:06:59 SUCCESS Length 14: 10 02 43 80 02 00 00 00 00 00 00 00 00 00
    29. 1193 10:06:59 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
    30. 1193 10:06:59 SUCCESS
    31. 1194 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    32. 1194 10:06:59 SUCCESS
    33. 1195 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    34. 1195 10:06:59 SUCCESS
    35. 1196 10:07:00 dacspy.exe IRP_MJ_READ Serial0 Length 100
    36. 1196 10:07:00 SUCCESS Length 14: 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    37. 1197 10:07:00 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
    38. 1197 10:07:00 SUCCESS
    39. 1198 10:07:00 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    40. 1198 10:07:00 SUCCESS
    41. 1199 10:07:00 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    42. 1199 10:07:00 SUCCESS
    43. 1200 10:07:00 dacspy.exe IRP_MJ_READ Serial0 Length 100
    44. 1200 10:07:00 SUCCESS Length 13: 00 07 08 08 08 00 00 08 00 10 03 58 C9
    45. 1201 10:07:00 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
    46. 1201 10:07:00 SUCCESS
    47. 1202 10:07:00 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    48. 1202 10:07:00 SUCCESS
    49. 1203 10:07:00 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
    To copy to clipboard, switch view to plain text mode 


    When I try to run this code, no data is added to the plot, and I get an empty plot.

    I know that the problem is with the QFileSystemWatcher because everything else has been tested and debugged before.
    I have put my text file in the debug file of my project (I'm in debug mode) and my whole project including Qt Creator implementation is on an external disk (:G), I don't know if that changes something..

    Thanks for your help !!
    Last edited by gen_mass; 24th June 2010 at 23:12.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFileSystemWatcher with a Qwt Real-time plot

    Reduce the problems complexity. Don't put the stuff into a graph but write a message to the debug output if you get a file change notification. That way you make sure its not your output that is a problem!

    If nothing is triggered, try to get some events when you change the log file manually...

    HIH

    Johannes

Similar Threads

  1. real time plotting
    By agostain in forum Qwt
    Replies: 0
    Last Post: 10th August 2009, 10:47
  2. Best way in Qt to plot curve per real-time reading?
    By Sheng in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2009, 22:33
  3. PlotWin with scroll on real time
    By looki in forum Qwt
    Replies: 5
    Last Post: 8th August 2008, 15:40
  4. real time plotting
    By gyre in forum Qwt
    Replies: 4
    Last Post: 11th December 2007, 16:13
  5. Rendering real time video using SDL and QT
    By venk2ksubbu in forum Newbie
    Replies: 4
    Last Post: 13th September 2007, 15:20

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.