Byte Swap Instruction

Join Date
Oct 2015
Location
alabama
Posts
30
Good Morning,

I'm using RS Logix 5000 to program an Allen-Bradley 1769-QBFC1B Version 20.12 and want to use the Byte Swap Instruction (SWPB), but not having much luck with it. I've attached a picture of the logic I've written for it and trying to debug it.

Currently, I have a scanner that reads a badge and takes a 6 digit index number and stores it into the Cognex_Scanner:I.ResultData[0] tag (SINT). Then I use the COP instruction to move it into Index_Number_Self_Check_Part_1.DATA[0] (STRING).

Then I convert the String to a DINT using the STOD instruction. So the value goes from 054609 to 54609. I actually need a way to keep the leading 0 as well. So I need it to read 054609 in the DINT tag also.

Finally, I apply the SWPB instruction on the DINT tag using order mode HIGH/LOW or 2. Ultimately, I want to go from 054609 to 506490, but it reads 20949, which makes no sense to me. The index_number_self_check_part_1_byte_swap that reads 20949 is also a DINT.

Any help debugging this code would surely be appreciated. Thanks.

P.S. And yes, I am toggling the input bits, so first I toggle 'Badge Scan', then I toggle "always_off_bit", and then I toggle "test".

badge scan byte swap.jpg
 
HIGHLOW SWPB of 54609 will always return 20949; HIGHLOW will "swap" the first byte with the second and the third with the fourth (run your Windows Calculator in Programmer mode -Decimal and Dword).
You will never be able to ad a "0" in front of a decimal number.
Why do you want to "swap" bytes in a string?! There are no bytes in STRING data types but characters.
If you need to "rearrange" a string there are ASCII String Instructions that might do the job: CONCAT, DELETE, FIND, INSERT, MID.
 
HIGHLOW SWPB of 54609 will always return 20949; HIGHLOW will "swap" the first byte with the second and the third with the fourth (run your Windows Calculator in Programmer mode -Decimal and Dword).
You will never be able to ad a "0" in front of a decimal number.
Why do you want to "swap" bytes in a string?! There are no bytes in STRING data types but characters.
If you need to "rearrange" a string there are ASCII String Instructions that might do the job: CONCAT, DELETE, FIND, INSERT, MID.

No we first convert the string to a DINT using the STOD instruction. Then we swap the bytes of the DINT tag using SWPB.
 
Thanks for all the help, I ended up using the MID instruction to grab each individual character, and then used CONCAT to combine them into one string.

I still do not understand how the BTD instruction you guys showed me; I looked it up on the manual but still don't understand how it works. If you guys have any other helpful examples about the BTD instruction, please share. Thanks!
 
The easiest thing is to create a string to use to swap data. Then copy Result Data[0] to Swap Data[1] and Copy Result Data[1] to Swap Data[0]. Do all characters you need to swap then Convert Swap String to DINT.

See Attached

Capture.jpg
 
Last edited:
The easiest thing is to create a string to use to swap data. Then copy Result Data[0] to Swap Data[1] and Copy Result Data[1] to Swap Data[0]. Do all characters you need to swap then Convert Swap String to DINT.

See Attached

Thanks for the example, this makes more sense now and would have saved me more tags in the end.
 

Similar Topics

I have a C-More HMI that changes my PLC String from "Machine Status" to "aMhcni etStasu" . There is an option with other objects that have string...
Replies
15
Views
3,413
I am writing software for a system(*) that has the following data flow 4-20mA Sensor => Siemens AI 8xU/I/RTD/TC ST card Siemens AI 8xU/I/RTD/TC...
Replies
16
Views
6,482
I ran into a situation where some devices I use display String bytes in a swapped order as they are stored in my PLC (CompactLogix). I thought it...
Replies
3
Views
3,810
Hi guys I'm having some weird data conversion issue over modbus happening. And I found a post here before that is similar to what I'm seeing but...
Replies
6
Views
7,073
Hello everybody, I have a communication to a Linux-PC via sockets. So I created a quite large UDT with all the content. Unfortunately we have to...
Replies
1
Views
2,140
Back
Top Bottom