xiaohh
Hi
 
I'm wondering the purpose of struct MBinfo in ModbusSlave.cpp, I think this struct is not must have, for example, for register read/write function, variable *int_memory[] can be accessed directly.
But I guess there should be some good reasons.
thiagoralves
*int_memory is a pointer. The variable alone means nothing. It needs to point to something. In this case it should point to either hardware IO or Modbus data. MBinfo is what holds the Modbus data
Reply 0 0
xiaohh
I want to make it more clear with a holding register write example.
If we write 'value' to register 'addr', it can be written in this way,
*int_memory[addr] = value;
 
It means MBinfo is not needed, and some memory can be saved.
Reply 0 0
thiagoralves
*int_memory[addr] is a pointer. If it doesn't point to anything, an attempt to write 'value' to it will result in a crash due to nullptr deference. MBinfo creates the "something" that the *int_memory[addr] is pointing to.
Reply 0 0
xiaohh
int_memory[10] is a pointer which equals __MW10, and points to ____MW10,
*int_memory[10] is a register value which equals to ____MW10.
Reply 0 0
thiagoralves
Correct, and that's how the architecture works. If a variable is mapped on that register, the pointer points to the variable instead of the malloc'ed data from MBinfo. This way, we have multiple sides of the firmware (modbus, I/O, ladder) accessing the same information through pointers. This simplifies the architecture significantly. But if there isn't any variable allocated to that register in the user program (i.e. you didn't define %MW10 anywhere), then this register must still exist, and it is tied to the data created by MBinfo. Indeed, some memory could be saved if we dynamically dealloc all defined variables, but the gain is too little for quite a bit of effort and possibly new bugs to be traced.
Reply 0 0
xiaohh
Thanks.
Reply 0 0
Reply