Community
cancel
Showing results for 
Search instead for 
Did you mean: 
JJose21
New Contributor I
924 Views

REALLY WEIRD "fix" to File Read (fgets) in Edison

I've been working on a system that reads data from the Internet "asynchronously" in Arduino IDE. My reference implementation is a home security prototype.

The way the async calls work is making a system call to a nodejs script that reads data from the internet on a forked process and places the results (and progress updates) to a text file.

So the system constantly checks on that file for its latest value.

But for some reason it was reading garbage at times; at some point it was reading ONLY garbage, which made the approach unuseful.

But! by accident, it started to work flawlesly, zero garbage, only meaningful reads to the file, even in very stressful conditions.

I say by accident because the code that makes it work is quite unrelated to the file read process.

The issue is documented here: https://github.com/janunezc/pvcloud/issues/24 https://github.com/janunezc/pvcloud/issues/24

The code that makes it work is this (in red):

void loop() {

/* BEGIN: THE FOLLOWING CODE COULD NOT BE MOVED OUT TO A FUNCTION BECAUSE THE SYSTEM LOSES ITS ABILITY TO READASYNC PVCLOUD */

String curMillis = String(millis()); //(!!!) Initializing curMillis to "0000" causes a lot of unrelated garbage on file read.

String header = "TOMBO(" + CheckChar + ")" + curMillis; //Removing curMillis variable DOESN'T seem to cause garbage in file read.

lcdOut(header,0);//(!!!) calling lcdOut(header) overloaded method causes unrelated garbage on file read!!!

/* END: THE PRECEDING CODE COULD NOT BE MOVED OUT TO A FUNCTION BECAUSE THE SYSTEM LOSES ITS ABILITY TO READASYNC PVCLOUD */

lcdCompositeOPMode();

detectPushButton();

asyncPVCloudUpdate();

processOPMode();

processSensorsLineChanges();

processAlarmConditionChanges();

}

Even if I move that code to the end of loop, garbage comes back.

The process that reads the file is in my so-called pvcloud_arduino_library in the same GIT repo (use development branch)

https://github.com/janunezc/pvcloud/blob/development/pvcloud_arduino_library/pvcloud_lib.cpp https://github.com/janunezc/pvcloud/blob/development/pvcloud_arduino_library/pvcloud_lib.cpp

String PVCloud::Check(String label){

String fileName = asyncFilePath + "/out_pvcloud_";

if (label != ""){

fileName += label;

}

else {

fileName += "any";

}

fileName += ".txt";

FILE *filePointer;

filePointer = fopen(fileName.buffer, "r");

char fileContent[200];

fgets(fileContent, 200, filePointer);

String value = fileContent;

value.trim();

fclose(filePointer);

return value;

}

5 Replies
JJose21
New Contributor I
85 Views

It seems I wont obtain a successful scientific explanation of why this happened, nor of why it got fixed by adding "b" to the parameters of the fopen command.

The error disappeared on this commit https://github.com/janunezc/pvcloud/commit/c4531c14e894001697ff6cb38c372c3e619cabb7 https://github.com/janunezc/pvcloud/commit/c4531c14e894001697ff6cb38c372c3e619cabb7

Anyone with a good explanation of how "r" and "rb" modes work will be highly admired.

Best Regards,

--jn

Pablo_M_Intel
Employee
85 Views

Hi crmakers,

I guess you already did some research on this, but this might help some other users with a similar question. fopen() with r as an argument will open a text file for reading, fopen() with rb as an argument will open a file in binary mode for reading. Using rb as an argument will work for non-text files. There's also this short explanation about the difference between text and binary I/O: http://c-faq.com/stdio/textvsbinary.html http://c-faq.com/stdio/textvsbinary.html.

Regards,

PabloM_Intel

JJose21
New Contributor I
85 Views

Thanks Pablo,

I understand that not using "b" instructs the libraries to do TEXT type of reads, while using "b" would handle them as binary.

The interesting thing is that, I am writing text as far as I understand. But the system works using "rb" and fails using just "r"

Anyway, I am staying with the version of my code that works and will do more research in the future of why this seems to be working "the other way around" of what it should be.

JJose21
New Contributor I
85 Views

Furthermore, the real question is how a piece of code that seems to be unrelated would have an impact on the results of the read operation.

Pablo_M_Intel
Employee
85 Views

Hi crmakers,

After doing a little more research on this matter, I found this thread from VBforums that you might find helpful, http://www.vbforums.com/showthread.php?395162-fopen-s-quot-r-quot-vs-quot-rb-quot http://www.vbforums.com/showthread.php?395162-fopen-s-quot-r-quot-vs-quot-rb-quot.

Regards,

PabloM_Intel

Reply