Split up / merge 2bytes to num or hex
Paba
✭
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:
0
Comments
-
Is this via a group input? If so, why not make one, sixteen bit group?Lee Justice0
-
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
0 -
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 on1 -
Hello,You can define a group encompass your bytes.Like:
<div> -Name "giFirstByte" -SignalType "GI" -Device "MyPNDevice" -DeviceMap "0-7"<br> -Name "giSecondetByte" -SignalType "GI" -Device "MyPNDevice" -DeviceMap "8-15"<br></div> -Name "giMyWord" -SignalType "GI" -Device "MyPNDevice" -DeviceMap "0-15"
1 -
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
Micky1 -
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:
0 -
Thanks Micky, this works as expected.Denis, can you tell me what i've mapped wrong?Best Greetings.0
-
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 Justice0
-
yes that's propably the simplest way.. too easy to think about it 2nd idea wont work but mapping first to second byte is doing the job.Thank you all0
-
Hello Papa,why don't you combine the two bytes to a group input (16 bit), then you save yourself the later conversionYou 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
Micky0 -
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
0
Categories
- All Categories
- 5.5K RobotStudio
- 394 UpFeed
- 18 Tutorials
- 13 RobotApps
- 297 PowerPacs
- 405 RobotStudio S4
- 1.8K Developer Tools
- 249 ScreenMaker
- 2.7K Robot Controller
- 309 IRC5
- 59 OmniCore
- 7 RCS (Realistic Controller Simulation)
- 785 RAPID Programming
- AppStudio
- 3 RobotStudio AR Viewer
- 18 Wizard Easy Programming
- 105 Collaborative Robots
- 4 Job listings