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();
{
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();
{
qDebug("Failed to open file.");
}
hello << "Date: ";
hello <<
QDate::currentDate().
toString();
hello << '\n';
hello << "hello world";
log.flush();
qDebug("File opened.");
fhello.close();
/*********************************************************/
/*********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();
/*********************************************************/
To copy to clipboard, switch view to plain text mode
int arm_rev0::readADC(int adcnum)
{
// 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
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 //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)
}
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)
}
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.
while(pressurebad)
{
qDebug("Running check main pressure");
pressurebad = checkMainPressure();
}
while(pressurebad)
{
qDebug("Running check main pressure");
pressurebad = checkMainPressure();
}
To copy to clipboard, switch view to plain text mode
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);
}
}
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);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks