Bit access to DWORD Variable

Runtime
lucaver89
2019-12-17
2019-12-18
  • lucaver89

    lucaver89 - 2019-12-17

    Hi to all,

    the following code give me the following error "access requires a literal or an integer symbolic constant"

       PROGRAM PLC_PRG
       
       VAR
          my_word      :DWORD;
          scan_bit      :INT;
          test         :BOOL;
       END_VAR
    
       FOR scan_bit:=0 TO 24 BY 1 DO
       
          IF(my_word.my_word) THEN
             test := TRUE;
          ELSE
             test := FALSE;
          END_IF      
       END_FOR      
             
    

    Is there a solution to do it?

    Thank you for help
    Luca

     
  • JAPIB

    JAPIB - 2019-12-17

    I'm sory, I don't understand this : "IF(my_word.my_word)..."

    I think you wanted to write "IF(my_word. scan_bit)..."

    To my knowledge you can't use a variable to acces a bit of a word (it is the same with SINT, INT, DINT, USINT, UDINT, BYTE, WORD, DWORD)


    To acces to a bit of a word you can only use :
    - a value, for example you can test (or write ) "my_word.2" or "my_word.5" ...

    • a Global Constant
      VAR_GLOBAL CONSTANT
      Indice:INT:=2;
      END_VAR

    And you can test (or write ) "my_word.Indice"

    One solution to test each bit of a word may be to use SHL/SHR operators to shift and test each bit.
    You can see CODESYS HELP : https://help.codesys.com/webapp/_cds_op ... n=3.5.15.0

    BR

     
  • dFx

    dFx - 2019-12-18

    This could provide a nice access :see this post

     
  • lucaver89

    lucaver89 - 2019-12-18

    JAPIB hat geschrieben:
    I'm sory, I don't understand this : "IF(my_word.my_word)..."
    I think you wanted to write "IF(my_word. scan_bit)..."
    To my knowledge you can't use a variable to acces a bit of a word (it is the same with SINT, INT, DINT, USINT, UDINT, BYTE, WORD, DWORD)


    To acces to a bit of a word you can only use :
    - a value, for example you can test (or write ) "my_word.2" or "my_word.5" ...
    - a Global Constant
    VAR_GLOBAL CONSTANT
    Indice:INT:=2;
    END_VAR
    And you can test (or write ) "my_word.Indice"


    One solution to test each bit of a word may be to use SHL/SHR operators to shift and test each bit.
    You can see CODESYS HELP : https://help.codesys.com/webapp/_cds_op ... n=3.5.15.0
    BR

    Sorry for my error, yes You have right should be ```

    IF(my_word. scan_bit)

    ```
    Thank you for help

     

Log in to post a comment.