PLCnext on LinkedInPLCnext on Instagram  PLCnext on YouTube Github PLCnext CommunityStore PLCnext Community

  1. jnorris
  2. PLCnext Engineer
  3. Monday, 19 October 2020

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.

Martin PLCnext Team Accepted Answer Pending Moderation
0
Votes
Undo

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.

Phoenix Contact Electronics Headquarters - PLCnext Runtime Product Management and Support

  1. more than a month ago
  2. PLCnext Engineer
  3. # 1
jnorris Accepted Answer Pending Moderation
0
Votes
Undo

How can I specify the write location as an SD card?

  1. more than a month ago
  2. PLCnext Engineer
  3. # 2
jnorris Accepted Answer Pending Moderation
0
Votes
Undo

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.

  1. more than a month ago
  2. PLCnext Engineer
  3. # 3
Martin PLCnext Team Accepted Answer Pending Moderation
0
Votes
Undo

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.

Phoenix Contact Electronics Headquarters - PLCnext Runtime Product Management and Support

  1. more than a month ago
  2. PLCnext Engineer
  3. # 4
  • Page :
  • 1


There are no replies made for this post yet.
However, you are not allowed to reply to this post.