Better Signal Mapping

5 days ago
5 days ago
  • mike-at-gms

    mike-at-gms - 5 days ago

    Hi,

    I am trying to cleanup some variable mapping. I have an I/O device that has its points defined as BIT. I would like to map these into an array of something but I cannot use an array of BIT. Mapping to an array allows me to access bits through a common FB so I only need to map the points once.
    I have created a Union based on a Forum response (https://forge.codesys.com/forge/talk/Engineering/thread/9613d49992/) and it seems to do what I would like. However, when I map the bits to my device, I get a C0355 warning (A single bit cannot be referenced. A reference to the complete byte will be stored.).

    What does this warning mean and is there a better way to map this?

    Thank you

    My code:
    TYPE Output_Byte :
    STRUCT
       Bit0 : BIT;
       Bit1 : BIT;
       Bit2 : BIT;
       Bit3 : BIT;
       Bit4 : BIT;
       Bit5 : BIT;
       Bit6 : BIT;
       Bit7 : BIT;
    END_STRUCT
    END_TYPE
    
    TYPE Output_Union :
    UNION
       //Note: I added the array here as a test. I get the same C355 warning even without.
       aByte : ARRAY[0..9] OF BYTE;
       Bytes : ARRAY[0..9] OF Output_Byte;
    END_UNION
    END_TYPE
    
    Usage in Global:
       testBytes    : Output_Union;
    
    Usage in my device map:
    Application.Global.testBytes.Bytes[0].Bit2
    

     

    Last edit: mike-at-gms 5 days ago
  • i-campbell

    i-campbell - 5 days ago

    Try

    testBools : ARRAY [0..9] OF BOOL;

     
  • mike-at-gms

    mike-at-gms - 5 days ago

    Thank you @i-campbell. Using testBools[x] in the mapping does work.

    I am trying to pass these points along to a client over a network and I would like to just send a bunch of Bytes (without manually converting them). The BOOL type does not map nicely to Bytes.

    I would like to do something like:

    InputBytes : ARRAY[0..9] OF BYTE;
    

    and then use 'InputBytes[0].2' in the mapping. However, this will not even compile.

    Using an array of BOOL makes the network sending messy.

    The benefit of the Union is that the value of the 8 BITS is exposed in through the BYTE object (aByte in the example). If I use BOOL in the Output_Byte structure, this relationship breaks.

    I will look at it more with your suggestion in mind and see if I am approaching this wrong.

    Thank you again.

     
  • i-campbell

    i-campbell - 5 days ago

    As you have 4 distinct parameters, it will not be possible to map them to a BIT variable from the mapping dialog.
    You could try
    testBytes AT %QB200 : Output_Union;
    OR
    testBytes AT %QB400 : Output_Union;
    Depending on which addressing mode your PLC uses.

    You could also use Util.Pack() / Util.Unpack() somewhere in your code

     

    Last edit: i-campbell 5 days ago

Log in to post a comment.