PDA

View Full Version : Problem while reading the data from RS232 using Qextserialport



sudheer168
28th August 2008, 09:39
Hi friends,
I have one problem with Qextserialport , that i was unable to read and store the data to database properly.In my program i was able to read and store the data properly for some time only and after that the data was getting corrupted(ie overwriting in port I think).
Here I am receiving huge data from other system through RS232 in which it contains a data frame of 50 channels approximately ,each channel information occupies 2bytes.The data will be 50 data frames per 1sec.when I was reading and storing these channels data into database, channel wise.I am able to insert the data for 2 or 3seconds only and then after the data corrupted. for this i used QTimer to call "receiveMsg()" for every 1millisec.
But since i am new to threads i am still figuring out how to implement it in the program, Can you pls give me any suggestions on how i should implement threads in my program.I don't know how to create the new threads and own classes. I don't know how to implement the threads in my program . Where to declare and where to start the threads.
This is the code I tried with out Threads.




void QT_receive::receiveMsg()
{
QByteArray m ;
QByteArray msg ;
QString ss;
msg.clear();
char buff[2];
if(port->bytesAvailable())
{
int i= port->read(buff, 2);
msg = QByteArray::fromRawData(buff,2);
if (i!= -1)
buff[i] = '\0';
else
buff[0] = '\0';
int d = 0;
while(d<msg.count())
{
m.prepend(msg.at(d));
d++;
}
ss = m.toHex();
QString sq("INSERT INTO Bulkdb values(%1,%2)");
sq = sq.arg(rcount)
.arg("'"+ ss +"'");
m_query.exec(sq);
rcount++;
ss.clear();
m.clear();
}
}


Please Help me to solve this problem

Regards,
Sudheer

calhal
28th August 2008, 15:44
You are reading two bytes every milisecond without checking if those two bytes are really there to read.

if(port->bytesAvailable())
I guess you have here -1 most of the time, but still you're processing the data.
I think 1ms is to often, you're just wasting your OS resources.

sudheer168
29th August 2008, 07:04
Hi Calhal and all,

Thanks for your reply.Actually that is not my problem, I am new to the threads concept. So I don't know how to use the threads in my program.Here I attached the source file. It consists of two processes. First one is Receiving the data from RS232 for every 1 millisecond. And another one is to insert the data into Database at the same time. So the process is breaking, because i haven't used the multi thread concept.

So any one can help me to solve the above problem.

With Regards,

Sudheer

calhal
29th August 2008, 08:15
Actually that is not my problem, I am new to the threads concept.
Maybe you're right. But please try to change

if(port->bytesAvailable())
to this

if(port->bytesAvailable()>0)
and this

i = port->read(buff,numBytes);
into this

if ( (i = port->read(buff,numBytes) <=0 ) return;

And again why 1ms?

wysota
29th August 2008, 09:22
So the process is breaking, because i haven't used the multi thread concept.

You don't need multiple threads for that. Just increase the timer timeout as suggested - 100ms will be just as fine as 1ms. How fast is your serial port? How many bytes can it transmit during 1ms?

sudheer168
29th August 2008, 10:37
Hi Wysota and all,

Thanks for reply. The Hardware system is sending 4096 bytes per 1 millisecond. It is so fast data acquisition. So we need to receive the data of 4096 bytes and at the same time we need to insert into the Database. So 1 millisecond is not perfect time to receive and insert. If we used the Timer for receiving then some data has been missing. So for that problem the multi thread concept is better. I am new to threads. I would like to know how to create , use the threads as per our program (I attached the C++ and .h files). Help me to solve this problem.


With Regards,

Sudheer

calhal
29th August 2008, 11:08
The Hardware system is sending 4096 bytes per 1 millisecond
So you're trying to receive 4kB/ms with 115200baud port or I'm missing something?

wysota
29th August 2008, 11:24
If we used the Timer for receiving then some data has been missing.
Nothing would be missing, the data is buffered.


So for that problem the multi thread concept is better.
Of course you are aware of the fact that threads slow down your application, so you will be aquiring your data in a lower rate, right?

By the way, you want to transfer four megabytes of data per second? Some fast serial port you have there... 32 megabauds? Are you sure your calculations are correct?