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 How to write bytes read from serial port to a QFile

    hiii friends,

    i am using Qt3.3.6 on Fedora Core-6 OS.
    i have written a code to read data from the serial port. It reads the data and prints on the terminal and also writes to a QFile.
    The code is as follows
    Qt Code:
    1. #include<termios.h>
    2. #include<fcntl.h>
    3. #include<errno.h>
    4. #include<sys/ioctl.h>
    5. #include<stdio.h>
    6. #include<sys/time.h>
    7. #include<iostream.h>
    8. #include<sys/types.h>
    9. #include<unistd.h>
    10. #include<string.h>
    11. #include <qfile.h>
    12. #include <qdatastream.h>
    13. #include <qstring.h>
    14. #include <qmessagebox.h>
    15. #include <qapplication.h>
    16. #include <qtextstream.h>
    17.  
    18.  
    19. main ()
    20. {
    21.  
    22. struct termios t;
    23. char *devicedest = "/dev/ttyr00";
    24.  
    25. unsigned char recvbuf[2];
    26. int rbyte, status, sPortdest,r,i,c=0;
    27. long count=0;
    28.  
    29. int inp_check=0;
    30. int ifirst=0;
    31. QFile ff("/root/ss.txt");
    32. ff.open( IO_WriteOnly );
    33. QTextStream stream(&ff);
    34. QString buff = "";
    35.  
    36. struct timeval timeout;
    37. fd_set testfds,readfds;
    38.  
    39.  
    40. t.c_cc[VMIN] = 1;
    41. t.c_cc[VTIME]=0;
    42. // t.c_cc[VEOF]='\n';
    43. /* t.c_iflag &= ~(BRKINT|IGNPAR|PARMRK|INPCK|INLCR|IGNCR|ICRNL|IXON);
    44.   t.c_iflag |= (IGNBRK|ISTRIP|IXOFF);
    45.   t.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP);
    46.   t.c_cflag &= (CSIZE|CSTOPB|HUPCL|PARENB);
    47.   t.c_cflag |= (CLOCAL|CREAD|CS8);*/
    48. // t.c_iflag |= (ISIG);
    49. // t.c_iflag &= ~(IUCLC);
    50.  
    51. // t.c_iflag &= ~BRKINT;
    52.  
    53. t.c_iflag = (ISIG | IUCLC) ;
    54. t.c_cflag = B9600 | CS8 | CREAD | CLOCAL| HUPCL;
    55.  
    56.  
    57. sPortdest= open(devicedest,O_RDONLY|O_NOCTTY|O_NDELAY);
    58. if(tcsetattr(sPortdest,TCSANOW,&t) != 0 )
    59. {
    60. cout<<"\n attribute not set";
    61. }
    62. FD_ZERO(&readfds);
    63. FD_SET(sPortdest,&readfds);
    64.  
    65.  
    66. if(sPortdest!=-1)
    67. {
    68. cout<<"Open successfully\n";
    69. do
    70. {
    71. testfds = readfds;
    72.  
    73. timeout.tv_sec=0;
    74. timeout.tv_usec=1000;
    75.  
    76. r=select(FD_SETSIZE,&testfds,(fd_set *)0,(fd_set *)0,&timeout);
    77.  
    78. if(r == -1)
    79. cout<<"Error in select";
    80.  
    81.  
    82. //Activity happen on some ports
    83. if(r > 0)
    84. {
    85. //check all
    86.  
    87. for(i=0;i<FD_SETSIZE;i++)
    88. {
    89.  
    90. if(FD_ISSET(i,&testfds))
    91. {
    92. //Activity on serial port
    93. if(i == sPortdest)
    94. {
    95.  
    96. if(ifirst == 0) //Set all attribute when first start
    97. {
    98. status = ioctl(sPortdest,TCSETS,t);
    99. ifirst = 1;
    100. }
    101. rbyte= read(sPortdest,recvbuf,1);
    102.  
    103. if(rbyte > 0)
    104. {
    105. recvbuf[1]='\0';
    106. cout<<recvbuf;
    107. stream<<recvbuf;
    108. count++;
    109. if((count%11)==0)
    110. {
    111. // cout<<"";
    112.  
    113. }
    114. usleep(1000);
    115. }
    116. }
    117. }
    118. }
    119. }
    120.  
    121. //Activity not happen on SP
    122. if(r == 0)
    123. {
    124. //cout<<"\nwaiting\n";
    125. usleep(1000);
    126. ff.close();
    127.  
    128. }
    129.  
    130. }while(1);
    131. }
    132. else
    133. {
    134. cout<<"Device could not be oepn successfully";
    135. }
    136.  
    137.  
    138. }
    To copy to clipboard, switch view to plain text mode 


    when i run this code on the terminal, it shows the file correctly on the terminal but it data written to the file is in some other format ( may b in hex). only integer numbers are getting stored in the file and not the actual characters.
    also when i run the program for the first time after tranmitting the file from other end, it says error : QFile::writeBlock() file not open.
    then i have to terminate the program and again run it. Then it does not give that error. But anyways the data written to the file is not the one getting printed to the terminal. On terminal the exact characters/numbers are printed while on the file some ascii or integer numbers get stored.

    please tell me how to solve this problem
    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

    try:
    Qt Code:
    1. stream<<(const char*)recvbuf;
    2. //or
    3. stream<<(char*)recvbuf;
    To copy to clipboard, switch view to plain text mode 
    ==========================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

    thanks for the reply

    the mistake was actually somewhere else

    if u see the code deeply, then i have closed the file and then for writing the data i was not opening it.

    now the file is being written correctly
    i just want to ask one thing now
    and that is i m currently transferring text files and writing the same to another text file. is it possible to transfer any kind of binary files like pdf, zip, jpg etc. and recieve it in the same format using the same code listed above? may be the slightest modification i guess would be to use QDataStream rather than QTextStream

    wat Say??
    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

    if u see the code deeply, then i have closed the file and then for writing the data i was not opening it.
    But you said you are getting data in the file, just not the same as you get in the console.
    How is it possible you got data in the file if it was closed? (not that I am saying that what you said is not the problem, since obviously you had it corrected, but I still don't understand how this is possible).

    is it possible to transfer any kind of binary files like pdf, zip, jpg etc. and recieve it in the same format using the same code listed above? may be the slightest modification i guess would be to use QDataStream rather than QTextStream
    Yes, for this you need to use QDataStream if you want to use a tream.
    Ah just a second, you are not using QextSerailPort - so you can't use QDataStream.
    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.
    ==========================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

    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

  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

    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.

  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

    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

  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

    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.

  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

    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

  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

    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.

  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

    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

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.