Output an integer > 255

  • Hello,


    I want to send a Z value "measured" by the robot via a search instruction. This value is now saved into a Double variable D020.


    This Z value can be bigger than 255mm so I need to send D020 to the PLC via 2 group outputs, OG68 & OG69.


    What is the best way to "break down" a double variable (or an integer one) into two bytes variables? Does somebody have a standard code for it?


    I wanted to create a loop and divide by 2 till I reach 0 and then assign each bit; but for this I need a modulo instruction which is not available...


    If possible I want to avoid using the ladder (only a system job).


    Many thanks!

  • SET D020 Z-Value


    SET D027 D020


    DIV D027 256


    SET B002 D027


    SET D027 256


    MUL D020 B002


    SUB D020 D027


    SET B003 D020


    DOUT OG(68) = B003


    DOUT OG(69) = B002

    Edited once, last by rAviteja_g ().

  • Dear rAviteja_g,


    If I use as an example Z-value of 1247mm => I got byte values B003 > 255?
    Is this code tested?


    Also an other question : with doubles, what happens when you divide and you get decimal number?


    eg 1247/256 = 4.87... ==> is the result 4 or 5?


    Many thanks!

  • I have typed error: the actual program is below- change the variables how you want. I tested- it is working fine. PLC side you group the two bytes as word, U see the value what you want to sent.



    NOP
    '--------------------------------
    'PLEASE ENTER THE REQUIRED
    'ROTATION VALUE IN VARIABLE D020
    'Z Value in D20

    '--------------------------------
    SET D020 1247
    '________________________________
    'COPY D001
    SET D001 D020
    'DIVIDE BY 256
    DIV D001 256
    'SAVING IN BYTE2
    SET B002 D001
    '------------
    SET D001 256
    MUL D001 B002
    SUB D020 D001
    SET B003 D000
    '------------
    'SENDING DATA TO PLC
    DOUT OG#(67) B003
    DOUT OG#(66) B002
    '---------------
    'COMPLETE TRANSFER
    '--------------------
    END






    when you get DIV 1247 256 --- U'll get 4 the fractional part we dont get.



    Thanks.

  • I don't have a pendant in front of me, but I would think you should be able to do something like this. So you'll be giving the value to the PLC through the groups, and they should be able to BTD it into a tag and do the addition to get the final number



    If your values are going to be larger than 512, then you should be able to create separate if statements that will cover you for when the D value is higher. You would need more than two groups though.


    // If the value is less/equal to 255 only write to one OG
    If D20 <= 256 then
    Set OG68 D00020
    Set OG69 0
    Endif


    //If the value is larger than 256, but less than 512, set first group to 255
    // then we subtract 256 from the D variable and set
    //the remainder into the second OG
    Elseif D00020 > 256 AND < 512
    Set OG68 256
    Sub D0020 256
    Set OG69 D0020
    Endif
    //If the data is larger than 512, but less than 1024,
    //We do the same thing, but subtract 512 once we have
    //populated the first two groups with that value
    Elseif D0020 >512 AND < 1024
    Set OG68 256
    Set OG69 256
    Sub D0020 512
    Set OG 70 DO20
    Endif
    //or even larger than 1024
    Elseif D0020 >1024 AND <2048
    Set OG68 256
    Set OG69 256
    Set OG70 256
    Sub D0020 1024
    Set OG 71 D0020
    Endif


    I don't have a pendant in front of me, so I can't test this for you right now... but I think that should work?

Advertising from our partners