modbus RTU - store some registers written by Master in EEPROM

Hi,
I have setup a modbus RTU serial slave using modbus_rtu_slave.pde from http://sites.google.com/site/jpmzometa/arduino-mbrt/arduino-modbus-slave
All of his code is listed on the modbus_rtu_slave.pde, not library based. ( I would like to use the library based but haven't got it to work yet :astonished: ) I modified the registers section for my use and everything works fine, however. I want to keep some of the registers written by the master in EEPROM so if the arduino uno restarts, reboots, power failure etc., it will retain the certain settings (i.e. slave id, temperature setpoint, baudrate, security levels, etc) Right now I declare the defaults in "void setup" but it won't retain the changes written by the master.
-----code snippets-----

#include<EEPROM.h>

enum {
MB_1,
MB_2,
MB_3,
etc...
MB_24,
MB_REGS
};

int regs[MB_REGS];

void setup()
{
/* Sets the default values here on bootup, changeable by modbus master BUT Changes will be lost on restart!!! Need to store in EEPROM /
regs[MB_1]=0, /
/
regs[MB_2]=1, /
slave id 1 thru 247*/
regs[MB_3]=19200, /* baudrate /
regs[MB_4]='n', /
parity 'n', 'e', 'o' /
regs[MB_5]=75, /
temperature setpoint in degrees /
regs[MB_6]=1, /
temperature format 0:degC, 1:degF, default=1 */
etc....

}
/*

  • I would like it to write to EEPROM when a new int write_single_register() does it's thing, I believe??? so we can get write_addr and reg_val from the master but
  • I just can't figure out where and how to insert the following..
    */

switch (write_addr) {
case 1: // regs[MB_2]
EEPROM.write(501, reg_val); // store settings above 500
break;
case 2: //regs[MB_3]
EEPROM.write(502, reg_val);
break;
case 3: //regs[MB_4]
EEPROM.write(503, reg_val);
break;
etc....
default:
// do nothing with EEPROM
break;
}

Thanks in advance!

Okay,
I have some code snippets from someone else's project that has a similar feature to get the last written register info, I haven't figured out yet how to implement it but it seems better than my original thinking. If I can get this to pull the info then I can do an EEPROM.write(). For testing I tried to get it to Serial.println() but it acts funny when I add that line. I need to be pointed in the right direction :slight_smile:

/*
*This is the slave's modbus holding register data map. Use this array
*to get or set data from/to the holding registers. This array has size
*MB_REGS.
*/
extern int regs[];

/*
*When the master changes (a) register(s), use this struct to check
*which one(s). Set written.num_regs back to zero after having checked.
*/
typedef struct lastwrite {
int start_addr;
int num_regs;
} lastwrite;
extern lastwrite written;

lastwrite written={0,0}; /* last written register(s) */

void loop()
//{
// /* This is all for the Modbus slave /
// start_mb_slave(MB_SLAVE, regs, MB_REGS);
//
// /
your code goes here /
// ...
// /
handle write event */
// if (written.num_regs) {
// ....
// written.num_regs=0;
// }
//}