Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Setting a struct data type in ModbusTCP mapping IO

jonathanD
2017-12-20
2017-12-24
  • jonathanD - 2017-12-20

    Hello,

    I have set in the ModbusTCP mapping IO a variable of struct data type, and when I try to log in I receive "Types of channel and mapped variable Application.MaskCW do not match".
    Attached a print screen of the problem.

    How can I map a variable of struct data type to the device?

    the structure is built like:

    TYPE MaskCW :
    STRUCT
       x1 : bit;
       x2 : bit;
       x3 : bit;
    END_STRUCT
    END_TYPE
    

    The declaration in a GVL list is:

    MaskCW : MaskCW;
    

    IMG: mapping problem.png

     
  • schaepper - 2017-12-22

    I cant help you directly with your problem because to me I would do it like this and I tried it out with DPDP coupler and it worked.
    But for you to temporarly be able to continue your work you can do this:

    Define a new variable in the configuration where you want to link it.
    Then use pointers

    VAR
       temporaryPointer : POINTER TO MaskCW;
       yourNewTempVariableInConfig: DWORD; This variable is the one from your config, not defined like in this codesnippet!
    END_VAR
    temporaryPointer := ADR(yourNewVariableInConfig);
    maskCWVariable:= temporaryPointer^;
    

    Maybe you know this way already but I just want to give you a workaround if not.

    I assume you don't have a variable Name MaskCW as well as a Type name MaskCW, because this would not work / compile normaly because the compiler would not be able to distinguish between MaskCW variable and the MaskCW Type.

    My 2 cents:
    Dont name your struct "MaskCW", its not clear what it means. Write what ever CW means, the name will not get "to big", this is an argument back when the displays were very small, nowadays it doesnt count anymore. I know some of this rules are in the PLCOpen guidlines, but even then a lot of stuff in there make absolutely no sense like Hungarian Notation.

     
  • teichhei - 2017-12-24

    jonathanD hat geschrieben:
    Hello,
    I have set in the ModbusTCP mapping IO a variable of struct data type, and when I try to log in I receive "Types of channel and mapped variable Application.MaskCW do not match".
    Attached a print screen of the problem.
    How can I map a variable of struct data type to the device?
    the structure is built like:

    TYPE MaskCW :
    STRUCT
       x1 : bit;
       x2 : bit;
       x3 : bit;
    END_STRUCT
    END_TYPE
    

    The declaration in a GVL list is:

    MaskCW : MaskCW;
    

    What do you mean you have mapped a struct type in the Modbus mapping? I would think that whatever you have needs to be broken down into either bits or packed in words, that's how I do it. Like using for example Application.PLC_PRG.MaskCW.x1 as an existing variable in the modbus mapping as bits. I don't believe you can simply map a struct because how is that going to be handled over multiple words?
    And I guess you mean bool not bit?

     
  • jonathanD - 2017-12-24

    Zitat:
    What do you mean you have mapped a struct type in the Modbus mapping? I would think that whatever you have needs to be broken down into either bits or packed in words, that's how I do it. Like using for example Application.PLC_PRG.MaskCW.x1 as an existing variable in the modbus mapping as bits. I don't believe you can simply map a struct because how is that going to be handled over multiple words?
    And I guess you mean bool not bit?

    Maybe I haven't explained myself correctly. I want to map a variable where I could refer to each of the bits separately, so I could name the bits in the code to something more readable.
    For example:
    Let's assume MaskCW is a WORD variable, and I want to control bit number 5 to operate a motor. I want to name this bit 'startMotor' without declaring each variable like 'Application.PLC_PRG.MaskCW.startMotor' in the mapping of the device.
    I want to refer it in the code like a structure, or any other way that will be easy to read like: MaskCW.startMotor := true;

    How can I do it?

    Thank you for your time and effort!

     
  • jonathanD - 2017-12-24

    I found a way to solve it.

    I created a Union with two vars, one of the struct type and the other with word type.
    In the GVL I declared a var of the Union type.
    In the mapping of the Modbus device I mapped the var from the GVL list and the var of WORD data type from the Union.
    Example:
    myUnion:
    x1 : myStructure;
    x2 : WORD;

    GVL:
    myVar : myUnion;

    Mapping:
    Application.myVar.x2

    Then in the code i can refer to what is listed in myStructure.

     

Log in to post a comment.