Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

PSoC™ 6 Forum Discussions

lussmar
Level 1
First question asked Welcome!
Level 1

Hello! Im currently trying to read a JSON file in my psoc project for the cyproto-063-BLE but i cannot get it to work. 

I have a JSON file that im trying to include in the project, read from and extract the data, im using the tiny-json parser (https://github.com/rafagafe/tiny-json). The parser works as intended, if i manually store the JSON as a string, i can parse and extract data as i please. The issues arise when i try to include the .json file into the project and open/read from it. 

Here is a code snippet 

static const char filename[] = "test.json";
FILE *file = fopen ( filename, "r" );
char buffer[128];

fread(buffer, 128, 1, file);
printf("line = %s\r\n", buffer);
fclose(file);

json_t what[32];
json_t const* json2 = json_create( buffer, what, sizeof what / sizeof *what );
char const* name = json_getPropertyValue(json2, "Name");
printf( "Name: %s.\r\n", name );

 

I have included the file under source files, as well as added the filepath to Project->Build settings->CM0+->Compiler->Additional Include Directiories. 

Following the debugger the program crashes at line "fread(buffer, 128, 1, file);"

 

Does anyone have any input on how to read and store the JSON file in psoc programmer, the file opening and reading works well when isolated in a regular C file ran through the terminal. I'll attach a test project where i try to read a json test file and print it to serial monitor using UART. Relevant header and json files are in a folder in the project called External_Code.

Any help is greatly appreciated!

//Arvid

0 Likes
1 Solution
AlenAn14
Moderator
Moderator 100 solutions authored 250 replies posted 250 sign-ins
Moderator

Hi @lussmar ,

The issue here is caused due to the fact that PSoC Creator does not support files of the extension .json.
The 'Additional Include Directiories' only looks upd .c and .h files and not .json files and hence even if you add the same its pointless as the compiler will simply ignore this file and hence the .json file is not actually present in the output binary file after build.

Also the code snippet "FILE *file = fopen ( filename, "r" );" returns a 0 value always in an embedded c compiler irrespective of the filename you use as an embedded device has no notion of filesystems like a processor running an OS ( windows OS) . This code is mainly meant for usage in OS devices like your PC as only in such an OS is the concept of file's present where you can read and write to. By using this code snippet, you are basically expecting a 1MB ROM/flash embedded device to have a filesystem like that is supported by your 500GB hdd/ssd PC which is impractical.

Mainly what I mean to say is that, if you give an embedded device a file name, it has no inbuilt code for a filesystem structure to know  where to look for it unless you implement such a filesystem on it.

Hence the result of the line "fread(buffer, 128, 1, file);" is basically same as "fread(temp, 128, 1, 0);" which will cause a hardfault error in the embedded system.

Warm Regards
Alen

View solution in original post

0 Likes
1 Reply
AlenAn14
Moderator
Moderator 100 solutions authored 250 replies posted 250 sign-ins
Moderator

Hi @lussmar ,

The issue here is caused due to the fact that PSoC Creator does not support files of the extension .json.
The 'Additional Include Directiories' only looks upd .c and .h files and not .json files and hence even if you add the same its pointless as the compiler will simply ignore this file and hence the .json file is not actually present in the output binary file after build.

Also the code snippet "FILE *file = fopen ( filename, "r" );" returns a 0 value always in an embedded c compiler irrespective of the filename you use as an embedded device has no notion of filesystems like a processor running an OS ( windows OS) . This code is mainly meant for usage in OS devices like your PC as only in such an OS is the concept of file's present where you can read and write to. By using this code snippet, you are basically expecting a 1MB ROM/flash embedded device to have a filesystem like that is supported by your 500GB hdd/ssd PC which is impractical.

Mainly what I mean to say is that, if you give an embedded device a file name, it has no inbuilt code for a filesystem structure to know  where to look for it unless you implement such a filesystem on it.

Hence the result of the line "fread(buffer, 128, 1, file);" is basically same as "fread(temp, 128, 1, 0);" which will cause a hardfault error in the embedded system.

Warm Regards
Alen

0 Likes