Results 1 to 20 of 20

Thread: How to write bytes read from serial port to a QFile

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Posts
    102
    Qt products
    Qt3
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: How to write bytes read from serial port to a QFile

    So you will have to read the input data in binary form and transmit it the same similar to what you have done above.
    Nothing speciall actually.
    i dint get u
    do you mean i dont need to change my code ??
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How to write bytes read from serial port to a QFile

    Yes you will need to change a bit.
    I was not sure if you want to read the data from the serial port or from a file and send through serial port.
    If you are getting the data through the serial port, then you can use QDataStream and QFile to save that binary data in its origianl binary form (not text).

    EDIT:
    by using QextSerialPort this is really easy, just few lines of code since both QextSerial and QFile are QIODevices, that you can use with QDataStream.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2006
    Posts
    102
    Qt products
    Qt3
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: How to write bytes read from serial port to a QFile

    so u mean 2 say just by using QDataStream in place of QTextStream in the above code will do !!! rite

    i have already got QExtSerial package with me. But i am afraid of using it since i m finding it difficult. if this code will work fine with other file formats also by using QDataStream then i think i wont b using QExtSerial for that. In my current code i m just reading the data from the serial port. but the application involves sending file through serial port also.

    if anybody has some code in which there is use of QExtSerial then please post it over here. it will b of gr8 help.

    best regards,
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How to write bytes read from serial port to a QFile

    for goodness sake,please don't use sms lingo.
    You have a kyeboard, use it.

    Well, basically yes, it would be enough if you change to QDataStream, and write access to the file should be binary raw.

    But your whole approach is not very nice.
    You are reading one char at a time, and write one char at a time.
    Why not read everything at once (or large chunks) and write is so as well.

    I think QextSerialPort is much easier then what you used here, and it plays nicely with Qt.

    But do what you think is the best for you.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2006
    Posts
    102
    Qt products
    Qt3
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: How to write bytes read from serial port to a QFile

    in my above code i have changed from QTextStream to QDataStream.

    but i not able to write the file properly.
    if the file is simple text file then it is written correctly but if it something else like .doc, .pdf etc. then i m not able to write to the file. the data written to the file is also not readable on the terminal as u might c from the code that i m also printing the data on the terminal and writing the same to the file simultaneously.

    please tell me wat 2 do in that case. is it that this problem cant be solved without using QExtSerial ??
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How to write bytes read from serial port to a QFile

    how do you expect us to answer that with out seeing the code you are talking about?

    PLEASE don't use sms short typing!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Sep 2006
    Posts
    102
    Qt products
    Qt3
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: How to write bytes read from serial port to a QFile

    the code is already posted above

    the only difference is instead of QTextStream i have used QDataStream
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How to write bytes read from serial port to a QFile

    you might want to try writeRawBytes()
    And you also should open the file in raw mode (IO_Raw)(as I told you before) .
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Sep 2006
    Posts
    102
    Qt products
    Qt3
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: How to write bytes read from serial port to a QFile

    i have done following modifications in the above code :

    Qt Code:
    1. ff.open( IO_Raw | IO_WriteOnly | IO_Append);
    2. stream.writeRawBytes((const char *)recbuf, 1);
    To copy to clipboard, switch view to plain text mode 

    but still the problem is that the file size is the same i.e. exact number of bytes are getting written but i m not able to read that file.
    i.e. if i transfer a pdf file then the pdf reader is not able to read the file
    if i transfer a .doc file then openoffice is not able to read the file
    if i transfer a .jpg file then picture viewer is not able to read the file

    can u please tell now what to do ??
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How to write bytes read from serial port to a QFile

    hmm...
    Try openning both files (the original and the one you wrote through the serial port) with an hex editor/viewer, this might show you what in fact is being written from your code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Sep 2006
    Posts
    102
    Qt products
    Qt3
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: How to write bytes read from serial port to a QFile

    Quote Originally Posted by high_flyer View Post
    hmm...
    Try openning both files (the original and the one you wrote through the serial port) with an hex editor/viewer, this might show you what in fact is being written from your code.
    what is hex editor/viewer and how can i open it?
    can you please tell how to open both the files original and the one i wrote through serial port.
    it would be quite helpful if you can explain in detail or can provide some code
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How to write bytes read from serial port to a QFile

    Hex editor
    Ask if you have further questions.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. 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
  •  
Qt is a trademark of The Qt Company.