PDA

View Full Version : Maintaining the delay between sending and reading the data



jjbabu
7th December 2007, 07:35
Hai,
In the bellow application am facing the delay problem.

I will explain clearly what my functionality requires--
Using the bellow function i will get the some data from the micro controller using the serial port and i will display that particular data on my GUI.

Initially i will send " : 7 0" code data to the Micro controller,using that code data micro controller will send some data,which i want to read and show on my GUI.

I gave some delay(using dummy for loops) between sending code data(": 7 0") and receiving controller data.
The for loops causes the problem to me,because the delay reduces with the CPU configuration.without delay my application unable to read entire data which i required.

plz assist me to overcome this problem.

one more thing i required is how to take the data on to QBytearray instead of taking on to the buffer,while reading the data.

i desperately need your suggestions.

Thanx alot.

void Synth1::get() // recieving synth lock detects from RC
{
port->open(QIODevice::ReadWrite);
port->write(QString::QString(":").toAscii());
for(long int i=0;i<=10000000;i++);
port->write(QString::QString(" ").toAscii());
port->write(QString::number(7).toAscii());
port->write(QString::QString(" ").toAscii());
for(long int i=0;i<=10000000;i++);
for(long int i=0;i<=10000000;i++);
port->write(QString::number(0).toAscii());
port->write(QString::QString(" ").toAscii());

for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
char buff[2048];
int numBytes;
numBytes = port->bytesAvailable();
if(numBytes > 0)
{
if(numBytes > 2048)
numBytes = 2048;
int i = port->read(buff, numBytes);
buff[i] = '\0';
QString msg = buff;
QString y = ": 7 ";
int j=msg.indexOf(y,0);
msg.remove(0,j);
ui.lineEdit_13->clear();
ui.lineEdit_13->insert(msg);
QStringList fields =msg.split(" ");
if(fields.size()==16)
{
QString str,id2,sum2,sy1,sy2,sy3,sy4,sy5,sy6,sy7,sy8,sy9,s y10,sy11,sy12;
str=fields[0];
id2=fields[1];
sum2=fields[2];
sy1=fields[3];
sy2=fields[4];
sy3=fields[5];
sy4=fields[6];
sy5=fields[7];
sy6=fields[8];
sy7=fields[9];
sy8=fields[10];
sy9=fields[11];
sy10=fields[12];
sy11=fields[13];
sy12=fields[14];
if(id2=="7")
{
ui.lineEdit->clear();
ui.lineEdit_2->clear();
ui.lineEdit_3->clear();
ui.lineEdit_4->clear();
ui.lineEdit_5->clear();
ui.lineEdit_6->clear();
ui.lineEdit_7->clear();
ui.lineEdit_8->clear();
ui.lineEdit_9->clear();
ui.lineEdit_10->clear();
ui.lineEdit_11->clear();
ui.lineEdit_12->clear();
ui.lineEdit->insert(sy1);
ui.lineEdit_2->insert(sy2);
ui.lineEdit_3->insert(sy3);
ui.lineEdit_4->insert(sy4);
ui.lineEdit_5->insert(sy5);
ui.lineEdit_6->insert(sy6);
ui.lineEdit_7->insert(sy7);
ui.lineEdit_8->insert(sy8);
ui.lineEdit_9->insert(sy9);
ui.lineEdit_10->insert(sy10);
ui.lineEdit_11->insert(sy11);
ui.lineEdit_12->insert(sy12);
}
else
{
QMessageBox:: about(this,tr("PORT"),
tr("<b>NOT VALID PACKET</b> recieved..."));
}
}
else
{
QMessageBox:: about(this,tr("PORT"),
tr("<b>DATA</b> NOT recieved properly."));
}
}

}

MarkoSan
7th December 2007, 07:59
The for loops causes the problem to me,because the delay reduces with the CPU configuration.without delay my application unable to read entire data which i required.

What do you mean delay reduces the CPU configuration. I do not understand what do you want to say.

jpn
7th December 2007, 08:00
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);
for(long int i=0;i<=100000000;i++);

You shouldn't block GUI applications' event loop with busy loops like these. You should use for example QTimer to schedule a slot to be called at certain delay. This makes it possible for the application to process its events meanwhile.

MarkoSan
7th December 2007, 08:09
Or better, you can read data on its own thread.