RobotStudio event

Split up / merge 2bytes to num or hex

Hello everyone,
i'm receiving 2 bytes from a plc and wonder if there's a way to merge them. I thought about 'convert' them to hexadecimal or just add the binary data together..
for example .. incoming values byte 1: 76, byte 2: 162  -- desired result: num: 19618
        0x4C & 0xA2  --> 0x4CA2
or    01001100 & 10100010  --> 0100110010100010
 and then convert it to decimal..



Tagged:

Comments

  • lemster68
    lemster68 ✭✭✭
    Is this via a group input?  If so, why not make one, sixteen bit group?
    Lee Justice
  • Paba
    Paba
    edited July 2019
    yes, but for tests i'm working with a virtual irc5 and think i only can use<br>
    https://robotapps.robotstudio.com/#/viewApp/2cab7b4f-754c-415b-a32c-cbe4da9353e9
    (RSConnectGIOToSnap7)
    which only provides singles bytes..
    For real profinet connection lateron, i already set up 16bit groups.. but for now this wont work i think.
    Correct me if i'm wrong


  • Paba
    Paba
    edited July 2019
    Ok i've found the correct syntax (ByteToStr(byte\Hex)).. was'nt as hard as i expected

        FUNC num mergedX(byte x0,byte x1)
            VAR bool b_temp;
            Xkoord:=hexToDec(ByteToStr(x0\Hex)+ByteToStr(x1\Hex));
            b_temp:=StrToVal(Xkoord,mergedbytes);
            RETURN mergedbytes;
        ENDFUNC
    Improvements?
    Post edited by Paba on
  • DenisFR
    DenisFR ✭✭✭
    Hello,
    You can define a group encompass your bytes.
    Like:
    <div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -Name "giFirstByte" -SignalType "GI" -Device "MyPNDevice" -DeviceMap "0-7"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -Name "giSecondetByte" -SignalType "GI" -Device "MyPNDevice" -DeviceMap "8-15"<br></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -Name "giMyWord" -SignalType "GI" -Device "MyPNDevice" -DeviceMap "0-15"





    ☑️2024 - RobotStudio® User Group

  • Micky
    Micky ✭✭✭
    Hello,

    you can calculate the value as follows:
    TPWrite "Value: "\num:=CalcValue(76,162);
    
    FUNC num CalcValue(byte byte1,byte byte2)
        RETURN byte1*256+byte2;
    ENDFUNC
    BR
    Micky
  • Ok this looks better.. but when i do so, the result is byte-swapped..
    i tried to map the word vice versa but then it comes to a nonsense result.
    Bytes swapped:
    ... dont know:


  • Thanks Micky, this works as expected.
    Denis, can you tell me what i've mapped wrong?

    Best Greetings.
  • lemster68
    lemster68 ✭✭✭
    What if you map first byte to second and vice versa?  Leave word as is.  Or inverse map first and second byte, also leave word as is.
    Lee Justice
  • yes that's propably the simplest way.. too easy to think about it :D  2nd idea wont work but mapping first to second byte is doing the job.

    Thank you all :)
  • Micky
    Micky ✭✭✭
    Hello Papa,
    why don't you combine the two bytes to a group input (16 bit), then you save yourself the later conversion
    You can define the signal mapping for the group inputs as follows:
    "16-23,24-31"
    You can also use here the bit or byte swap, e.g. "23-16,31-24"

    best regards
    Micky
  • Thx Mickey, thats the way i'm doing it right now.
    First Byte mapped from 24-31, second Byte from 16-23 (to avoid byte swapping), Word mapped 16-31 and everything works fine