Read values from .txt
-
Hi
I’m working on a project where I need to store some calibration parameters in a file within a /calib/ directory on the SD card. I was wondering if there’s a way to access the data from that file and load it into an array variable for further use. Any guidance or suggestions would be greatly appreciated. Thanks in advance! -
@Paski there is no facility to read from a general text file, however you can read from a single-line CSV file. See function
fileread
at https://docs.duet3d.com/en/User_manual/Reference/Gcode_meta_commands#functions. -
I think the current "RRF-ish" way to store parameters would be to write each to the file as a gcode statement that will create the value in a global variable.
So if you have three calibration values you need to store : calib1 = 10, calib2 = 20, calib3 = .2
You could write them in a file called saved_values.gecho >"saved_values.g" set global.calib1 = 10 echo >>"saved_values.g" set global.calib2 = 20 echo >>"saved_values.g" set global.calib3 = .2
Note the use of a single ">" on the first line which overwrites any previous file contents and ">>" on the others that appends a new line to the file.
To read in the values, you first create the global variables, then run the macro. This code is either in your config.g or is run when you want the saved values:
global.cabli1 = null ; creates the variable and assigns it a dummy value global.cabli2 = null global.cabli3 = null M98 P"saved_values.g"
In the M98 command, it's ideal to include the file location (something like 0:/sys/saved_values.g if the macro is saved on the local SD card and is in the "sys" directory)