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

Constants and or Enumeration in a Function Block

dengsberg
2021-03-31
2021-03-31
  • dengsberg - 2021-03-31

    I have created a function block with a Method. I would like to pass this method a Constant or an Enumeration value, from the outside the function block. I would like the constants and enumerations contained within the function block itself, but have access to them from the outside.

    So if the function block is called myFunctionBlock, and the method is call SendChar, and the Constant is CHR_STX, it looks like this :
    myBlock : myFunctionBlock;
    myBlock.SendChar(CHR_STX);

    I have something working, but I don't think it is the proper way to do it. I created some constants of scope VAR_OUTPUT. Is there a better way?

    VAR_OUTPUT CONSTANT
    CHR_STX: BYTE := 16#02;
    CHR_ETX: BYTE := 16#03;
    CHR_ACK: BYTE := 16#06;
    CHR_NAK: BYTE := 16#15;
    END_VAR

    I would also like to create an ErrorCode enumeration that is contained within the function block, but can be accessed by the outside. How can I do this?
    Something like this:
    TYPE enumErrorCode :
    (
    NONE := 0,
    CHECKSUM := 1,
    ADDRESS := 2,
    LENGTH :=3
    );
    END_TYPE

    (I'm using version 3.5 SP16)

     
  • tvm - 2021-03-31

    Why don't you just create an ENUM, call it ASCII or whatever

    TYPE ASCII
    {
      CHR_STX:= 16#02;
      CHR_ETX:= 16#03;
      CHR_ACK:= 16#06;
      CHR_NAK:= 16#15;
    } BYTE
    END_TYPE
    

    then just make your method input of type ASCII

    VAR_INPUT
      In:  ASCII;
    END_VAR
    

    Then call your method like this:

    myBlock.SendChar(ASCII.CHR_STX);
    

    same thing with the error code ENUM. Just create the ENUM and you can use it anywhere.

    VAR
      ErrorCode:  enumErrorCode;
    END_VAR
    
     
  • dengsberg - 2021-03-31

    I was hoping to keep the enum contained within the function block, not the entire project. So if I have other projects that uses the function block, I don't have to create the enum again and again.

    Is it different with libraries? It seems like I can download a library and the library contains enums within itself. That is what I was shooting for.

     
  • tvm - 2021-03-31

    Yes, then I would create a library that contains the ENUM as well as the function block. Then you can reuse them in whatever project you install the library into.

     

Log in to post a comment.