I have a few questions regarding a project I’m currently working on.
I am developing a system to save XML recipes onto a USB stick connected to a PLC. Saving works fine so far, but I would like to add a feature that allows the user to assign a name to each recipe.
To do this, I created a string array where all the recipe names are stored. When loading a recipe, I retrieve the name from this array.
I have two questions:
Dynamic table display:
I am using a table widget to display the recipe names. If, for example, only the first 10 entries in the array are filled and the rest are empty, how can I make the table display only the filled entries and hide the empty rows?
Deleting files from the USB stick:
What is the recommended way to delete files directly from the USB stick connected to the PLC?
Dynamic table display:
Table widget has a property “tableConfiguration” that can be binded to a string variable with the correct JSON syntax.
Check this property in Table documentation for a description of the syntax and the possibilities
Here is a code I used in a project to generate this JSON with functions from AsBrStr :
// Generate configuration of Table widget to hide empty rows
// based on this syntax (see Table widget documentation) : {"specRows": [ {"from":0, "to":10, "visible":false} ]}
brsmemset(ADR(TableConfig), 0, SIZEOF(TableConfig));
brsstrcat(ADR(TableConfig), ADR('{"specRows": [{"from":'));
tmpStr1 := UINT_TO_STRING(RecipeNum);
brsstrcat(ADR(TableConfig), ADR(tmpStr1));
brsstrcat(ADR(TableConfig), ADR(', "to":'));
tmpStr1 := UINT_TO_STRING(REC_REC_NUM);
brsstrcat(ADR(TableConfig), ADR(tmpStr1));
brsstrcat(ADR(TableConfig), ADR(', "visible":false}]}'));
In this example, TableConfig variable is generated in a way that widget should hide rows from “RecipeNum” to “REC_REC_NUM” :
TableConfig is the string variable binded to the widget,
RecipeNum is the index of the first row to hide,
REC_REC_NUM the maximum index of the array
Deleting files from the USB stick:
If you already managed to detect the USB stick and save files into it, you have done the worst part of the job For deleting files you can use the FB FileDelete from FileIO library.
It just needs name of the file device and name of the file.
Thank you for the code and the explanation — it worked perfectly for me.
I also noticed the file handling function blocks, and I was wondering if you know whether there is a function that can read all file names from a USB stick (or from a specific folder on it), so I can display them in a table.
I came across DirRead, but from the documentation, I understood that it only reads one file name at a time rather than the entire folder. I might have misunderstood it, so I’m not completely sure. Do you have any suggestions on how to handle this?
Thanks again for your help — I really appreciate it.