1

I'm learning how to program PLC with Codesys. Right now I'm trying to map some variable on outport of a plc in Fluidsim. I've already seen the program function declearing the variabels as

EB0 AT %QB0: BYTE;

Now it's pretty ugly to use EB0 dot the port (like EB0.0). I would like something more programmer friendly like:

EmergencyLight AT %QB0.0: BOOL;

but it elevate an error. ChatGPT says that it can be used in struct, something like:

LightOutputs AT %QB0 : STRUCT
        Emergency: BOOL;        // Q0.0
        Activation: BOOL;        // Q0.1
        Conveyer: BOOL;         // Q0.2
END_STRUCT;

but I can't make it work (also no reference on documenation, at least that I can find).

Any suggestion to how to make it works?

1 Answer 1

2

In codesys, you can't reference a single bit with a normal variable, so EmergencyLight AT %QB0.0: BOOL; is invalid.

You can access individual bits of any integer types (docs), so EB0.2 := TRUE is valid, however, if you want to have meaningful names for each bit, one option would be to have constants as bit indexes (read in the same docs), so something like this:

VAR_GLOBAL CONSTANT
    EmergencyLight: USINT := 2;
END_VAR

PROGRAM PLC_PRG
    EB0.EmergencyLight := TRUE;
END_PROGRAM

But there's also another, arguably better option, and that is to define a custom struct with bit mapping (docs), which is what the LLM suggested to you, but the syntax was incorrect:

TYPE OUTPUT_MAPPING_1 :
STRUCT
    Emergency: BIT;    // Qx.0
    Activation: BIT;   // Qx.1
    Conveyer: BIT;     // Qx.2

    {attribute 'hide'}
    _reserved1: BIT;
    {attribute 'hide'}
    _reserved2: BIT;
    {attribute 'hide'}
    _reserved3: BIT;

    Light: BIT;        // Qx.6
END_STRUCT
END_TYPE

PROGRAM PLC_PRG
VAR
    out AT %QB0: OUTPUT_MAPPING_1;
END_VAR
    
    out.Light := TRUE; // same as "out.6 := TRUE;" if out was of type BYTE
END_PROGRAM
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I looking for. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.