Différences entre versions de « ENG-CANSAT-PICO-LOG »
Ligne 61 : | Ligne 61 : | ||
So, even if LittleFS do have some minimal buffering, it is possible to force writing to the flah ( with {{fname|flush()}} ). So the user code can rely on better file-system stability and better control over forced writing operation. | So, even if LittleFS do have some minimal buffering, it is possible to force writing to the flah ( with {{fname|flush()}} ). So the user code can rely on better file-system stability and better control over forced writing operation. | ||
+ | |||
+ | The following example just {{fname|flush()}} writes at every line written to the file. | ||
+ | |||
+ | Power can be lost at every moment, the flushed writes are safely stored. | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
Ligne 73 : | Ligne 77 : | ||
{{underline|Please note:}} abusing the {{fname|flush()}} operation will slow down the code and will degrade flash faster (Flash Memory cells do have a maximum write cycles). | {{underline|Please note:}} abusing the {{fname|flush()}} operation will slow down the code and will degrade flash faster (Flash Memory cells do have a maximum write cycles). | ||
+ | |||
+ | [[Fichier:LittleFS-flushed.png]] | ||
{{ENG-CANSAT-PICO-TRAILER}} | {{ENG-CANSAT-PICO-TRAILER}} |
Version du 8 mars 2022 à 00:19
MicroPython File System
MicroPython board does fits an internal file system. This means that Python language on a MicroPython board can also manage files!
On a MicroPython board, you can manage files and directories. Creating them, deleting them, adding data to them, etc.
As any good implementation of Python, MicroPython can manage text files and binary files.
MicroPython file system (~100Kio) is not as large as a computer file system (10+ Gigabytes). Do not manipulate/write large amount of data on a MicroPython file-system. |
Data Logging on MicroPython
Doing data logging with MicroPython is quite easy. You just open a file and write data into it like you will do with any Python code.
Writing data to text file
The following example adds lines to the 'file.txt'. The with statement will automatically close the file when the code gets out of the with scope.
with open( 'file.txt', 'a' ) as _file:
_file.write( "Hey, it is MicroPython\r\n" )
Notes:
- The 'a' parameter in the open() call stand for "append". So executing several times the code will append the same text again and again to the file.
- IF the file doesn't exists yet THEN it will be automatically created.
- Use the 'w' parameter (instead of 'a') to overwrite the file content.
- As the write() just writes bytes then the the carriage return and line feed (\r\n) must also be written.
The following screen capture do show the 'file.txt' on the pico file system (do not hesitate to refresh the file list) and the file content is displayed on the file pane.
File-system, Writing and data lost
The computer file-system:
When working with file on a computer it is essential (even critial) to close the file to be ensured that all data are written to the file-system.
Ensuring good performance on a computer do involves lot buffering mechanism at software and hardware levels.
Cutting the power on a computer usually results into missing data or corrupted files.
Note: this is not true for databases which ensure and 200% the log file writing before applying changes to the database.
SD Card file system:
Regardless of the platform using the SD card, the content and stability of a SD card is veryyyyyyy sensitive to the open/close operation on the file-system. Users of the Raspberry-Pi nano computer knows that powering of a Pi during SD write operation could completely corrupt the file-system. In some rare situation the SD card can be made completely unreadable.
Using SD-Card with microcontroler is not recommended when writing operation are involved. Using SD in write mode implies electronic skills to spare some "emergency power" for closing files on the SD card before the final shutdown.
MicroPython file-system:
The since MicroPython 1.12 (2019), the file-system is based on LittleFS.
LittleFS is a flash-based device filesystem. It has been designed to be more resistant to filesystem corruption and power failures resilient. Unless other file-systems, the RAM used by LittleFS does not depend on the file-system size (it is said "RAM bounded" without RAM allocation overflow). See LittleFS Github for more information.
So, even if LittleFS do have some minimal buffering, it is possible to force writing to the flah ( with flush() ). So the user code can rely on better file-system stability and better control over forced writing operation.
The following example just flush() writes at every line written to the file.
Power can be lost at every moment, the flushed writes are safely stored.
import time
with open( 'file2.txt', 'a' ) as _file:
for i in range( 50 ):
print( i )
_file.write( 'Hey, it is MicroPython %i\r\n' % i )
_file.flush() # Force writing operation
time.sleep( 1 )
Please note: abusing the flush() operation will slow down the code and will degrade flash faster (Flash Memory cells do have a maximum write cycles).
Written by Meurisse D. for MCHobby