modular-psu-firmware icon indicating copy to clipboard operation
modular-psu-firmware copied to clipboard

Add file read/write capabilities to micropython

Open fietser28 opened this issue 3 years ago • 3 comments

I would like to be able to manually store data to no volatile memory (= SD card) from my micropython scripts. This is for storing settings and maybe logging raw data.

  • [x] For this I would like to be able to open/close/read/write data to files from micropython. There probably are file libraries for micropython available.

  • [x] It should be easy to avoid filename conflicts, so there probably should be a way to find out the name of the current running script to enable the script to generate a filename like '

  • [ ] Have a best practice example script (maybe combined with #151)

Storing settings (and maybe state) on non volatile memory is also needed for #151

fietser28 avatar Oct 19 '21 11:10 fietser28

second item will be resolved with SCRipt:RUN? implementation (in version 1.8).

fietser28 avatar Oct 19 '21 11:10 fietser28

@mvladic showed this already can be done. First item is already possible and done.

fietser28 avatar Oct 20 '21 18:10 fietser28

Here is the MicroPython code to read and write setting to a file on the SD card using SCPI commands:

# settings is a list of strings
settings = ["settings1", "settings2", "settings3", "0", "42"]

SETTINGS_FILE_PATH = "/Scripts/my_script.settings"

# read settings list from the file
def read_settings():
    try:
        settings_str = scpi('MMEM:UPL? "' + SETTINGS_FILE_PATH + '"')
        skip = 2 + int(settings_str[1])
        settings_str = settings_str[skip:]
        settings = settings_str.split("\n")
    except:
        pass

# write settings list to the file
def write_settings():
    settings_str = "\n".join(settings)
    settings_str_len = str(len(settings_str))

    scpi('MMEM:DOWN:FNAM "' + SETTINGS_FILE_PATH + '"')
    scpi('MMEM:DOWN:SIZE ' + settings_str_len)
    scpi('MMEM:DOWN:DATA #' + str(len(settings_str_len)) + settings_str_len + settings_str)
    scpi('MMEM:DOWN:FNAM ""')

https://www.envox.eu/eez-bench-box-3/bb3-scpi-reference-manual/5-subsystem-command-reference/bb3-scpi-mmemory/#mmem_down_data

https://www.envox.eu/eez-bench-box-3/bb3-scpi-reference-manual/5-subsystem-command-reference/bb3-scpi-mmemory/#mmem_upl

mvladic avatar Oct 20 '21 21:10 mvladic