Hi forum, please help. I try to write log file to SD card for debugging. However, I got “memory permission error”.
I am aware that @jpearman had example using vex::brain::sdcard::savefile
. I would like to learn if there is another way to do it.
Is it because vexos did not support stdio.
void usercontrol(void) {
// User control code here, inside the loop
FILE * usd_file_write;
usd_file_write=fopen("log.txt", "w");
fprintf(usd_file_write, "/* log file for debug */");
fclose(usd_file_write);
int timeOld=Brain.timer(timeUnits::msec);
int timeNew ;
int deltaTime;
while (1) {
timeNew=Brain.timer(timeUnits::msec);
deltaTime=timeNew-timeOld;
timeOld=Brain.timer(timeUnits::msec);
FILE* usd_file_write = fopen("log.txt", "a");
fprintf(usd_file_write, "wait(%d, msec)", deltaTime);
fclose(usd_file_write);
wait(20, msec); // Sleep the task for a short amount of time to
// prevent wasted resources.
}
}
stdio if supported, you probably have an issue with the SD card. The memory permission error will come from using a NULL FILE pointer. You should always test return value of fopen.
void usercontrol(void) {
// User control code here, inside the loop
FILE * usd_file_write;
usd_file_write=fopen("log.txt", "w");
if( usd_file_write ) {
fprintf(usd_file_write, "/* log file for debug */\n");
fclose(usd_file_write);
}
int timeOld=Brain.timer(timeUnits::msec);
int timeNew ;
int deltaTime;
while (1) {
timeNew=Brain.timer(timeUnits::msec);
deltaTime=timeNew-timeOld;
timeOld=Brain.timer(timeUnits::msec);
FILE* usd_file_write = fopen("log.txt", "a");
if( usd_file_write ) {
fprintf(usd_file_write, "wait(%d, msec)\n", deltaTime);
fclose(usd_file_write);
}
wait(20, msec); // Sleep the task for a short amount of time to
// prevent wasted resources.
}
}
2 Likes
Thank you for your help. Here is the updated code and I did see that fopen failed. I use the same SD card with other codes and VEXOS can write to this SD card. I do not think it is SD card issue. And the brain recognized the SD card
Is there anything else I need to change? printf( "sdcard exist %d", Brain.SDcard.isInserted());
shows 1
.
void usercontrol(void) {
// User control code here, inside the loop
FILE * usd_file_write;
usd_file_write=fopen("log.txt", "w");
if( usd_file_write ) {
fprintf(usd_file_write, "/* log file for debug */\n");
fclose(usd_file_write);
printf("success\n");
} else printf("fail\n");
printf( "sdcard exist %d", Brain.SDcard.isInserted());
int timeOld=Brain.timer(timeUnits::msec);
int timeNew ;
int deltaTime;
while (1) {
timeNew=Brain.timer(timeUnits::msec);
deltaTime=timeNew-timeOld;
timeOld=Brain.timer(timeUnits::msec);
FILE* usd_file_write = fopen("log.txt", "a");
if( usd_file_write ) {
fprintf(usd_file_write, "wait(%d, msec)\n", deltaTime);
fclose(usd_file_write);
}
wait(20, msec); // Sleep the task for a short amount of time to
// prevent wasted resources.
}
}
idk, I tried the code I posted and it works.
2 Likes
Thank you. It did work with another SD card (Samsung 128 GB PRO)!
The old card I used is Kingston 1GB and it works with std::ofstream ofs;
, but not the code above.
As long as the SD card format is Fat32 it should not matter. Inside vexos, fopen, std::ofstream and sdcard. savefile all end up in exactly the same code.
2 Likes