Results 1 to 2 of 2

Thread: QFileSystemWatcher used for a Qwt "Real_time" plot

Hybrid View

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

    Default QFileSystemWatcher used for a Qwt "Real_time" plot

    Hi,

    I work on Windows XP, and I want to plot a signal coming from a device in real-time(approx.). 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 new data just written and close the file.

    Here's the class I use to get the data in my text file, with the QSystemFileWatcher:

    data.cpp:

    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 each "length 13" line of the text file in a data buffer. This data buffer
    4. //is passed to another class (plot) who take cares of displaying these datas on a qwt plot.
    5.  
    6. #include "data.h"
    7. Data::Data(double * &_voies, QObject*p):
    8. {
    9. _voies = voies= new double[2];
    10. file_name = "Portmon.log";
    11. m_position =0;
    12. m_num = 0;
    13.  
    14. //Find the last length 13 in the text file, so we can find the
    15. //start position of the text file cursor (m_position)
    16.  
    17. ifstream file("Portmon.log");
    18.  
    19. while(getline(file,ligne))
    20. {
    21. string::size_type loc = line.find("Length 13:",0);
    22. if (loc != string::npos)
    23. m_position=file.tellg();
    24.  
    25. }
    26. file.close();
    27.  
    28.  
    29.  
    30. }
    31.  
    32. //--------------------------------------------------------------------------------
    33. //void Data::on_watcher()
    34. // Implement a QFileSystemWatcher with a fileChanged signal
    35.  
    36. void Data::on_watcher()
    37. {
    38. m_watcher = new QFileSystemWatcher();
    39. m_watcher->addPath(file_name);
    40. QObject::connect(m_watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(onFileChanged(const QString&)));
    41. }
    42.  
    43.  
    44. //-----------------------------------------------------------------------------
    45. //void Data::onFileChanged( const QString& file_name)
    46. //When the file is detected as changed,
    47. //the text file is opened, the data forming the signal/
    48. // is extracted, and the file is closed.
    49.  
    50. void Data::onFileChanged( const QString& file_name)
    51. {
    52. int nb_data =0;
    53. double perSec=1;
    54. ifstream file("Portmon.log");
    55. file.clear();
    56. file.seekg(m_position,ios::beg);
    57. while(getline(file,ligne))
    58. {
    59. string::size_type loc = line.find("Length 13:",0);
    60.  
    61. if (loc != string::npos)
    62. {
    63. // Data collecting
    64. contractions_hex = ligne.substr (loc+14,2);
    65. valeur_cardio_connecte = ligne.substr(loc+26,2);
    66. //position of the file cursor uptaded
    67. m_position = file.tellg();
    68. //Conversion from hexadecimal to int.
    69. std::istringstream iss (contractions_hex);
    70. iss >> std::hex >> m_num;
    71. double y1 =m_num/2;
    72. double time = nb_data*perSec;
    73. //Adding data to the data buffer, another class is in charge of displaying the data on plot:
    74. voies[0]=time;
    75. voies[1]=y1;
    76. nb_data++;
    77. }
    78. }
    79. file.close();
    80. }
    To copy to clipboard, switch view to plain text mode 


    data.h

    Qt Code:
    1. #ifndef DATA_H
    2. #define DATA_H
    3. #include <QtGui>
    4. #include <QFileSystemWatcher>
    5. #include <QString>
    6. #include <QFile>
    7. #include <string>
    8. #include <iostream>
    9. #include <sstream>
    10. #include <fstream>
    11. #include <QFileInfo>
    12.  
    13. using namespace::std;
    14.  
    15. class Data:
    16.  
    17. public QObject
    18.  
    19. {
    20.  
    21. Q_OBJECT
    22.  
    23. public: Data(double* &_voies,QObject*p=NULL);
    24. void on_watcher();
    25.  
    26. private:
    27. QFileSystemWatcher *m_watcher;
    28. QFile file;
    29. QString file_name;
    30. double *voies;
    31. int m_position;
    32. int m_num;
    33. std::string line;
    34. std::string contractions_hex;
    35. std::string valeur_cardio_connecte;
    36.  
    37. public slots:
    38. void onFileChanged(const QString& file_name);
    39. };
    40.  
    41. #endif DATA_H
    To copy to clipboard, switch view to plain text mode 
    Here's an example at of a text file (Portmon.log):

    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 1202
    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; 25th June 2010 at 12:05.

  2. #2
    Join Date
    Mar 2009
    Location
    Tennessee, US
    Posts
    41
    Thanks
    3
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QFileSystemWatcher used for a Qwt "Real_time" plot

    I don't think that QFileSystemWatcher is going to get you where you need to go.

    Instead of your current approach, you could try:

    1. Just open the file using QFile as read-only and them simply check when the file is bigger. If you keep a position of your last read, you can simply start reading from where you last read and read to the bottom of the file. If something new is read in, then output that to the plot. You can do this in a while loop.

    2. A better programming approach is to output the data to a local network socket or pipe which will be easier to use built-in Qt class like QTcpSocket or QSocketNotifier.

    Hope that helps.
    Amos
    Qt Programmer Extraordinaire

    Current Work:
    Ripxx Sports Measurement Device - www.ripxx.com
    (Featured in MYTHBUSTERS on 2010-05-19 in S08E08)

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 21:46
  2. Replies: 3
    Last Post: 15th February 2010, 17:27
  3. Replies: 3
    Last Post: 8th July 2008, 19:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

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
  •  
Qt is a trademark of The Qt Company.