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