PDA

View Full Version : Strange problem with timer



nagabathula
12th April 2011, 10:44
Hello friends i have small problem. i have finished a small project. where i retrieve some data from RS232 interface and save that data to the data base and the display the converted data in a QTableView. Now my problem is the program retrieves data properly for a while and then the data retrieval is stopped. According to what i have noticed it stops after updating around 20 rows into the data base. I looked into the code so much but cud't figure out where i am going wrong. not able to know why the data from the port is not retrieved after some duration.

this is the code where i am retrieving the data from the port, i am calling this function every 120 mili seconds when the user clicks Acquire Button, i am getting huge data some 700 channels of data every 1.6 seconds.


void datareceiver::myexport1()
{
qint64 i;
char buff[4096];
QByteArray result;
quint16 r;

if(port->bytesAvailable())
{
quint16 r;
inscnt++;
numBytes = port->bytesAvailable();
data = port->read(numBytes % 2 ? numBytes : numBytes-1);
newdata.append(data);
// Save the data in newdata into data base after data manipulation

}
else
{

switch( QMessageBox::warning( this,","RS232 Interface is not connected properly. Try to re-connection again. \n \n Do you want to continue." ,
"Yes", "No"/*, "Leave Anyway"*/, 0, 1 ) )
{
case 0:
break;
case 1:
exit(1);
break;

}
QSound::play("notify.wav");
qLed->setColor(Qt::red);
label_3->setText("LINK FAILURE");
pushButton_6->setDisabled(false);
}

I am assuming that the timer is stopped but there is no event where i am stopping the timer is stopped.
Do you find any problem in this part of the code that this might be happening.

Added after 1 21 minutes:

Thank you

some one pls help me out with this problem, just not able to figure out where i am going wrong why the data retrieval. It is not the problem of the Timer its some problem with the port or the way i am reading the data from the serial port. Do you find any mistakes in this part of the code.??

Thank you

nagabathula
12th April 2011, 12:43
hello some one pls tell me if i am doing something wrong in the data receiving part.. Cause after a while the data from the serial port stops..

regards

high_flyer
12th April 2011, 13:07
I am assuming that the timer is stopped
run the application in a debuger, until you stop getting data.
Then put a break point in the slot connected to the timer.
If the break point gets caught, then you know its not the timer.
Continue to step through until you see which part of the code that is supposed to run is not being called.

nagabathula
12th April 2011, 14:53
hello thank you for the reply.. I did try that there is no problem with the timer. I put a counter in the loop it kept incrementing even after the data stopped. So i think there is some problem in this part of the code. The data flow into the serial port from the device is continuous with out any break, but my program stop acquiring the data after some undefined period of time. This program will have to run for 10 hours at stretch sometimes but right now it stops after some 10 or 155 minutes.


if(port->bytesAvailable())
{
quint16 r;
inscnt++;
numBytes = port->bytesAvailable();
data = port->read(numBytes % 2 ? numBytes : numBytes-1);
newdata.append(data);

// The data present in newdata is manipulated and inserted into data base here

}
i think there is some problem in this part of the code. do you see any mistake here in the above code. I am trying to retrieve data from a serial port. I am calling this function with a timer every 120 milli seconds.

Regards

wysota
12th April 2011, 15:47
The code looks fine. Why do you think the problem is here?

nagabathula
12th April 2011, 16:11
Hello i think the problem is in that place because i am not finding what else can go wrong in the function that the data retrieval stops. This is the whole function where i am receiving and saving data into the data base. I call it every 120 milli seconds once the user clicks the acquire button.


void datareceiver::myexport1()
{
qint64 numBytes ;
char buff[4096];
QByteArray result;
quint16 r;
if(port->bytesAvailable())
{
inscnt++;
numBytes = port->bytesAvailable();
data = port->read(numBytes % 2 ? numBytes : numBytes-1);
newdata.append(data);
}

I feel the program is correct. Cause every thing works fine the only problem i have now is that the data received from the serial port stops all of a sudden after a while.

thank you

high_flyer
12th April 2011, 16:25
Hello i think the problem is in that place because i am not finding what else can go wrong in the function that the data retrieval stops

I feel the program is correct.

Look, programming is not about "feelings" or because you can't see where the problem is in one place, hence assume its in another.
You have to check.
Just like in the first post you thought the problem was with the timer, and after checking, you saw your "feeling" was wrong.

The only way to know where the problem is is to debug the application.
Once you stop getting data, put a break point, and continue stepping through until you get to the part of the code that is NOT doing what you expect.

wysota
12th April 2011, 20:16
So does the function run? Does the flow enter the if block? Does "data" contain the data you need? Debug your app as already asked for and answer those questions. Just please keep your feelings away from it, give us facts.

nagabathula
13th April 2011, 08:43
Hello thank you both for the suggestions. I did test the program a lot from my analysis i find the program takes more memory as time passes. When i initially start the program the cpu usage is 17% as time goes by may be after ten mins it increases after half an hous it goes upto 60 % to 90 % some times thats when the data stops. I have put a else statement if there are no port->bytesAvailable(); then i pop up a window telling link failure. This also does't happen when the data stops so there is data available in the port but it is not getting through.
I doubt this part of the code where i am doing some aligning of data.
this part of the code is happening right after i append the data i retrieve from the port into the QByteArray newdata.



// Left Bit shifting the first Byte by 5 times and ORing it with the second Byte and append that data into the Byte Array result.
for (int i = 0; i < newdata.size(); i+=2)
{
r = (((quint16) newdata.at(i) << 5) |(newdata.at(i+1)));
char *ptrR = (char*) &r;
result.append(ptrR[1]);
result.append(ptrR[0]);
//delete[] ptrR;
}
hexdatach = result.toHex();

this is the only part of code where pointers are used the rest of the code i am using QString , QVector, and QByteArray containers for all my operations. Can you guide me to how i delete the char* ptrR. i tried delete[] ptrR;but it breaks the program as soon as i click the acquire button, thats when i call this funtion..

thank you

wysota
13th April 2011, 09:25
Hello thank you both for the suggestions. I did test the program a lot from my analysis i find the program takes more memory as time passes.
How did you come to this conclusion?


When i initially start the program the cpu usage is 17%
How did you measure that? 17% of what?

Why do you refuse to debug your application properly? We can write 100 posts here about it and reach no conclusion or you can just debug your app and have an answer within two minutes. The choice is yours.

nagabathula
13th April 2011, 09:35
i did use the qDebug to see whats in the variables but it din't help me much.. I opened windows task manager before the program started. and monitored the cpu usage after i started the application i noticed the program stops acquiring data once the cpu usage reach's 70% . I am using qt integrated with visual studio 6.0 can you please tell me how we debug a application..

thank you

wysota
13th April 2011, 09:48
i did use the qDebug to see whats in the variables but it din't help me much.
Meaning?

I opened windows task manager before the program started. and monitored the cpu usage after i started the application
CPU usage is always 100%. The fact that the monitor shows your program is consuming 17% of that only means that the remaining processes use 83%. It doesn't tell you anything about how busy your program is. When the OS sees your process is quite active, it is being scheduled more often thus the relative cpu usage increases.


am using qt integrated with visual studio 6.0 can you please tell me how we debug a application..
You find a menu entry that says "Start debugging" and click it. Then you stop your app when you see abnormal behaviour, you set breakpoints and start observing how your program behaves. In doubt google for more detailed information about debugging under VS.

nagabathula
13th April 2011, 10:13
i used qDebug() to see what is present in all the arrays and Frame Id counters one by one what happens to the array when data stops. I did have a look at debugger but i am not able to understand it properly yet. i am still reading about debugging in google.