I'm developing a simple RS485 based MODBUS slave application on a Microchip PIC micro-controller and need to know best practices to follow on its application side. I can guide myself to get the hardware layer working.
From my understanding, the MODBUS slave has a database of application specific registers upon which the master performs R&W operations through commands.
Now this database needs to be run-time updatable based on device operation and should also be persistent so that device status can be recovered after power reset. Also, the master can ask for register read in contiguous manner (for e.g. read 10 registers from ID 2121). Based on all the requirements, I'm thinking of creating two copies of the database - one on RAM and the other on flash / eeprom. The one on RAM would simply be a set of arrays and the one on flash / eeprom would be block of memory locations. Upon power-up, contents from the flash will be copied to the RAM locations and all the run-time R&W operations thereon will be performed on the RAM copy itself. Now, there could be two ways to transfer the latest data from RAM to flash:
Do it instantly when any of the data changes
- Master will be notified about operation success only when data is transferred to NV memory
- Could invite too many flash/eeprom operations
Do it periodically
- Less transactions with the NV memory
- There could be data loss if power goes down before transfer was complete, and worse master would think that data was successfully written to the slave
Is my understanding correct till now? If yes, which of the two ways is usually followed? Or is there a better one?