Array of Strucure in MODBUS communication

Y

Thread Starter

Yugal

Hi,
I need to transfer some chunk of data through modbus (modbus over TCP) communication.
I have an array of structure in my application(in C++) i.e.

struct{
int al
float b;
int c;
}
MyStruct;
MyStruct ABC[200];

now i need to send this data to other devices which also have the same structure in application.
Please guide me to create a map of registers so that i can send this chunk of data with minimum number of commands.

thanks in advance,
Yugal
 
You won't be able to send all of that in one message, as it would take too many registers to fit. You will have to split it up over several.

As for creating a register map, you just need to use as many consecutive registers as it takes to fit the data. That will depend on the size of your ints and floats which I can't tell from looking at your source code. You'll have to decide what size you want your variables to be and then either use a union, or a series of shifts and masks to split them up into 16 bit words.

What you are going to want to do is to write a function which stuffs each value in the struct into one or more registers (depending on the size of each variable), and then iterate over the array doing this. That way you don't have to deal with struct padding and alignment issues.
 
> You won't be able to send all of that in one message, as it would take too many registers to fit. You will have to split it up over several. <

> As for creating a register map, you just need to use as many consecutive registers as it takes to fit the data. <

---- snip ----

>What you are going to want to do is to write a function which stuffs each value in the struct into one or more registers (depending on the size of each variable), and then iterate over the array doing this. That way you don't have to deal with struct padding and alignment issues. <

Can you please give me more details about mapping the above structure. Let's say that int is 4 bytes and floats are also 4 bytes.
in my thinking mapping will look like:

40001 ABC[0].a1
40003 ABC[0].b
40005 ABC[0].c
40007 ABC[1].a1 and so on...
This is just to get clarity on mapping this structure.

Please guide me one this subject.

Thanks
Yugal
 
Yes, you can certainly just use 2 consecutive registers per variable, with the next variable following immediately after. Since you said you want to transfer the data with as few transactions a possible, you don't want to leave any gaps in the register map.
 
Top