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

Convert decimal to 128 bits

kmckinney
2015-09-01
2015-09-08
  • kmckinney - 2015-09-01

    Hello, I am new to codesys and I'm trying figure out a good method that would allow me to receive a decimal number (input) and convert it to binary allowing me to turn on 128 bits (outputs). I could really use some suggestions.

    thanks for your input

     
  • Anonymous - 2015-09-03

    Originally created by: scott_cunningham

    Simple to do in ST:

    PROGRAM PLC_PRG
    VAR
       Out   : ARRAY[0..127] OF BOOL;
       Decimal : WORD;
       Temp : WORD;
       J: INT;
    END_VAR
    Temp := Decimal;
    FOR J := 0 TO 15 DO
       Out[J] := Temp.0;
       Temp := SHR(Temp, 1);
    END_FOR
    

    Typically BIT operations are used on type BYTE/WORD/DWORD for clarity (instead of INT, DINT), but the example code also works if Decimal is an INT or DINT.

     
  • kmckinney - 2015-09-03

    Scott first thank you for your response.

    Just to give you more detail.

    A customer makes a purchase and he/she has the 128 options. I'm trying to receive 1 variable that can be a combination of any of those 128 options. Obviously that is 4 DWords. I was thinking decimal might be the best way

    I'm not sure

    Or maybe have them send me an string of 128 (0 or 1)....but then I'd need to convert that into an array of bool....

    If you have suggestions let me know.

    Thanks again

     
  • Anonymous - 2015-09-03

    Originally created by: scott_cunningham

    I corrected my last post - shifting over 128 bits against a WORD won't help (only gives outputs 0..15).

    How is the information being delivered to you? Over a fieldbus, by a file, by ??? The most compact data transfer is by sending you 4 DWORDs, each DWORD controlling 32 outputs. You could require a 128 character string, but that costs transmission time and seems a bit crude.

    So maybe quickly solve as:

    PROGRAM MapOutputs
    VAR_INPUT
       Outs_92_131 : DWORD;
       Outs_62_91  : DWORD;
       Outs_32_61  : DWORD;
       Outs_0_31   : DWORD;
    END_VAR
    VAR
       Out   : ARRAY[0..127] OF BOOL;
    END_VAR
    FOR J := 0 TO 127 DO
       IF J = 0 THEN
          Temp := Out_0_31;
       ELSIF J = 32 THEN
          Temp := Out_32_61;
       ELSIF J = 64 THEN
          Temp := Out_62_95;
       ELSIF J = 96 THEN
          Temp := Out_92_127;
       END_IF
       Out[J] := Temp.0;
       Temp := SHR(Temp, 1);
    END_FOR
    

    There are many other more ways to solve this - offered as a down-and-dirty method to show concept.

    You may also want to think about using a structure of BOOLs (not the same as a structure of BITS) if you want your code to show meaning for the output instead of just a bit on a DWORD. For example, it may make more sense to specifically call out things like option A, option B, feature 17. From there you can map those specific items to specific outputs. For troubleshooting this is "better" because it speaks human. For coding, it takes longer:

    TYPE T_OUT :
    STRUCT
       Option1   : BOOL;
       Option2   : BOOL;
       FeatureA   : BOOL;
       TopNotch   : BOOL;
       ...
    END_STRUCT
    END_TYPE
    PROGRAM PLC_PRG
    VAR_INPUT
       Outs   : T_OUT;
    END_VAR
    

    Then map each item of the structure to an output on your hardware IO mapping, which would look like:

    Ultimately, you really have to decide how your "customer" can give you the information easily and without errors such as flipping bit order, etc.

    IMG: output_map.png

     
  • shooter - 2015-09-07

    128 bits is a very very big decimal number.
    so 8 bits is 255 16 bits is 65535
    128 is 3,4028236692093846346337460743177e+38 oops so other solution isneeded.
    so we will need more info.

     
  • kmckinney - 2015-09-08

    Thank you for your response shooter.

    When a customer makes a purchase, the POS needs to send me some type of information that lets me know what combination of outputs need to be turned on (what combination of outputs the customer purchased).

    My original thought was POS sends me a decimal over modbus and I convert to a word. But like you stated the decimal for 128 bits is too large for decimal form. So now I'm trying to determine the next best solution.

    POS could send array, string, maybe 4 double words. But I think I'd prefer an array. Reason being, the array index could represent the bits and the value of the array at the index could represent the value of the bit.

    That is what I am thinking now but I would love suggestions from another who has more experience than me. This is my first time working with Modbus so I'd like to communicate as efficiently as possible.

    Thank you again for responding to my question, I look forward to reading your response

     
  • shooter - 2015-09-08

    so you get a limited decimal number

    find the number in an array, giving you back the type number.
    with this type you can set a lot of bits by using for simplicity 16 arrays of 8 bits or 8 arrays with words.
    example POS says 123
    array [200 types,2]
    find number 123 in array, if found you get back a typenumber
    another array has out [200,8] words
    so go to typenumber and you will find the bits needed.

     
  • kmckinney - 2015-09-08

    shooter hat geschrieben:
    so you get a limited decimal number
    find the number in an array, giving you back the type number.
    with this type you can set a lot of bits by using for simplicity 16 arrays of 8 bits or 8 arrays with words.
    example POS says 123
    array [200 types,2]
    find number 123 in array, if found you get back a typenumber
    another array has out [200,8] words
    so go to typenumber and you will find the bits needed.

    I'm not sure i am understanding. Do you know where I can research the concept to better understand.
    What do you mean array [200 types, 2]? A 2-dimensional array of...? Is 123 a value of an array[index]? What exactly is typenumber...never heard of it before. Please know I am asking for clarity not being a pain or sarcastic. I am really trying to understand. Thank you for all your help

     
  • shooter - 2015-09-08

    lets assume the factory makes a cabinet with max 128 drawers (your bits)
    the customer checks the catalogue
    in the cat there are lots of types like 3 drawer bottom is type 1
    3 drawer top is type 2
    3 drawer 5 from bottom is type 3
    5 drawer 5 from bottom 6 drawers open then 2 drawers is type 4
    etc.

    and picks a cabinet.
    he gives you the typenumber for example 3 as a word.
    you have an array with these typenumbers as look up list and 8 words so for easy it is array [9,100] of word:=(.......,3,11100000,0,0,0,0,0,0,0,4,0000001111100000,0000000000000011,.......)
    so word1 is array[2,2]word2 is 3,2 etc.

     

Log in to post a comment.