Results 1 to 5 of 5

Thread: possible open/write two files same time with QFile

  1. #1

    Default possible open/write two files same time with QFile

    I'm using Qextserialport which writes to the serial port as if it was a file (Linux).

    Now I also want to generate a log file.

    When I have both of these open it seems like I can't write to either of them. Is it even possible to have two files open and write/read from them?

    Or do you have to open one and close it, then open the other and close it before using the other.

  2. #2
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: possible open/write two files same time with QFile

    You can definitely write to several files without having to close other files first. Try making a minimal program that has the problem and if you don't find the bug while making that program, post it here.

  3. #3

    Default Re: possible open/write two files same time with QFile

    Here is some code that works to write to two files. I have a button to send data through the serial port which calls a readADC function. If I manually click this button, it will read the ADC and write to the log file. But as soon as I run a loop to check the ADC automatically, it doesn't get the new value of the ADC. It's the same function. Code is below.

    Qt Code:
    1. /*********Logging file************************************/
    2. flog.remove();
    3. if( !flog.open( QIODevice::ReadWrite | QIODevice::Append ) )
    4. {
    5. qDebug("Failed to open file.");
    6. }
    7.  
    8.  
    9. log << "Date: ";
    10. log << QDate::currentDate().toString();
    11. log << '\n';
    12. log << "hello world";
    13. log.flush(); //I had to use flush to get both files to write
    14. qDebug("File opened.");
    15. // flog.close();
    16.  
    17. //This is just a test file to see if I could open two files at once, I could as //long as I used flush.
    18. //flog.remove();
    19. if( !fhello.open( QIODevice::ReadWrite | QIODevice::Append ) )
    20. {
    21. qDebug("Failed to open file.");
    22. }
    23.  
    24.  
    25. hello << "Date: ";
    26. hello << QDate::currentDate().toString();
    27. hello << '\n';
    28. hello << "hello world";
    29. log.flush();
    30. qDebug("File opened.");
    31. fhello.close();
    32.  
    33. /*********************************************************/
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. int arm_rev0::readADC(int adcnum)
    2. {
    3. QString adcmsg="ADC ";
    4. // int randnum;
    5. adcmsg.append(QString::number(adcnum));
    6. adcmsg.append(" has been read successfully");
    7. debug(adcmsg);
    8. qDebug("Reading ADC");
    9. //Generate a random ADC reading as a place holder
    10. // randnum = rand() % 1024 + 1;
    11.  
    12. //Get ADC reading from the board
    13. QByteArray entercmd="", adc="" ,bitsize="";
    14. entercmd.append(254); //need to append an int, defining it doesn't work, 254 is for command mode
    15. port->write(entercmd); //enter command mode
    16. adcnum = adcnum + 149; //shifting to match command set
    17. adc.append(adcnum); //set adc value to whatever is selected from adcnum
    18.  
    19. //Clear the serial buffer by storing all the data to a buffer
    20. char buff[1024];
    21. int numBytes1;
    22.  
    23. numBytes1 = port->bytesAvailable();
    24. if(numBytes1 > 0)
    25. {
    26. if(numBytes1 > 1024) numBytes1 = 1024;
    27.  
    28. int i1 = port->read(buff, numBytes1);
    29. buff[i1] = '\0';
    30.  
    31. }
    32.  
    33. // debug(buff); //Display this for curiousity
    34. //End clearing serial buffer
    35.  
    36. port->write(adc); //tell board to return the value of the adc
    37.  
    38. //Put this byte into a buffer
    39. char adcbuff[1024];
    40. int numBytes;
    41.  
    42. numBytes = 1; //port->bytesAvailable();
    43. if(numBytes > 0)
    44. {
    45. if(numBytes > 1024) numBytes = 1024;
    46.  
    47. int i = port->read(adcbuff, numBytes);
    48. adcbuff[i] = '\0';
    49.  
    50. }
    51. unsigned char buff1 = adcbuff[0]; //need to convert signed char to unsigned char (otherwise we get signed number eg -1 instead of 255)
    52. double rawadc = buff1; //implicitly define buff1 as a double for calculation purposes
    53. int rawadcint = buff1;
    54.  
    55. //value of adc is now stored in rawadc
    56. ui->t_rawadc->setText(QString::number(rawadc)); //display the adc number in raw data box
    57. debug(QString::number(rawadc));
    58. //From random number, calculate voltage, and display in voltage box
    59. double voltrefer, voltage;
    60. QString currentvolt = ui->voltref->currentText();
    61. if (currentvolt == "3.3"){
    62. voltrefer = 3.3;
    63. }
    64. else if (currentvolt == "5"){
    65. voltrefer = 5;
    66. }
    67. else if (currentvolt == "8"){
    68. voltrefer = 8;
    69. }
    70. else if (currentvolt == "10"){
    71. voltrefer = 10;
    72. }
    73. else if (currentvolt == "12"){
    74. voltrefer = 12;
    75. }
    76. else if (currentvolt == "15"){
    77. voltrefer = 15;
    78. }
    79. //calculate voltage from number
    80. voltage = ((rawadc/255 * voltrefer)-0.17) * 25.56; //This is actually PSI now, need to write a separate equation
    81. ui->t_voltsadc->setText(QString::number(voltage)); //send to text box
    82. //end show voltage in voltage box
    83.  
    84. return(rawadcint); //Returns the raw adc value (0-255)
    85. }
    To copy to clipboard, switch view to plain text mode 


    Here's the loop that doesn't work. If I hit a button that calls readADC, it works. But when I call it in this function it doesn't.
    Qt Code:
    1. while(pressurebad)
    2. {
    3. qDebug("Running check main pressure");
    4. pressurebad = checkMainPressure();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. int arm_rev0::checkMainPressure() //returns 0 if psi is good, a 1 if not
    2. {
    3. /*this checks to see if the main tank is at 100psi, if it isn't it loads or deloads the tank*/
    4. int pressure;
    5. pressure = readADC(2); //assume this is hooked to big tank
    6. /*the adc will return a 255 if the main tank is at 120psi*/
    7. if ((pressure < (213 + 15)) && (pressure > (213-15)))
    8. {
    9. relayoff(3); //turns release valve off of main tank
    10. relayoff(4); //turns off pumps
    11. relayoff(5);
    12. return(0);
    13. }
    14. else if (pressure > (213 + 5)) //pressure is too high
    15. {
    16. relayon(3); //assumes relay 3 is hooked to release valve of main tank
    17. relayoff(4);
    18. relayoff(5);
    19. qDebug("In greater than");
    20. return(1);
    21. }
    22. else if (pressure < 213 )
    23. {
    24. relayon(4); //assumes pumps are hooked to relay 4 and 5
    25. relayon(5);
    26. relayoff(3);
    27. qDebug("In less than");
    28. return(1);
    29. }
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

  4. #4

    Default Re: possible open/write two files same time with QFile

    Here's the debug() function that gets called in readADC. I don't know why it would matter since it worked fine when just clicking a button manually.

    Qt Code:
    1. void arm_rev0::debug(QString buffer)
    2. {
    3. /*Write to window
    4.   msg.append(buffer);
    5.   msg.append("\n");
    6.   ui->t_debug->setText(msg);
    7.   */
    8.  
    9. /*Write to log file*/
    10.  
    11. log << buffer << '\n';
    12. log.flush();
    13.  
    14.  
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

  5. #5

    Default Re: possible open/write two files same time with QFile

    I've narrowed the problem down to this section of code.

    Qt Code:
    1. void arm_rev0::debug(QString buffer)
    2. {
    3. /*Write to window
    4.   msg.append(buffer);
    5.   msg.append("\n");
    6.   ui->t_debug->setText(msg);
    7.   */
    8.  
    9. /*Write to log file*/
    10.  
    11. log << buffer << '\n';
    12. log.flush();
    13.  
    14.  
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    If I have
    Qt Code:
    1. /*Write to window
    2.   msg.append(buffer);
    3.   msg.append("\n");
    4.   ui->t_debug->setText(msg);
    5.   */
    To copy to clipboard, switch view to plain text mode 

    uncommented, it works. otherwise, it doesn't work. Thoughts?

Similar Threads

  1. QFile resized files gets truncated after writing
    By MaximA in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2008, 17:23
  2. changing files lastmodified time
    By ramazangirgin in forum Qt Programming
    Replies: 2
    Last Post: 27th March 2008, 10:04
  3. How to set the file's date and time.
    By santosh.kumar in forum General Programming
    Replies: 3
    Last Post: 12th October 2007, 11:13
  4. modification time on files
    By soul_rebel in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 20:51
  5. QFile and Creating Files
    By Jingoism in forum Newbie
    Replies: 2
    Last Post: 28th July 2007, 17:11

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.