Results 1 to 15 of 15

Thread: readrawdata' s read size

  1. #1
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Question readrawdata' s read size

    Hello friends,

    I am trying to send serial data over com port. i am using qextserialport for serialport communication.

    I thought that most suitable way for me is to use QDatastream . As below, when i clicked the send button, some part(1944 bytes ) of avatar.png is sent.

    I say "some part of" ; because for the values more than 1944, i get an error about memcpy. I tried this on another machine; this time instead of 1944, at most 1976 bytes were being send without an error..

    What is the problem with it?

    Qt Code:
    1. void MainWindow::on_sendButton_clicked()
    2. {
    3. QFile file("C:/avatar.png");
    4. QDataStream out(&file);
    5.  
    6. if (!file.open(QIODevice::ReadOnly)) {
    7. cerr << "Cannot open file for writing: "
    8. << qPrintable(file.errorString()) << endl;
    9. return;
    10. }
    11.  
    12. char *datas = new char();
    13. out.readRawData(datas,1944);
    14. sp->send(1944,datas);
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: readrawdata' s read size

    It's pretty obvious:
    Qt Code:
    1. char* datas = new char();
    To copy to clipboard, switch view to plain text mode 
    allocates a SINGLE char. So it would crash no matter on what machine you run it. It depends on how much the heap is fragmented. In your particular case, there was an unallocated hole of 1944 bytes starting at the address pointed to by datas.

    You should read the file in buffers.
    Last edited by marcel; 21st January 2008 at 11:57. Reason: spelling error

  3. #3
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: readrawdata' s read size

    Thanks Marcel,

    But valid arguements for readRawData are (char * s, int len) , how will read a buffer into QDatastream..
    and also my send function (actually write(const char *data, qint64 len) ) has arguements : const char *data, qint64 len

    How can i adopt all these?
    I am a new programmer , so be tolerant toward me

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: readrawdata' s read size

    Try this:
    Qt Code:
    1. int maxSize = 1024;
    2. char* datas = new char[maxSize];
    3. while(!out->atEnd())
    4. {
    5. int actuallyRead = out.readRawData(datas, maxSize);
    6. sp->send(actuallyRead, datas);
    7. }
    8. delete[] datas;
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to marcel for this useful post:

    yagabey (22nd January 2008)

  6. #5
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Thumbs up Re: readrawdata' s read size

    Ok thanks, it works correctly now!..

  7. #6
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: readrawdata' s read size

    A small qestion more:

    How can this code progress to the next 1024 bytes, after reading first 1024. Does atEnd() do it for us?

    Qt Code:
    1. int maxSize = 1024;
    2. char* datas = new char[maxSize];
    3.  
    4. while(!out->atEnd()) //checks the end of file
    5. {
    6. int actuallyRead = out.readRawData(datas, maxSize); //counts read bytes
    7. sp->send(actuallyRead, datas); //send the data
    8. }
    9. delete[] datas;
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: readrawdata' s read size

    No, readRawData advances the iodevice marker.

  9. #8
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: readrawdata' s read size

    Hmmm ok I got it...

    in the datalistener side, i used a similar code:

    Qt Code:
    1. void MainWindow::dataListener()
    2. {
    3. int maxSize = 1024;
    4. char *buffer = new char[maxSize];
    5. QFile file("C:/imgreceived.GIF");
    6. QDataStream in(&file);
    7.  
    8. if (!file.open(QIODevice::WriteOnly)) {
    9. cerr << "Cannot open file for writing: "
    10. << qPrintable(file.errorString()) << endl;
    11. return;
    12. }
    13.  
    14. while(1){
    15. int rec=sp->receive(maxSize,buffer);
    16. in.writeRawData(buffer,rec);
    17. if(rec != maxSize)
    18. break;
    19. }
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    This works correctly but i doubt using that break condition :if(rec != maxSize), because the last record may also be 1024 bytes(low possiblity;but perhaps )

    How else i can end writing to the file ?

  10. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: readrawdata' s read size

    QExtSerialPort is a QIODevice so you can still use atEnd to test if there's something to read.

  11. #10
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: readrawdata' s read size

    do you mean something like:

    Qt Code:
    1. while(!sp->atEnd()){
    2. int rec=sp->receive(maxSize,buffer);
    3. in.writeRawData(buffer,rec);
    4. }
    To copy to clipboard, switch view to plain text mode 
    didn't work but i used bytesavailable()>0 it is ok now..

    Qt Code:
    1. while(sp->bytesavailable()>0){
    2. int rec=sp->receive(maxSize,buffer);
    3. in.writeRawData(buffer,rec);
    4. }
    To copy to clipboard, switch view to plain text mode 

  12. #11
    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: readrawdata' s read size

    Quote Originally Posted by marcel View Post
    QExtSerialPort is a QIODevice so you can still use atEnd to test if there's something to read.
    QExtSerialPort is a sequencial device so atEnd() will probably not work properly.

  13. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: readrawdata' s read size

    Quote Originally Posted by wysota View Post
    QExtSerialPort is a sequencial device so atEnd() will probably not work properly.
    Seems so...

  14. #13
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: readrawdata' s read size

    Any other way to decide end of reception?

  15. #14
    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: readrawdata' s read size

    The protocol you use should be able to send an "end of stream" marker or send the expected stream length at the beginning of the transmission. There are no ways of indicating the end of transmission on the transportation layer, because you might always write some more data into the stream.

  16. The following user says thank you to wysota for this useful post:

    yagabey (22nd January 2008)

  17. #15
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Question Re: readrawdata' s read size

    Is there anybody who can show me a sample code for datalistener side? It is working problemeatic ... First 4 kb of the data is being read; but the rest not !

    The datalistener is connected to a signal which is emitted when bytesAvailable()>0.

    On the serialport monitor, I observed that; first alll of the data is being sent and after that on the datalistener side, this mass data is being read. I mean; it is like a half duplex communiction; reading and writing are not simultaneous...May this be the cause for the problem?

Similar Threads

  1. QLabel size policy
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2007, 17:57
  2. How to read Raw Information from CD in MAC?
    By vishal.chauhan in forum General Programming
    Replies: 0
    Last Post: 10th July 2007, 12:26
  3. How to read CD with read?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 29th June 2007, 08:20
  4. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29
  5. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 22:14

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.