Results 1 to 8 of 8

Thread: Problem while reading the data from RS232 using Qextserialport

  1. #1
    Join Date
    Aug 2008
    Posts
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Problem while reading the data from RS232 using Qextserialport

    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.

    Qt Code:
    1. void QT_receive::receiveMsg()
    2. {
    3. QByteArray msg ;
    4. QString ss;
    5. msg.clear();
    6. char buff[2];
    7. if(port->bytesAvailable())
    8. {
    9. int i= port->read(buff, 2);
    10. msg = QByteArray::fromRawData(buff,2);
    11. if (i!= -1)
    12. buff[i] = '\0';
    13. else
    14. buff[0] = '\0';
    15. int d = 0;
    16. while(d<msg.count())
    17. {
    18. m.prepend(msg.at(d));
    19. d++;
    20. }
    21. ss = m.toHex();
    22. QString sq("INSERT INTO Bulkdb values(%1,%2)");
    23. sq = sq.arg(rcount)
    24. .arg("'"+ ss +"'");
    25. m_query.exec(sq);
    26. rcount++;
    27. ss.clear();
    28. m.clear();
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    Please Help me to solve this problem

    Regards,
    Sudheer

  2. #2
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem while reading the data from RS232 using Qextserialport

    You are reading two bytes every milisecond without checking if those two bytes are really there to read.
    Qt Code:
    1. if(port->bytesAvailable())
    To copy to clipboard, switch view to plain text mode 
    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.
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  3. #3
    Join Date
    Aug 2008
    Posts
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem while reading the data from RS232 using Qextserialport

    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
    Attached Files Attached Files

  4. #4
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem while reading the data from RS232 using Qextserialport

    Actually that is not my problem, I am new to the threads concept.
    Maybe you're right. But please try to change
    Qt Code:
    1. if(port->bytesAvailable())
    To copy to clipboard, switch view to plain text mode 
    to this
    Qt Code:
    1. if(port->bytesAvailable()>0)
    To copy to clipboard, switch view to plain text mode 
    and this
    Qt Code:
    1. i = port->read(buff,numBytes);
    To copy to clipboard, switch view to plain text mode 
    into this
    Qt Code:
    1. if ( (i = port->read(buff,numBytes) <=0 ) return;
    To copy to clipboard, switch view to plain text mode 

    And again why 1ms?
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem while reading the data from RS232 using Qextserialport

    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?
    Last edited by wysota; 29th August 2008 at 10:19. Reason: Missing tag

  6. #6
    Join Date
    Aug 2008
    Posts
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem while reading the data from RS232 using Qextserialport

    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
    Attached Files Attached Files

  7. #7
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem while reading the data from RS232 using Qextserialport

    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?
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem while reading the data from RS232 using Qextserialport

    Quote Originally Posted by sudheer168 View Post
    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?

Similar Threads

  1. qextserialport BAUDRATE problem
    By dheeraj in forum Qt Programming
    Replies: 6
    Last Post: 19th March 2009, 19:29
  2. problem with reading text files
    By Axsis in forum Newbie
    Replies: 1
    Last Post: 25th April 2008, 12:29
  3. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  4. problem with reading input data in qt
    By Ahmad in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2007, 10:58
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.