6 years, 11 months ago.

fscanf and feof not working issue: printing 0.00 values

/media/uploads/imran18/afe.cpp Hi,

I am using LPC1768. We are trying to read data from the file present in sd card. After reading and displaying all parameter values are becoming zero.

Please find below the code used in our system.

  1. include "mbed.h"
  2. include<stdio.h>
  3. include<stdlib.h>
  4. include "bq769x0.h"
  5. include "USBSerial.h"
  6. include "SDFileSystem.h" DigitalIn ALERT(alertPin); DigitalIn charge(chargepin); Timer timer; I2C i2c0(p27, p28); bq769x0 bootpin(bootpin); Serial serial(USBTX,USBRX);For serial communincation through UART0 SDFileSystem sd(p11, p12, p13, p14, "sd"); bool CHG, DSG; int a; int c=0; int i,count,celbal1,celbal2,celbal3,sys_ctrl2, ocd=0, scd=0, ov=0, uv=0; float loopStartTime = 0, loopStartTime1 = 0, Total_loop_time=0; float Total_loop_time1=0; float starttime = 0.0; float currentreadtime = 0.0; float loopEndTime = 0.0; float celbalstart_time = 0, celbalend_time =0, Bal_time = 0;; float time_diff = 0; float bat_volt,bat_volt1; float cellvolt[15],cellvolt1[15]; int check_status, mincell_volt, maxcell_volt, Avgcell_volt; float temp_deg1, temp_deg2, temp_deg3, temp_deg11, temp_deg21, temp_deg31 ; float soc; float battery_current, battery_current1, Bat_Ah = 0; float sc_current, overcurrent_protection, undervolt_prot,overvolt_prot; float Balancing_Status; int no_ofcells,no_ofconnectedcells; int ctrl2,prot1; float overcurrentcharge_protection; char sys_stat;

void write_data_to_serial() {

serial.printf("%.3f,%.3f,%.3f, %.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.8f,%.8f,%.8f%.3f,%.3f,%.3f \r\n",Total_loop_time,cellvolt[0],cellvolt[1],cellvolt[2],cellvolt[3],cellvolt[4],cellvolt[5],cellvolt[6],cellvolt[7],cellvolt[8],cellvolt[9],cellvolt[10],cellvolt[11],cellvolt[12],cellvolt[13],cellvolt[14], bat_volt,battery_current,Bat_Ah,soc,temp_deg3,temp_deg2,temp_deg1); }

void read_file() { FILE *tmp; tmp= fopen("/sd/afetest.csv", "r"); if(tmp==NULL) represent file is present { serial.printf("file not present\n"); } {

while(!feof(tmp)) { int z=0; z = fscanf(tmp,"%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f \r\n",&Total_loop_time1,&cellvolt[0],&cellvolt[1],&cellvolt[2],&cellvolt[3],&cellvolt[4],&cellvolt[5],&cellvolt[6],&cellvolt[7],&cellvolt[8],&cellvolt[9],&cellvolt[10],&cellvolt[11],&cellvolt[12],&cellvolt[13],&cellvolt[14], &bat_volt,&battery_current,&Bat_Ah,&soc,&temp_deg3,&temp_deg2,&temp_deg1); serial.printf("\n\n z is %d \r\n ", z); {

} } } fclose(tmp); }

void write_data_to_SD(FILE *fp) { Perform a write test if (fp != NULL) {

fprintf(fp,"%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f \r\n",Total_loop_time,cellvolt[0],cellvolt[1],cellvolt[2],cellvolt[3],cellvolt[4],cellvolt[5],cellvolt[6],cellvolt[7],cellvolt[8],cellvolt[9],cellvolt[10],cellvolt[11],cellvolt[12],cellvolt[13],cellvolt[14], bat_volt,battery_current,Bat_Ah,soc,temp_deg3,temp_deg2,temp_deg1);

} else { printf("Write to SD card failed!\n"); } fclose(fp); }

int main() { serial.baud(115200); timer.start(); serial.printf(" Program started\r\n"); bq769x0 obj( i2c0, alertPin, 3, 0x08, false); initialization of AFE registers and crc serial.printf(" Initialization of i2c is done and slave address is defined as 0x08\r\n"); check_status = obj.checkStatus(); serial.printf(" Check_status %d\r\n", check_status); obj.update(); serial.printf(" Current, cellvoltages, temperatures and balancing switches are updated\r\n"); obj.setShuntResistorValue(0.5); serial.printf(" Shunt resistor value is defined as 0.5mohms\r\n"); obj.setBatteryCapacity(3000); serial.printf(" Battery capacity is defined as 3000 mAh\r\n"); no_ofcells = obj.getNumberOfCells(); no_ofconnectedcells = obj.getNumberOfConnectedCells(); obj.setTemperatureLimits(0, 60, 0, 60); serial.printf(" Charge and discharge max and min temp values are defined as 0 and 60\r\n"); sc_current = obj.setShortCircuitProtection(150000, 100); delay is 100 usec serial.printf(" Short circuit protection is set and value is = %.3f\r\n", sc_current*0.001); overcurrent_protection = obj.setOvercurrentDischargeProtection(50000, 2000); delay is 1280ms serial.printf(" Overcurrent discharge protection is set and value is = %.3f\r\n", overcurrent_protection*0.001); undervolt_prot = obj.setCellUndervoltageProtection(3000, 5); delay is 5 sec serial.printf(" Cellundervoltage protection is set and value is = %.3f\r\n", undervolt_prot*0.001); overvolt_prot = obj.setCellOvervoltageProtection(4200, 2); delay is 2 sec serial.printf(" Cellovervoltage protection is set and value is = %.3f\r\n", overvolt_prot*0.001); obj.setBalancingThresholds(0, 4100, 20); balancing limit is set sd.mount(); sd card file initialization void read_file(); / HERE I AM USING THE READ FUNCTION AND GENERATING 0 VALUES

write_data_to_serial();Display data; FILE *logFile = fopen("/sd/afetest.csv", "a"); fprintf(logFile,"Total_loop_time,cellvolt[0],cellvolt[1],cellvolt[2],cellvolt[3],cellvolt[4],cellvolt[5],cellvolt[6],cellvolt[7],cellvolt[8],cellvolt[9],cellvolt[10],cellvolt[11],cellvolt[12],cellvolt[13],cellvolt[14], bat_volt,battery_current,Bat_Ah,soc,temp_deg3,temp_deg2,temp_deg1\r\n");

while(1) { c=c+1; loopStartTime = (timer.read_high_resolution_us()*0.000001); gives timer value in usec obj.update(); update cell volts, battery current, temperature and cell balancing is started soc = obj.getSOC(); sys_ctrl2 = obj.readRegister(SYS_CTRL2); serial.printf("loopStartTime is: %f\r\n", loopStartTime); bat_volt = obj.getBatteryVoltage()*0.001; serial.printf("Bat_volt is : %.3f\r\n", bat_volt); battery_current = obj.getBatteryCurrent()*0.001; serial.printf(" Battery current is : %.3f\r\n", battery_current); for(i=1;i<=15;i++) { cellvolt[i-1] = obj.getCellVoltage(i)*0.001; }

Be the first to answer this question.