Codesys v3.5 Sint to byte

abjha1998
2024-04-22
2024-04-24
  • abjha1998 - 2024-04-22

    Hi everyone,
    I have been doing a project where I have to send CAN messages from WAGO plc pf200 in BYtes. The signed integer needs to be unpacked into two bytes. so far I have tried Union but that has not worked with me

     
  • bjarne-pagaard - 2024-04-24

    Hi
    A SINT is a short (signed) integer. It is already only 1 byte - so you should have no problem casting it to a byte like so: bMyByte := TO_BYTE(sintMyShortInt);
    If you have a regular INT you want to put in 2 bytes - there are a lot of ways you can do this. A Union is certainly one of them. You could have a union with 2 memebers: An array of 2 bytes as one member, and an integer value as another member.

    Another way would be to look at MEMCPY to put the value into your CAN-message.
    .. or create a function to take your input value as input, and giving you 2 individual bytes as output. This could be handy if you need to change the byte-order.

    Integer data types:
    https://help.codesys.com/webapp/_cds_datatype_integer;product=codesys;version=3.5.17.0

    -Bjarne

     

Log in to post a comment.