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
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    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

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

    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.

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

    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

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

    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.

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

    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

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

    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.

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

    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

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

    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.

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

    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

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

    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.

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

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

    i have now tried to use QExtSerialPort and have written a small application using is which is as follows :

    Qt Code:
    1. #include </sfiles/Serial Communication/qextserialport-0.9.0/posix_qextserialport.h>
    2. #include </sfiles/Serial Communication/qextserialport-0.9.0/qextserialbase.h>
    3. #include <iostream.h>
    4. #include <stdio.h>
    5. #include <stdlib.h>
    6. #include <qfile.h>
    7. #include <qapplication.h>
    8. #include <qstring.h>
    9. #include <qmessagebox.h>
    10. #include <qdatastream.h>
    11. #include <qtextstream.h>
    12.  
    13.  
    14.  
    15. void Form1::send_clicked()
    16. {
    17. QFile ff("/root/ss.txt");
    18. QDataStream stream(&ff);
    19. ff.open( IO_ReadOnly );
    20.  
    21. if(ff.exists())
    22. {
    23. qWarning("file exists");
    24. }
    25. else
    26. {
    27. qWarning("file does not exists");
    28. }
    29.  
    30. Posix_QextSerialPort sp("/dev/ttyS0");
    31. sp.open();
    32. sp.flush();
    33. char *data = new char();
    34. ff.readBlock(data, 15);
    35. if ( !sp.isOpen())
    36. {
    37. qWarning("port not opened");
    38. }
    39. else
    40. {
    41. qWarning("port opened successfully");
    42. }
    43. qWarning("Writing data ... ");
    44. qWarning(data);
    45. sp.writeBlock(data , 15);
    46. sp.close();
    47. ff.close();
    48. if(sp.open())
    49. {
    50. qWarning("port still open after closing");
    51. }
    52. else
    53. {
    54. qWarning("port closed successfully");
    55. }
    56. qWarning("Data written successfully..........now exiting");
    57. close();
    58.  
    59.  
    60. }
    61.  
    62.  
    63. void Form1::recieve_clicked()
    64. {
    65. QFile ff("/root/sss.txt");
    66. Posix_QextSerialPort sp("/dev/ttyS0");
    67. sp.open();
    68. if(!sp.isOpen())
    69. {
    70. qWarning("port not opened");
    71. }
    72. else
    73. {
    74. qWarning("port opened successfully");
    75. }
    76. char *data = new char();
    77. while(1)
    78. {
    79. //sleep(1000);
    80. //cout<<"No bytes currently waiting on the port "<<endl;
    81.  
    82. if(sp.bytesWaiting()>0)
    83. {
    84. ff.open( IO_Raw | IO_WriteOnly | IO_Append );
    85. cout<<"Bytes waiting ---"<<sp.bytesWaiting()<<endl;
    86. sp.readBlock(data,15);
    87. ff.writeBlock(data,15);
    88. ff.close();
    89. }
    90.  
    91. }
    92. sp.close();
    93.  
    94. }
    To copy to clipboard, switch view to plain text mode 

    i m quite sure that the libraries have compiled well.
    i have added two pushbuttons in the applications through which you can send and recieve the data

    by clicking on send button the output is as follows :

    Qt Code:
    1. [root@localhost ftransfer]# ./ftransfer
    2. file exists
    3. port opened successfully
    4. Writing data ...
    5. God is Great
    6.  
    7. *** glibc detected *** ./ftransfer: malloc(): memory corruption (fast): 0x08332fe8 ***
    8. ======= Backtrace: =========
    9. /lib/libc.so.6[0x29fe55]
    10. /lib/libc.so.6(__libc_malloc+0x7e)[0x2a0d2e]
    11. /usr/lib/libstdc++.so.6(_Znwj+0x27)[0x6875d27]
    12. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QGArray7newDataEv+0x1e)[0x10f2a0e]
    13. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QGArrayC2Ei+0x3b)[0x10f2c5b]
    14. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN8QCStringC1Ei+0x2c)[0x10e2fac]
    15. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZNK10QUtf8Codec11fromUnicodeERK7QStringRi+0x4c)[0x114656c]
    16. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZNK10QTextCodec11fromUnicodeERK7QString+0x39)[0x1140489]
    17. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZNK7QString9local8BitEv+0x39)[0x1114169]
    18. /usr/lib/qt-3.3/lib/libqt-mt.so.3[0x10f0159]
    19. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN5QFile10encodeNameERK7QString+0x2a)[0x10effba]
    20. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN5QFile4openEi+0x101)[0x10d9161]
    21. /usr/lib/libqextserialport.so.1(_ZN20Posix_QextSerialPort4openEi+0x93)[0x9ac88b]
    22. ./ftransfer(_ZN7QWidget6createEmbb+0xe69)[0x804c285]
    23. ./ftransfer(_ZN7QWidget6createEmbb+0x16e9)[0x804cb05]
    24. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QObject15activate_signalEP15QConnectionListP8QUObject+0x16a)[0xdf8e2a]
    25. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QObject15activate_signalEi+0xbd)[0xdf995d]
    26. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QButton7clickedEv+0x2c)[0x118c8ac]
    27. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QButton13keyPressEventEP9QKeyEvent+0x61)[0xe9c7b1]
    28. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QWidget5eventEP6QEvent+0x36b)[0xe3627b]
    29. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN12QApplication14internalNotifyEP7QObjectP6QEvent+0x9b)[0xd900fb]
    30. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN12QApplication6notifyEP7QObjectP6QEvent+0x54d)[0xd91a1d]
    31. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN9QETWidget17translateKeyEventEPK7_XEventb+0x36a)[0xd25b0a]
    32. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN12QApplication15x11ProcessEventEP7_XEvent+0x635)[0xd26a75]
    33. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN10QEventLoop13processEventsEj+0x4eb)[0xd386ab]
    34. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN10QEventLoop9enterLoopEv+0x42)[0xda9672]
    35. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN10QEventLoop4execEv+0x26)[0xda9536]
    36. /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN12QApplication4execEv+0x1f)[0xd8fc0f]
    37. ./ftransfer(_ZNK7QDialog8sizeHintEv+0x173)[0x804be2f]
    38. /lib/libc.so.6(__libc_start_main+0xdc)[0x24ef2c]
    39. ./ftransfer(_ZNK7QDialog8sizeHintEv+0x55)[0x804bd11]
    40. ======= Memory map: ========
    41. 00110000-0020e000 r-xp 00000000 03:07 483679 /usr/lib/libX11.so.6.2.0
    42. 0020e000-00212000 rwxp 000fe000 03:07 483679 /usr/lib/libX11.so.6.2.0
    43. 00212000-00237000 r-xp 00000000 03:07 1329150 /lib/libm-2.5.so
    44. 00237000-00238000 r-xp 00024000 03:07 1329150 /lib/libm-2.5.so
    45. 00238000-00239000 rwxp 00025000 03:07 1329150 /lib/libm-2.5.so
    46. 00239000-00370000 r-xp 00000000 03:07 1329143 /lib/libc-2.5.so
    47. 00370000-00372000 r-xp 00137000 03:07 1329143 /lib/libc-2.5.so
    48. 00372000-00373000 rwxp 00139000 03:07 1329143 /lib/libc-2.5.so
    49. 00373000-00376000 rwxp 00373000 00:00 0
    50. 00376000-00389000 r-xp 00000000 03:07 1329145 /lib/libpthread-2.5.so
    51. 00389000-0038a000 r-xp 00012000 03:07 1329145 /lib/libpthread-2.5.so
    52. 0038a000-0038b000 rwxp 00013000 03:07 1329145 /lib/libpthread-2.5.so
    53. 0038b000-0038d000 rwxp 0038b000 00:00 0
    54. 0038d000-003f5000 r-xp 00000000 03:07 483703 /usr/lib/libmng.so.1.0.0
    55. 003f5000-003f8000 rwxp 00067000 03:07 483703 /usr/lib/libmng.so.1.0.0
    56. 003f8000-0040a000 r-xp 00000000 03:07 483673 /usr/lib/libz.so.1.2.3
    57. 0040a000-0040b000 rwxp 00011000 03:07 483673 /usr/lib/libz.so.1.2.3
    58. 0040b000-0040e000 r-xp 00000000 03:07 483687 /usr/lib/libXrandr.so.2.0.0
    59. 0040e000-0040f000 rwxp 00002000 03:07 483687 /usr/lib/libXrandr.so.2.0.0
    60. 0040f000-00421000 r-xp 00000000 03:07 483704 /usr/lib/libXft.so.2.1.2
    61. 00421000-00422000 rwxp 00012000 03:07 483704 /usr/lib/libXft.so.2.1.2
    62. 00422000-00424000 r-xp 00000000 03:07 1329144 /lib/libdl-2.5.so
    63. 00424000-00425000 r-xp 00001000 03:07 1329144 /lib/libdl-2.5.so
    64. 00425000-00426000 rwxp 00002000 03:07 1329144 /lib/libdl-2.5.so
    65. 00426000-00428000 r-xp 00000000 03:07 483677 /usr/lib/libXau.so.6.0.0
    66. 00428000-00429000 rwxp 00001000 03:07 483677 /usr/lib/libXau.so.6.0.0
    67. 00429000-0042e000 r-xp 00000000 03:07 483678 /usr/lib/libXdmcp.so.6.0.0
    68. 0042e000-0042f000 rwxp 00004000 03:07 483678 /usr/lib/libXdmcp.so.6.0.0
    69. 0042f000-00460000 r-xp 00000000 03:07 483702 /usr/lib/liblcms.so.1.0.15
    70. 00460000-00461000 rwxp 00030000 03:07 483702 /usr/lib/liblcms.so.1.0.15
    71. 00461000-00464000 rwxp 00461000 00:00 0
    72. 00464000-00479000 r-xp 00000000 03:07 686877 /usr/lib/qt-3.3/plugins/styles/bluecurve.so
    73. 00479000-0047a000 rwxp 00015000 03:07 686877 /usr/lib/qt-3.3/plugins/styles/bluecurve.so
    74. 005c4000-005cd000 r-xp 00000000 03:07 487954 /lib/libnss_files-2.5.so
    75. 005cd000-005ce000 r-xp 00008000 03:07 487954 /lib/libnss_files-2.5.so
    76. 005ce000-005cf000 rwxp 00009000 03:07 487954 /lib/libnss_files-2.5.so
    77. 007e8000-007e9000 r-xp 007e8000 00:00 0 [vdso]
    78. 009a8000-009af000 r-xp 00000000 03:07 484535 /usr/lib/libqextserialport.so.1
    79. 009af000-009b0000 rwxp 00006000 03:07 484535 /usr/lib/libqextserialport.so.1
    80. 00ab3000-00ac2000 r-xp 00000000 03:07 483684 /usr/lib/libXext.so.6.4.0
    81. 00ac2000-00ac3000 rwxp 0000e000 03:07 483684 /usr/lib/libXext.so.6.4.0
    82. 00ac5000-00ae4000 r-xp 00000000 03:07 1329151 /lib/libexpat.so.0.5.0
    83. 00ae4000-00ae6000 rwxp 0001e000 03:07 1329151 /lib/libexpat.so.0.5.0
    84. 00ae8000-00b0f000 r-xp 00000000 03:07 483675 /usr/lib/libfontconfig.so.1.1.0
    85. 00b0f000-00b17000 rwxp 00027000 03:07 483675 /usr/lib/libfontconfig.so.1.1.0
    86. 00b19000-00b21000 r-xp 00000000 03:07 483680 /usr/lib/libXrender.so.1.3.0
    87. 00b21000-00b22000 rwxp 00007000 03:07 483680 /usr/lib/libXrender.so.1.3.0
    88. 00b24000-00b28000 r-xp 00000000 03:07 483688 /usr/lib/libXfixes.so.3.1.0
    89. 00b28000-00b29000 rwxp 00003000 03:07 483688 /usr/lib/libXfixes.so.3.1.0
    90. 00b2c000-00b45000 r-xp 00000000 03:07 1329142 /lib/ld-2.5.so
    91. 00b45000-00b46000 r-xp 00018000 03:07 1329142 /lib/ld-2.5.so
    92. 00b46000-00b47000 rwxp 00019000 03:07 1329142 /lib/ld-2.5.so
    93. 00b47000-01378000 r-xp 00000000 03:07 196091 /usr/lib/qt-3.3/lib/libqt-mt.so.3.3.6
    94. 01378000-013b9000 rwxp 00830000 03:07 196091 Aborted
    95. [root@localhost ftransfer]#
    To copy to clipboard, switch view to plain text mode 

    after printing God is Great on the terminal it waits for about 1 min. and then immediately prints the above memory map and backtrace.

    also when i clicke recieve the program hangs( i know i runs in infinite while loop) and nothing is recieved.

    can anyone please tell me where is the fault ??
    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
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    some comments:
    1. why do you allocate the serial port on the stack?
    make it a member, this way you don't need to allocate it in every method, and better on the heap.
    It is dengeraus to do it the way you do.
    2. why don't you use QDataStream on the serial port?
    3. you want to use isOpen() not open() :
    Qt Code:
    1. if(sp.isOpen()) //here
    2. {
    3. qWarning("port still open after closing");
    4. }
    To copy to clipboard, switch view to plain text mode 

    It could well be that once you fix these points, the problem will go away, if not, posts again.

    Another point:
    You don't need to use Posix_QexSerial port, it is not portable.
    Use QextSerialPort, and give the _TTY_POSIX_ flag in the make file.
    This way your code will compile on windows as well.
    ==========================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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.