News:

  • October 13, 2025, 11:04:44 AM

Login with username, password and session length

Author Topic: Copying a Block of Bits from a Structure  (Read 1745 times)

Bolt

  • Hero Member
  • *****
  • Posts: 591
Copying a Block of Bits from a Structure
« on: January 11, 2021, 08:40:04 AM »
I realize I  can't cast within a structure.  However, if I create a block of Bits in the structure with a specific layout, say lined up on dw0:W0 and dw0:W1, can I refer to those words in a MOVE, MEMCOPY or similar instruction?  I can copy a block of casted bits to a structure's word, and a structure's word to a block of casted bits, but how do I copy a block of structure bits to a block of casted bits?
Can I "overlap" the memory layout, have coils with dw:0, dw:1, etc addresses, then create a word with dw:W0 address?  It's not telling me no when creating such layout, but I haven't uploaded it.  Sounds dangerous.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6126
  • Yes Pinky, Do-more will control the world!
Re: Copying a Block of Bits from a Structure
« Reply #1 on: January 11, 2021, 09:20:40 AM »
I realize I  can't cast within a structure.  However, if I create a block of Bits in the structure with a specific layout, say lined up on dw0:W0 and dw0:W1, can I refer to those words in a MOVE, MEMCOPY or similar instruction?  I can copy a block of casted bits to a structure's word, and a structure's word to a block of casted bits, but how do I copy a block of structure bits to a block of casted bits?
Can I "overlap" the memory layout, have coils with dw:0, dw:1, etc addresses, then create a word with dw:W0 address?  It's not telling me no when creating such layout, but I haven't uploaded it.  Sounds dangerous.

You can overlap. It is quite useful to do so. In C, that is referred to as a union.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: Copying a Block of Bits from a Structure
« Reply #2 on: January 11, 2021, 09:23:49 AM »
Can I "overlap" the memory layout, have coils with dw:0, dw:1, etc addresses, then create a word with dw:W0 address?  It's not telling me no when creating such layout, but I haven't uploaded it.  Sounds dangerous.
Absolutely.

You have to manually configure a DWORD (or WORD or BYTE) on top of the set of bits in the UDT editor.  Say you want to call it Flags.  It is a new field, but the DWORD offset is the same DWORD offset as the bits.  If you have just a BYTE or WORD, you would then also specify the BYTE offset (0..3) or WORD offset (0..1) for the specific BYTE or WORD.

If you see the first attachment (UDTFlags1.png), it shows an example of a UDT with some bits and a REAL.  I then added a Flags BYTE that Manually specifies the Layout to lay ON TOP OF the bits that exist.  The UDT dialog lists out the memory offset in the Layout column (e.g. .FlagsA is dw0:0 for bit 0 of the first 32 bit DWORD, or .ARealValue is at dw1, or .Flags is dw0:B0, or the first BYTE of the first DWORD.

The 2nd attachment (UDTFlags2.png) is the Memory Layout (see the View Memory Layout button on the UDT dialog).  It helps you see the memory layout VISUALLY.  The first column is the DWORD offset within the structure.  Hence, dw0 has a bunch of fields in it - some flags, but then also a BYTE that overlaps ON TOP OF the same memory of the bits.  This is sort-of a cast.  In the C programming language, this is called a "union".  Hence, .Flags shares the same memory as .FlagsA, .FlagsB, .FlagsC (with 5 unused bits in that BYTE).  This .Flags structure member could had its layout manually configured as a WORD or the entire DWORD.

The Date/Time structure uses this mechanism for the Year/Month/Day as a .Date DWORD member on top of the WORD/BYTE/BYTE layout of those members.  The DayOfWeek/Hour/Minute/Second has a .Time DWORD member on top of those BYTE/BYTE/BYTE/BYTE struct members of the Date/Time structure.

The Memory Layout dialog helps you VISUALLY see the specified memory layout.  This could also be useful if you were going to use MEMCOPY to copy an entire structure on top of Modbus memory (e.g. MHR).  Then you could visually see how to crack the "structure" using casts in MHR memory that matches the exact layout in the original structure.  You can see ANY structure type, both user-defined and built-in structures.

Bolt

  • Hero Member
  • *****
  • Posts: 591
Re: Copying a Block of Bits from a Structure
« Reply #3 on: January 11, 2021, 09:32:54 AM »
Great!  So I answered my own question, but was too afraid to try it!  Thanks for clarifying.