PDA

View Full Version : possible open/write two files same time with QFile



npotency
14th November 2009, 23:58
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.

drhex
15th November 2009, 00:35
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.

npotency
15th November 2009, 00:56
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.



/*********Logging file************************************/
flog.remove();
if( !flog.open( QIODevice::ReadWrite | QIODevice::Append ) )
{
qDebug("Failed to open file.");
}


log << "Date: ";
log << QDate::currentDate().toString();
log << '\n';
log << "hello world";
log.flush(); //I had to use flush to get both files to write
qDebug("File opened.");
// flog.close();

//This is just a test file to see if I could open two files at once, I could as //long as I used flush.
//flog.remove();
if( !fhello.open( QIODevice::ReadWrite | QIODevice::Append ) )
{
qDebug("Failed to open file.");
}


hello << "Date: ";
hello << QDate::currentDate().toString();
hello << '\n';
hello << "hello world";
log.flush();
qDebug("File opened.");
fhello.close();

/************************************************** *******/


int arm_rev0::readADC(int adcnum)
{
QString adcmsg="ADC ";
// int randnum;
adcmsg.append(QString::number(adcnum));
adcmsg.append(" has been read successfully");
debug(adcmsg);
qDebug("Reading ADC");
//Generate a random ADC reading as a place holder
// randnum = rand() % 1024 + 1;

//Get ADC reading from the board
QByteArray entercmd="", adc="" ,bitsize="";
entercmd.append(254); //need to append an int, defining it doesn't work, 254 is for command mode
port->write(entercmd); //enter command mode
adcnum = adcnum + 149; //shifting to match command set
adc.append(adcnum); //set adc value to whatever is selected from adcnum

//Clear the serial buffer by storing all the data to a buffer
char buff[1024];
int numBytes1;

numBytes1 = port->bytesAvailable();
if(numBytes1 > 0)
{
if(numBytes1 > 1024) numBytes1 = 1024;

int i1 = port->read(buff, numBytes1);
buff[i1] = '\0';

}

// debug(buff); //Display this for curiousity
//End clearing serial buffer

port->write(adc); //tell board to return the value of the adc

//Put this byte into a buffer
char adcbuff[1024];
int numBytes;

numBytes = 1; //port->bytesAvailable();
if(numBytes > 0)
{
if(numBytes > 1024) numBytes = 1024;

int i = port->read(adcbuff, numBytes);
adcbuff[i] = '\0';

}
unsigned char buff1 = adcbuff[0]; //need to convert signed char to unsigned char (otherwise we get signed number eg -1 instead of 255)
double rawadc = buff1; //implicitly define buff1 as a double for calculation purposes
int rawadcint = buff1;

//value of adc is now stored in rawadc
ui->t_rawadc->setText(QString::number(rawadc)); //display the adc number in raw data box
debug(QString::number(rawadc));
//From random number, calculate voltage, and display in voltage box
double voltrefer, voltage;
QString currentvolt = ui->voltref->currentText();
if (currentvolt == "3.3"){
voltrefer = 3.3;
}
else if (currentvolt == "5"){
voltrefer = 5;
}
else if (currentvolt == "8"){
voltrefer = 8;
}
else if (currentvolt == "10"){
voltrefer = 10;
}
else if (currentvolt == "12"){
voltrefer = 12;
}
else if (currentvolt == "15"){
voltrefer = 15;
}
//calculate voltage from number
voltage = ((rawadc/255 * voltrefer)-0.17) * 25.56; //This is actually PSI now, need to write a separate equation
ui->t_voltsadc->setText(QString::number(voltage)); //send to text box
//end show voltage in voltage box

return(rawadcint); //Returns the raw adc value (0-255)
}


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.


while(pressurebad)
{
qDebug("Running check main pressure");
pressurebad = checkMainPressure();
}


int arm_rev0::checkMainPressure() //returns 0 if psi is good, a 1 if not
{
/*this checks to see if the main tank is at 100psi, if it isn't it loads or deloads the tank*/
int pressure;
pressure = readADC(2); //assume this is hooked to big tank
/*the adc will return a 255 if the main tank is at 120psi*/
if ((pressure < (213 + 15)) && (pressure > (213-15)))
{
relayoff(3); //turns release valve off of main tank
relayoff(4); //turns off pumps
relayoff(5);
return(0);
}
else if (pressure > (213 + 5)) //pressure is too high
{
relayon(3); //assumes relay 3 is hooked to release valve of main tank
relayoff(4);
relayoff(5);
qDebug("In greater than");
return(1);
}
else if (pressure < 213 )
{
relayon(4); //assumes pumps are hooked to relay 4 and 5
relayon(5);
relayoff(3);
qDebug("In less than");
return(1);
}

}

npotency
15th November 2009, 01:01
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.


void arm_rev0::debug(QString buffer)
{
/*Write to window
msg.append(buffer);
msg.append("\n");
ui->t_debug->setText(msg);
*/

/*Write to log file*/

log << buffer << '\n';
log.flush();



}

npotency
15th November 2009, 01:28
I've narrowed the problem down to this section of code.


void arm_rev0::debug(QString buffer)
{
/*Write to window
msg.append(buffer);
msg.append("\n");
ui->t_debug->setText(msg);
*/

/*Write to log file*/

log << buffer << '\n';
log.flush();



}

If I have


/*Write to window
msg.append(buffer);
msg.append("\n");
ui->t_debug->setText(msg);
*/

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