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..



  • lemster68lemster68 United States ✭✭✭
    Is this via a group input?  If so, why not make one, sixteen bit group?
    Lee Justice
  • PabaPaba Germany
    edited July 2019
    yes, but for tests i'm working with a virtual irc5 and think i only can use<br>
    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

  • PabaPaba Germany
    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;
            RETURN mergedbytes;
    Post edited by Paba on
  • DenisFRDenisFR FRANCE ✭✭✭
    You can define a group encompass your bytes.
    <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"

  • MickyMicky Germany ✭✭✭

    you can calculate the value as follows:
    TPWrite "Value: "\num:=CalcValue(76,162);
    FUNC num CalcValue(byte byte1,byte byte2)
        RETURN byte1*256+byte2;
  • PabaPaba Germany
    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:

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

    Best Greetings.
  • lemster68lemster68 United States ✭✭✭
    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
  • PabaPaba Germany
    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 :)
  • MickyMicky Germany ✭✭✭
    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:
    You can also use here the bit or byte swap, e.g. "23-16,31-24"

    best regards
  • PabaPaba Germany
    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
Sign In or Register to comment.