This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Read data on txt file on MSP430

Other Parts Discussed in Thread: MSP430F5529

Hi, 

I'm using the MSP430F5529 and I want to read data on txt file on the internal memory of microcontroller.

I use the M1 example by TI (Single-LUN; File System Emulation)

My problem : I don't find how to read the data on multiple txt files and use this data in my program.

Have you an idea?

Thanks,

Bastien

  • Bastien Fl��ckiger said:
    I'm using the MSP430F5529 and I want to read data on txt file on the internal memory of microcontroller.

    ???

    Unless you have programmed an internal file system, MSP430 internal memory has no files.
    Do you have a text file on HD or elsewhere and want it to read into internal memory at runtime? Do you have a text file you want to include into your application binary?

    What 'M1 example' are you talking about? 'M1' for Cortex M1? What has this to do with MSP430?

    You should explain your situation and requirements in more than two sentences.

  • Thank you for your reply and your questions.


    Yes, it is an internal file, the M1 example comes from MSP430 USB Developers Package in MSC examples.

    And I copy text files from a computer via USB.

    My program must retrieve data from text files. For example at 08:00 I read the text file "eight.txt" and at 10:00 I read the data in  "ten.txt".

  • Bastien Fl��ckiger said:
    My problem : I don't find how to read the data on multiple txt files and use this data in my program.

    In order to understand what exactly you do not understand, we need to know: do you know how to read multiple txt files and use their data on PC? I am asking because basic concepts are very similar.

  • No, I never do that on PC. On Internet I found the library "stdio.h" and I tried to use the functions fopen, fgets, fscanf and fclose. But it does not work.

  • Bastien Fl��ckiger said:
    I found the library "stdio.h" and I tried to use the functions fopen, fgets, fscanf and fclose. But it does not work.

    To do  the f-funcitons, you need a file system isntalled. But the MSP does not have one by default. The whoel concept of files is unknown to the MSP by default. You have memory locations that you can read from and (in case of flash with limitaitons) write to. Nothing more.

    You either need a filesystem driver that interprets memory locations (in internal flash or external memory) as 'files' and provides the implementation for the f-functions, or you use an alternative approach:

    you can compile the text files as arrays right into your application. then you can access them like you can access any other string constant.

    For a project, I generate tables with waveforms in excel. I export teh data column them as CSV and directly include it into my code:

    unsigned int data[512]={
    #include "data.csv"
    };

    For a column with 512 values.

    If you have a text of unknown length, and do not need to know the length, jus tthe end of the text, you may use

    unsigned char text[] = {
    #include "text.txt"
    ,0};

    Then the text is included char by char and ends with a 0-byte as delimiter (compatible with C strings)

  • Thank you for your reply.


    But this solution can not work. Because I dispute 255 text files that I need to recover the data.
    And with this solution the compiler gives me error in the line "# include" file1.txt "because it is not yet present in memory, they will be copied later by USB (when my circuit is programmed and the program runs, I connect the USB to the PC to copy the 255 text files in the memory).


    Unless there is a way to copy the text file before (or simultaneously) to a build?

  • You need file system. Perhaps FatFs by ChaN will suit your needs: http://elm-chan.org/fsw/ff/00index_e.html

  • Ilmars said:
    You need file system.

    Not necessarily. If the data is sent to the application while running, it can be stored anywhere. In a file system or at a dedicated location. The latter has less overhead and faster access, but of course a file system is more convenient.

  • Jens-Michael Gross said:
    If the data is sent to the application while running, it can be stored anywhere.

    We are talking about reading files, not data. Please refer to thread subject ;) Place where we store data in form of files we usually call "file system" - disregarding how simple it is.

  • Ilmars said:
    We are talking about reading files, not data. Please refer to thread subject ;)

    Wouldn'T be the first time where thread subject and requirements won' tmatch. It' sstill unclear what these files contain and what they are used for. Internally? Or just stored and delivered? Anyway, files contain eithe rnothing (zero-size) or data (non-zero size). A file system is a way to store and identify data, but not the only one and sometimes by far not the most efficient one.

  • Thank you for yours help


    I still have a problem, I read a text file and recovers data without problems with FatFs (f_open, f_read and f_close)
    But when I read another file after it the program crashes.

    Do you have any idea?

    PS: If you need my code, I can post it

  • Bastien Fl��ckiger said:
    I still have a problem, I read a text file and recovers data without problems with FatFs (f_open, f_read and f_close)
    But when I read another file after it the program crashes.

    Unfortunately I have to admit that Fatfs is too complex for "remote debugging using forum" or just guesswork to be productive. It can be virtually anything. Try to isolate where exactly crash happens, what kind of code and data involved and so on. This is what we all do - when something does not work, we search for an error using debug and deduction.

  • Ok,

    I just discovered that when I copy two text files on the memory, USB on windows. I unplug, reconnect, and control the content of files on windows. One of the two files as all character replaced by  "NUL".

    There is no problem when I have only one file in the memory.

  • Bastien Fl��ckiger said:
    I just discovered that when I copy two text files on the memory, USB on windows. I unplug, reconnect, and control the content of files on windows. One of the two files as all character replaced by  "NUL".

    That's strange. Try reformatting the file system. Maybe there' ssomething twisted here.
    Or there is a problem with the code that reads the existing allocation/directory data and adds the second file. Maybe a missing flush of the organizational informations?

  • Hello Jens-Michael,

    I am trying to do the same thing.

    I have a text file that I want to read in as character arrays.

    The text file content is below:

    W 00 05

    R 00 02

    X 200

    The text file is called program.txt

    I added this to the beginning of int main(void)

    char text[]={
    #include "program.txt"
    ,0};

    When I try to build on to the MSP430F5529 it will not build successfully, it generates the following error::

    **** Build of configuration Debug for project MSP430F55xx_1 ****

    "c:\\ti\\ccsv6\\utils\\bin\\gmake" -k all
    'Building file: ../MSP430F55xx_1.c'
    'Invoking: MSP430 Compiler'
    "c:/ti/ccsv6/tools/compiler/msp430_4.3.1/bin/cl430" -vmspx --abi=eabi --include_path="c:/ti/ccsv6/ccs_base/msp430/include" --include_path="c:/ti/ccsv6/tools/compiler/msp430_4.3.1/include" --advice:power=all -g --define=__MSP430F5529__ --diag_warning=225 --display_error_number --diag_wrap=off --silicon_errata=CPU21 --silicon_errata=CPU22 --silicon_errata=CPU23 --silicon_errata=CPU40 --printf_support=minimal --preproc_with_compile --preproc_dependency="MSP430F55xx_1.pp" "../MSP430F55xx_1.c"
    "..\program.txt", line 1: error #20: identifier "W" is undefined
    "..\program.txt", line 1: error #68: expected a "}"
    "..\program.txt", line 4: warning #1-D: last line of file ends without a newline
    "../MSP430F55xx_1.c", line 7: warning #12-D: parsing restarts here after previous syntax error
    "../MSP430F55xx_1.c", line 5: remark #1535-D: (ULP 8.1) variable "text" is used as a constant. Recommend declaring variable as either 'static const' or 'const'
    2 errors detected in the compilation of "../MSP430F55xx_1.c".

    When I take out the include statement, it can compile and build again.

    Am I doing something wrong here?

    Thanks

    Kang Kang

  • What do you think the compiler will do when you give it a source code like

    Char text[] = {W 00 05
    R 00 02
    X 200,0};

    Right: I will tell you that ‘W’, ‘R’ and ‘X’ are undefined and it cannot compile this at all.

    Of course the content off the text file must be in a format that is a valid initializer and can be compiled.

    So the file content must be either (including all quotes)

    “W 00 05\r\nR 00 02\r\nX 200”

    Or

    ‘W’,0,5,’R’,0,2,’X’,200

    Depending on what you want to be in your string. Likely the first variant (in which case the trailing 0 isn’t necessary, as string constants are always terminated with an implicit 0)

    The include directive just replaces itself by the content of the file, as if the file content were typed into the source code. The result must be compilable source code.

  • Hello, 

    I thank you very much for this solution. It is simple. 

    But, I tested it and I have a little problem. When I upload the code into the MSP, I have an error indicating that the csv file does not exist eventhough I placed it in the same directory of the .ino.

    Could you please tell me how th solve this problem. Where should I place my csv file or how I can be exported ?

    Thank you !!!!! 

    Alice

  • How do you include it?
    If you use
    #include <data.csv>, the compiler will look in the common include folders only. If you use #include "data.csv", it should search the project folder first.
    It might be that the IDE requires some special handling, like adding the file to the project (like any other project-specific ".h" header file)

**Attention** This is a public forum