Does anyone have a good example for writing and reading data to a file using the FILE commands? I want to write the value of an array 1-16 of BOOL to a file. I’ve tried creating a read and write buffer of array of two BYTE and then I set each ,X of the write BYTE array equal to an element of my BOOL array. I can run the file commands with no errors, but after reading my read buffer doesn’t change.
Hello, Here is a simple example I did a while back showing how to write to a text file: https://www.plcnext-community.net/en/discussions-2-offcanvas/example-for-saving-data-in-a-file.html That was written in ST but I hope you can see how to use the same functions in LD, if necessary. I don’t have an example to hand that shows the corresponding read commands, so please let us know if you aren’t making any progress with those. ~ Martin.
How can I specify the write location as an SD card?
I guess I could better describe what i am trying to do. I am trying to save recipe data to a CSV or txt file on the SD card because the PLCnext doesn’t have enough retained memory. So I have several BOOL arrays which help determine which inputs a certain recipe uses, and several strings which contain basic info like the name and step descriptions. So far I can create the file on the PLC internal memory, but I can’t figure out how to write to a card or how to write mixed data to the file. I am doing it all in structured text. EDIT: I did finally get writing and reading the BOOL arrays to work, I had my size wrong.
how to write to the card
I guess that you are writing the file to a directory somewhere under
/opt/plcnext/. If you have an SD card installed, then any file created there will be written to the SD card automatically.how to accurately write the values of the BOOL array
The two obvious options are as the characters ‘0’ or ‘1’ (ASCII 48 and 49), or the strings ‘TRUE’ or ‘FALSE’. My initial thought would be to pass each BOOL through a TO_STRING function (resulting in ‘TRUE’ or ‘FALSE’) and then a STRING_TO_BUF function to get it into the buffer that you will write to file.
how to write mixed data to the file.
I’m not sure what you mean … you will need to know the exact format of the CSV file when reading it, including the order and type of every field on every line. If this is not possible, then perhaps CSV is not the best format, and perhaps you could consider something like JSON, maybe using the JSON utility library in the PLCnext Store. Hope this helps. ~ Martin.
you write:
Hello,
Here is a simple example I did a while back showing how to write to a text file:
The link:Does anyone have a good example for writing and reading data to a file using the FILE commands? I want to write the value of an array 1-16 of BOOL to a file. I’ve tried creating a read and write buffer of array of two BYTE and then I set each ,X of the write BYTE array equal to an element of my BOOL array. I can run the file commands with no errors, but after reading my read buffer doesn’t change.
points to this post and contains no ST code nor any example.
CSV is a very common format for which read and write support is also very common
Upon writing, the library code would identify each of the data types of the variables to be written
and convert them to the required Strings and/or Buffer and then write the buffer to the file.
Usually upon creating a new file, the names of the variables, separated by commas, would be written
to the first line of the file as a header.
Upon reading, the buffer would be read from the file, up to the next newline,
and then that buffer would be parsed up to the next comma and each segment converted to the
associated data type.
I think this thread has the missing code you’re referring to:
Forum - PLCnext Community PLCnext Community (Unfortunately links and images in older posts broke when this forum moved to a new platform).









