PDA

View Full Version : QFileSystemWatcher with a Qwt Real-time plot



gen_mass
24th June 2010, 23:37
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:


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//The following class (data) is used as a sampling thread with the QFileSystemWatcher.
// It puts the significant values extracted from the text file in a data buffer. This data buffer
//is passed to another class (plot) who take cares of displaing these datas.

#ifndef DATA_H
#define DATA_H

#include <QtGui>
#include <QFileSystemWatcher>
#include <QString>
#include <QFile>
#include <QVector>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <QFileInfo>


using namespace::std;

class Data: public QObject
{
Q_OBJECT

public:
Data(double* &_voies,QObject*p=NULL);
void on_watcher();
private:
QFileSystemWatcher *m_watcher;
QFile file;
QString file_name;
double *voies;
qint64 size1;
qint64 size2;
int m_position;
int m_num;
std::string line;
std::string contractions_hex;
std::string valeur_cardio_connecte;
public slots:
void onFileChanged(const QString& file_name);

};

#endif // DATA_H


data.cpp:



#include "data.h"

Data::Data(double * &_voies, QObject*p):
QObject(p)
{

_voies = voies= new double[2];

file_name = "Portmon.log";
QFile file2(file_name);
size2 = QFileInfo(file2).size();
m_position =0;
m_num = 0;

}
void Data::on_watcher()
{
m_watcher = new QFileSystemWatcher();
m_watcher->addPath(file_name);
QObject::connect(m_watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(onFileChanged(const QString&)));
}


//------------------------------------------------------------------------------
// plot::Traitement()
//When the file is detected as changed,
//the text file is opened and the data forming the signal
// is extracted
// //------------------------------------------------------------------------------

void Data::onFileChanged( const QString& file_name)
{
int nb_data =0;
double perSec=1;


ifstream file("Portmon.log");
file.clear();
file.seekg(m_position,ios::beg);
while(getline(file,ligne))
{
string::size_type loc = line.find("Length 13:",0);
if (loc != string::npos)
{

contractions_hex = ligne.substr (loc+14,2); // Data collecting
valeur_cardio_connecte = ligne.substr(loc+26,2);//
m_position = file.tellg();//position of the file cursor
std::istringstream iss (contractions_hex); //Conversion from hexadecimal to int.
iss >> std::hex >> m_num;
double y1 =m_num/2;
double temps = nb_data*perSec;
//Adding data to the data buffer, another class is in charge of displaying the data on the plot
voies[0]=temps;
voies[1]=y1;
nb_data++;

}

}

}


Here's an example at of a text file:


[\\ISTILPORT06]
1179 10:06:58 SUCCESS
1180 10:06:59 dacspy.exe IRP_MJ_READ Serial0 Length 100
1180 10:06:59 SUCCESS Length 14: 10 02 43 80 02 00 00 00 00 00 00 00 00 00
1181 10:06:59 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
1181 10:06:59 SUCCESS
1182 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
1182 10:06:59 SUCCESS
1183 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
1183 10:06:59 SUCCESS
1184 10:06:59 dacspy.exe IRP_MJ_READ Serial0 Length 100
1184 10:06:59 SUCCESS Length 14: 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1185 10:06:59 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
1185 10:06:59 SUCCESS
1186 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
1186 10:06:59 SUCCESS
1187 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
1187 10:06:59 SUCCESS
1188 10:06:59 dacspy.exe IRP_MJ_READ Serial0 Length 100
1188 10:06:59 SUCCESS Length 13: 00 0A 0B 0C 0A 00 00 08 00 10 03 92 38
1189 10:06:59 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
1189 10:06:59 SUCCESS
1190 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
1190 10:06:59 SUCCESS
1191 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
1191 10:06:59 SUCCESS
1192 10:06:59 dacspy.exe IRP_MJ_READ Serial0 Length 100
1192 10:06:59 SUCCESS Length 14: 10 02 43 80 02 00 00 00 00 00 00 00 00 00
1193 10:06:59 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
1193 10:06:59 SUCCESS
1194 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
1194 10:06:59 SUCCESS
1195 10:06:59 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
1195 10:06:59 SUCCESS
1196 10:07:00 dacspy.exe IRP_MJ_READ Serial0 Length 100
1196 10:07:00 SUCCESS Length 14: 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1197 10:07:00 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
1197 10:07:00 SUCCESS
1198 10:07:00 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
1198 10:07:00 SUCCESS
1199 10:07:00 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
1199 10:07:00 SUCCESS
1200 10:07:00 dacspy.exe IRP_MJ_READ Serial0 Length 100
1200 10:07:00 SUCCESS Length 13: 00 07 08 08 08 00 00 08 00 10 03 58 C9
1201 10:07:00 dacspy.exe IOCTL_SERIAL_GET_COMMSTATUS Serial0
1201 10:07:00 SUCCESS
1202 10:07:00 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0
1202 10:07:00 SUCCESS
1203 10:07:00 dacspy.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0


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 !!

JohannesMunk
25th June 2010, 22:28
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